1
1
use valuable:: field:: * ;
2
2
use valuable:: * ;
3
3
4
+ #[ test]
5
+ fn test_manual_static_impl ( ) {
6
+ struct MyStruct {
7
+ num : u32 ,
8
+ list : Vec < String > ,
9
+ sub : SubStruct ,
10
+ }
11
+
12
+ static MY_STRUCT_FIELDS : & [ NamedField < ' static > ] = & [
13
+ NamedField :: new ( "num" ) ,
14
+ NamedField :: new ( "list" ) ,
15
+ NamedField :: new ( "sub" ) ,
16
+ ] ;
17
+
18
+ struct SubStruct {
19
+ message : & ' static str ,
20
+ }
21
+
22
+ static SUB_STRUCT_FIELDS : & [ NamedField < ' static > ] = & [ NamedField :: new ( "message" ) ] ;
23
+
24
+ impl Valuable for MyStruct {
25
+ fn as_value ( & self ) -> Value < ' _ > {
26
+ Value :: Structable ( self )
27
+ }
28
+
29
+ fn visit ( & self , visit : & mut dyn Visit ) {
30
+ visit. visit_named_fields ( & NamedValues :: new (
31
+ MY_STRUCT_FIELDS ,
32
+ & [
33
+ Value :: U32 ( self . num ) ,
34
+ Value :: Listable ( & self . list ) ,
35
+ Value :: Structable ( & self . sub ) ,
36
+ ] ,
37
+ ) ) ;
38
+ }
39
+ }
40
+
41
+ impl Structable for MyStruct {
42
+ fn definition ( & self ) -> StructDef < ' _ > {
43
+ StructDef :: new ( "MyStruct" , Fields :: NamedStatic ( MY_STRUCT_FIELDS ) , false )
44
+ }
45
+ }
46
+
47
+ impl Valuable for SubStruct {
48
+ fn as_value ( & self ) -> Value < ' _ > {
49
+ Value :: Structable ( self )
50
+ }
51
+
52
+ fn visit ( & self , visit : & mut dyn Visit ) {
53
+ visit. visit_named_fields ( & NamedValues :: new (
54
+ SUB_STRUCT_FIELDS ,
55
+ & [ Value :: String ( self . message ) ] ,
56
+ ) ) ;
57
+ }
58
+ }
59
+
60
+ impl Structable for SubStruct {
61
+ fn definition ( & self ) -> StructDef < ' _ > {
62
+ StructDef :: new ( "SubStruct" , Fields :: NamedStatic ( SUB_STRUCT_FIELDS ) , false )
63
+ }
64
+ }
65
+
66
+ let my_struct = MyStruct {
67
+ num : 12 ,
68
+ list : vec ! [ "hello" . to_string( ) ] ,
69
+ sub : SubStruct { message : "world" } ,
70
+ } ;
71
+
72
+ assert_eq ! (
73
+ format!( "{:?}" , my_struct. as_value( ) ) ,
74
+ "MyStruct { num: 12, list: [\" hello\" ], sub: SubStruct { message: \" world\" } }"
75
+ ) ;
76
+ }
77
+
4
78
#[ test]
5
79
fn test_named_field ( ) {
6
80
let name = "hello" . to_string ( ) ;
@@ -42,11 +116,7 @@ fn test_fields_unnamed() {
42
116
43
117
#[ test]
44
118
fn test_struct_def ( ) {
45
- let def = StructDef :: new (
46
- "hello" ,
47
- Fields :: Unnamed ,
48
- false ,
49
- ) ;
119
+ let def = StructDef :: new ( "hello" , Fields :: Unnamed , false ) ;
50
120
51
121
assert_eq ! ( def. name( ) , "hello" ) ;
52
122
assert ! ( matches!( def. fields( ) , Fields :: Unnamed ) ) ;
@@ -55,18 +125,9 @@ fn test_struct_def() {
55
125
56
126
#[ test]
57
127
fn test_named_values ( ) {
58
- let fields = [
59
- NamedField :: new ( "foo" ) ,
60
- NamedField :: new ( "bar" ) ,
61
- ] ;
128
+ let fields = [ NamedField :: new ( "foo" ) , NamedField :: new ( "bar" ) ] ;
62
129
63
- let vals = NamedValues :: new (
64
- & fields[ ..] ,
65
- & [
66
- Value :: U32 ( 123 ) ,
67
- Value :: String ( "hello" ) ,
68
- ]
69
- ) ;
130
+ let vals = NamedValues :: new ( & fields[ ..] , & [ Value :: U32 ( 123 ) , Value :: String ( "hello" ) ] ) ;
70
131
71
132
let other_field = NamedField :: new ( "foo" ) ;
72
133
0 commit comments