1+ use facet:: Facet ;
12use num_traits:: { Bounded , FromPrimitive , Num , ToPrimitive } ;
23use serde:: Serialize ;
34use serde:: de:: DeserializeOwned ;
@@ -6,8 +7,6 @@ use std::fmt;
67use std:: ops:: AddAssign ;
78
89use std:: { convert:: TryInto , str:: FromStr } ;
9- use strum:: IntoEnumIterator ;
10- use strum_macros:: { EnumIter , EnumString } ;
1110
1211use super :: serializer:: { FixedSizeKeySerializer , KeySerializer } ;
1312use crate :: serializer:: KeyVec ;
@@ -22,7 +21,9 @@ use std::result::Result as StdResult;
2221pub type NodeID = u64 ;
2322
2423/// The fully qualified name of an annotation.
25- #[ derive( Serialize , Deserialize , Default , Eq , PartialEq , PartialOrd , Ord , Clone , Debug , Hash ) ]
24+ #[ derive(
25+ Facet , Serialize , Deserialize , Default , Eq , PartialEq , PartialOrd , Ord , Clone , Debug , Hash ,
26+ ) ]
2627pub struct AnnoKey {
2728 /// Name of the annotation.
2829 pub name : String ,
@@ -31,7 +32,9 @@ pub struct AnnoKey {
3132}
3233
3334/// An annotation with a qualified name and a value.
34- #[ derive( Serialize , Deserialize , Default , Eq , PartialEq , PartialOrd , Ord , Clone , Debug , Hash ) ]
35+ #[ derive(
36+ Facet , Serialize , Deserialize , Default , Eq , PartialEq , PartialOrd , Ord , Clone , Debug , Hash ,
37+ ) ]
3538pub struct Annotation {
3639 /// Qualified name or unique "key" for the annotation
3740 pub key : AnnoKey ,
@@ -40,7 +43,9 @@ pub struct Annotation {
4043}
4144
4245/// Directed edge between a source and target node which are identified by their ID.
43- #[ derive( Serialize , Deserialize , Eq , PartialEq , PartialOrd , Ord , Clone , Debug , Hash , Default ) ]
46+ #[ derive(
47+ Facet , Serialize , Deserialize , Eq , PartialEq , PartialOrd , Ord , Clone , Debug , Hash , Default ,
48+ ) ]
4449#[ repr( C ) ]
4550pub struct Edge {
4651 pub source : NodeID ,
@@ -135,7 +140,8 @@ pub trait ComponentType:
135140}
136141
137142/// A simplified implementation of a `ComponentType` that only has one type of edges.
138- #[ derive( Clone , Eq , PartialEq , PartialOrd , Ord , EnumString , EnumIter , Debug ) ]
143+ #[ derive( Facet , Clone , Eq , PartialEq , PartialOrd , Ord , Debug ) ]
144+ #[ repr( u16 ) ]
139145pub enum DefaultComponentType {
140146 Edge ,
141147}
@@ -160,7 +166,7 @@ impl fmt::Display for DefaultComponentType {
160166
161167pub struct DefaultGraphIndex ;
162168
163- #[ derive( Serialize , Deserialize ) ]
169+ #[ derive( Facet , Serialize , Deserialize ) ]
164170pub struct DefaultGlobalStatistics ;
165171
166172impl ComponentType for DefaultComponentType {
@@ -173,15 +179,27 @@ impl ComponentType for DefaultComponentType {
173179 Ok ( DefaultGraphIndex { } )
174180 }
175181 fn all_component_types ( ) -> Vec < Self > {
176- DefaultComponentType :: iter ( ) . collect ( )
182+ vec ! [ DefaultComponentType :: Edge ]
177183 }
178184 fn calculate_global_statistics ( _graph : & mut Graph < Self > ) -> StdResult < ( ) , ComponentTypeError > {
179185 Ok ( ( ) )
180186 }
181187}
182188
189+ impl FromStr for DefaultComponentType {
190+ type Err = GraphAnnisCoreError ;
191+
192+ fn from_str ( s : & str ) -> StdResult < Self , Self :: Err > {
193+ if s == "Edge" {
194+ Ok ( Self :: Edge )
195+ } else {
196+ Err ( GraphAnnisCoreError :: InvalidComponentType ( s. to_string ( ) ) )
197+ }
198+ }
199+ }
200+
183201/// Identifies an edge component of the graph.
184- #[ derive( Serialize , Deserialize , Eq , PartialEq , PartialOrd , Ord , Hash , Clone , Debug ) ]
202+ #[ derive( Facet , Serialize , Deserialize , Eq , PartialEq , PartialOrd , Ord , Hash , Clone , Debug ) ]
185203pub struct Component < CT : ComponentType > {
186204 /// Type of the component
187205 ctype : u16 ,
@@ -257,3 +275,20 @@ impl NumValue for u64 {}
257275impl NumValue for u32 { }
258276impl NumValue for u16 { }
259277impl NumValue for u8 { }
278+
279+ #[ cfg( test) ]
280+ mod tests {
281+ use std:: str:: FromStr ;
282+
283+ use crate :: { errors:: GraphAnnisCoreError , types:: DefaultComponentType } ;
284+
285+ #[ test]
286+ fn parse_invalid_component_type ( ) {
287+ let result = DefaultComponentType :: from_str ( "doesnotexist" ) ;
288+ assert ! ( result. is_err( ) ) ;
289+ assert ! ( matches!(
290+ result. unwrap_err( ) ,
291+ GraphAnnisCoreError :: InvalidComponentType ( _)
292+ ) ) ;
293+ }
294+ }
0 commit comments