1- use  crate :: { tap_config:: TapConfig ,  tap_error:: TapError } ; 
1+ use  crate :: { tap_config:: TapConfig ,  tap_error:: TapError ,  ShapeId ,  TapReaderBuilder ,  TapShape } ; 
2+ use  indexmap:: IndexMap ; 
23use  serde_derive:: { Deserialize ,  Serialize } ; 
3- use  std:: path:: Path ; 
4+ use  std:: { io ,   path:: Path } ; 
45use  tracing:: debug; 
56
67#[ derive( Debug ,  Serialize ,  Deserialize ) ]  
78struct  TapShapeId ( String ) ; 
89
9- #[ derive( Debug ,  Serialize ) ]  
10+ #[ derive( Debug ,  Serialize ,   PartialEq ) ]  
1011pub  struct  DCTap  { 
1112    version :  String , 
13+     shapes :  IndexMap < Option < ShapeId > ,  TapShape > , 
1214} 
1315
1416impl  Default  for  DCTap  { 
@@ -21,12 +23,58 @@ impl DCTap {
2123    pub  fn  new ( )  -> DCTap  { 
2224        DCTap  { 
2325            version :  "0.1" . to_string ( ) , 
26+             shapes :  IndexMap :: new ( ) , 
2427        } 
2528    } 
2629
27-     pub  fn  read_buf ( _path :  & Path ,  _config :  TapConfig )  -> Result < DCTap ,  TapError >  { 
28-         let  dctap = DCTap :: new ( ) ; 
30+     pub  fn  add_shape ( & mut  self ,  shape :  & TapShape )  { 
31+         self . shapes . insert ( shape. shape_id ( ) ,  shape. clone ( ) ) ; 
32+     } 
33+ 
34+     pub  fn  from_path ( path :  & Path ,  _config :  TapConfig )  -> Result < DCTap ,  TapError >  { 
35+         let  mut  dctap = DCTap :: new ( ) ; 
36+         debug ! ( "DCTap parsed: {:?}" ,  dctap) ; 
37+         let  mut  tap_reader = TapReaderBuilder :: new ( ) . flexible ( true ) . from_path ( path) ?; 
38+         for  maybe_shape in  tap_reader. shapes ( )  { 
39+             let  shape = maybe_shape?; 
40+             println ! ( "Shape read: {shape:?}" ) ; 
41+             dctap. add_shape ( & shape) 
42+         } 
43+         Ok ( dctap) 
44+     } 
45+ 
46+     pub  fn  from_reader < R :  io:: Read > ( reader :  R )  -> Result < DCTap ,  TapError >  { 
47+         let  mut  dctap = DCTap :: new ( ) ; 
2948        debug ! ( "DCTap parsed: {:?}" ,  dctap) ; 
49+         let  mut  tap_reader = TapReaderBuilder :: new ( ) . flexible ( true ) . from_reader ( reader) ?; 
50+         for  maybe_shape in  tap_reader. shapes ( )  { 
51+             let  shape = maybe_shape?; 
52+             println ! ( "Shape read: {shape:?}" ) ; 
53+             dctap. add_shape ( & shape) 
54+         } 
3055        Ok ( dctap) 
3156    } 
3257} 
58+ 
59+ #[ cfg( test) ]  
60+ mod  tests { 
61+     use  crate :: { PropertyId ,  TapShape ,  TapStatement } ; 
62+ 
63+     use  super :: * ; 
64+ 
65+     #[ test]  
66+     fn  test_simple ( )  { 
67+         let  data = "\  
68+ 
69+ Person,PersonLabel,knows,KnowsLabel 
70+ " ; 
71+         let  dctap = DCTap :: from_reader ( data. as_bytes ( ) ) . unwrap ( ) ; 
72+         let  mut  expected_shape = TapShape :: new ( ) ; 
73+         expected_shape. set_shape_id ( & ShapeId :: new ( "Person" ) ) ; 
74+         expected_shape. add_statement ( TapStatement :: new ( PropertyId :: new ( "knows" ) ) ) ; 
75+ 
76+         let  mut  expected_dctap = DCTap :: new ( ) ; 
77+         expected_dctap. add_shape ( & expected_shape) ; 
78+         assert_eq ! ( dctap,  expected_dctap) ; 
79+     } 
80+ } 
0 commit comments