@@ -44,7 +44,7 @@ macro_rules! impl_as_type {
4444#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
4545pub enum ValueKind {
4646 U8 , U16 , U32 , U64 , U128 , I8 , I16 , I32 , I64 , I128 , F32 , F64 ,
47- String , Bool , Matrix ( Box < ValueKind > , Vec < usize > ) , Set , Map , Record , Table , Tuple , Id , Index , Reference , Atom ( u64 ) , Empty , Any
47+ String , Bool , Matrix ( Box < ValueKind > , Vec < usize > ) , Enum ( u64 ) , Set , Map , Record , Table , Tuple , Id , Index , Reference , Atom ( u64 ) , Empty , Any
4848}
4949
5050#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -84,6 +84,7 @@ pub enum Value {
8484 Record ( MechMap ) ,
8585 Table ( MechTable ) ,
8686 Tuple ( MechTuple ) ,
87+ Enum ( Box < MechEnum > ) ,
8788 Id ( u64 ) ,
8889 Index ( Ref < usize > ) ,
8990 MutableReference ( MutableReference ) ,
@@ -117,6 +118,7 @@ impl Hash for Value {
117118 Value :: Table ( x) => x. hash ( state) ,
118119 Value :: Tuple ( x) => x. hash ( state) ,
119120 Value :: Record ( x) => x. hash ( state) ,
121+ Value :: Enum ( x) => x. hash ( state) ,
120122 Value :: String ( x) => x. hash ( state) ,
121123 Value :: MatrixBool ( x) => x. hash ( state) ,
122124 Value :: MatrixIndex ( x) => x. hash ( state) ,
@@ -166,6 +168,7 @@ impl Value {
166168 Value :: Table ( x) => { return x. pretty_print ( ) ; } ,
167169 Value :: Tuple ( x) => { return x. pretty_print ( ) ; } ,
168170 Value :: Record ( x) => { return x. pretty_print ( ) ; } ,
171+ Value :: Enum ( x) => { return x. pretty_print ( ) ; } ,
169172 Value :: MatrixIndex ( x) => { return x. pretty_print ( ) ; }
170173 Value :: MatrixBool ( x) => { return x. pretty_print ( ) ; }
171174 Value :: MatrixU8 ( x) => { return x. pretty_print ( ) ; } ,
@@ -186,7 +189,6 @@ impl Value {
186189 Value :: IndexAll => builder. push_record ( vec ! [ ":" ] ) ,
187190 Value :: Id ( x) => builder. push_record ( vec ! [ format!( "{:?}" , humanize( x) ) ] ) ,
188191 Value :: Kind ( x) => builder. push_record ( vec ! [ format!( "{:?}" , x) ] ) ,
189-
190192 } ;
191193 let mut table = builder. build ( ) ;
192194 table. with ( Style :: modern ( ) ) ;
@@ -226,6 +228,7 @@ impl Value {
226228 Value :: MatrixF32 ( x) => x. shape ( ) ,
227229 Value :: MatrixF64 ( x) => x. shape ( ) ,
228230 Value :: MatrixValue ( x) => x. shape ( ) ,
231+ Value :: Enum ( x) => vec ! [ 1 , 1 ] ,
229232 Value :: Table ( x) => x. shape ( ) ,
230233 Value :: Set ( x) => vec ! [ 1 , x. set. len( ) ] ,
231234 Value :: Map ( x) => vec ! [ 1 , x. map. len( ) ] ,
@@ -276,6 +279,7 @@ impl Value {
276279 Value :: Map ( x) => ValueKind :: Map ,
277280 Value :: Record ( x) => ValueKind :: Record ,
278281 Value :: Tuple ( x) => ValueKind :: Tuple ,
282+ Value :: Enum ( x) => ValueKind :: Enum ( x. id ) ,
279283 Value :: MutableReference ( x) => ValueKind :: Reference ,
280284 Value :: Empty => ValueKind :: Empty ,
281285 Value :: IndexAll => ValueKind :: Empty ,
@@ -628,4 +632,32 @@ impl Hash for MechTuple {
628632 x. hash ( state)
629633 }
630634 }
635+ }
636+
637+ // Enum -----------------------------------------------------------------------
638+
639+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
640+ pub struct MechEnum {
641+ pub id : u64 ,
642+ pub variants : Vec < ( u64 , Option < Value > ) > ,
643+ }
644+
645+ impl MechEnum {
646+
647+ pub fn pretty_print ( & self ) -> String {
648+ let mut builder = Builder :: default ( ) ;
649+ let string_elements: Vec < String > = vec ! [ format!( "{}{:?}" , self . id, self . variants) ] ;
650+ builder. push_record ( string_elements) ;
651+ let mut table = builder. build ( ) ;
652+ table. with ( Style :: modern ( ) ) ;
653+ format ! ( "{table}" )
654+ }
655+
656+ }
657+
658+ impl Hash for MechEnum {
659+ fn hash < H : Hasher > ( & self , state : & mut H ) {
660+ self . id . hash ( state) ;
661+ self . variants . hash ( state) ;
662+ }
631663}
0 commit comments