1
+ use crate :: codegen:: imports:: Visibility ;
2
+ use crate :: parser:: { Prefix , TagKind } ;
1
3
use std:: fmt;
2
4
3
5
pub struct Struct {
6
+ visibility : Visibility ,
4
7
name : String ,
5
8
fields : Vec < Field > ,
6
9
attributes : Vec < Attribute > ,
7
10
}
8
11
9
12
impl Struct {
10
- pub fn new < I : Into < String > > ( name : I ) -> Self {
13
+ pub fn new < I : Into < String > > ( visibility : Visibility , name : I ) -> Self {
11
14
Self {
15
+ visibility,
12
16
name : name. into ( ) ,
13
17
fields : Vec :: new ( ) ,
14
18
attributes : vec ! [ Attribute :: Derive ( vec![
15
- Derive :: Serialize ,
16
- Derive :: Deserialize ,
19
+ Derive :: AsnType ,
20
+ Derive :: Encode ,
21
+ Derive :: Decode ,
22
+ Derive :: PartialEq ,
23
+ Derive :: PartialOrd ,
24
+ Derive :: Eq ,
25
+ Derive :: Ord ,
26
+ Derive :: Debug ,
17
27
] ) ] ,
18
28
}
19
29
}
20
30
21
31
pub fn add_field ( & mut self , field : Field ) {
22
32
self . fields . push ( field) ;
23
33
}
34
+
35
+ pub fn add_rasn_attributes ( mut self , attributes : Vec < Rasn > ) -> Self {
36
+ self . attributes . push ( Attribute :: Rasn ( attributes) ) ;
37
+ self
38
+ }
24
39
}
25
40
26
41
impl fmt:: Display for Struct {
@@ -30,7 +45,7 @@ impl fmt::Display for Struct {
30
45
writeln ! ( f) ?;
31
46
}
32
47
33
- writeln ! ( f, "struct {} {{" , self . name) ?;
48
+ writeln ! ( f, "{} struct {} {{" , self . visibility , self . name) ?;
34
49
35
50
if !self . fields . is_empty ( ) {
36
51
itertools:: join ( self . fields . iter ( ) . map ( ToString :: to_string) , "\n " ) . fmt ( f) ?;
@@ -42,6 +57,7 @@ impl fmt::Display for Struct {
42
57
}
43
58
44
59
pub struct Field {
60
+ visibility : Visibility ,
45
61
attributes : Vec < Attribute > ,
46
62
name : String ,
47
63
optional : bool ,
@@ -52,7 +68,13 @@ pub struct Field {
52
68
impl fmt:: Display for Field {
53
69
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
54
70
if !self . attributes . is_empty ( ) {
55
- itertools:: join ( self . attributes . iter ( ) . map ( ToString :: to_string) , "\n " ) . fmt ( f) ?;
71
+ itertools:: join (
72
+ self . attributes
73
+ . iter ( )
74
+ . map ( |x| format ! ( "\t {}" , x. to_string( ) ) ) ,
75
+ "\n " ,
76
+ )
77
+ . fmt ( f) ?;
56
78
writeln ! ( f) ?;
57
79
}
58
80
@@ -62,16 +84,18 @@ impl fmt::Display for Field {
62
84
self . ty . clone ( )
63
85
} ;
64
86
65
- write ! ( f, "{} : {}," , self . name, ty)
87
+ write ! ( f, "\t {}{} : {}," , self . visibility , self . name, ty)
66
88
}
67
89
}
68
90
69
91
#[ derive( Default ) ]
70
92
pub struct FieldBuilder {
93
+ visibility : Visibility ,
71
94
name : String ,
72
95
ty : String ,
73
96
optional : bool ,
74
97
default_value : Option < String > ,
98
+ attributes : Vec < Attribute > ,
75
99
}
76
100
77
101
impl FieldBuilder {
@@ -80,12 +104,18 @@ impl FieldBuilder {
80
104
let ty = ty. into ( ) ;
81
105
82
106
Self {
107
+ visibility : Visibility :: Private ,
83
108
name,
84
109
ty,
85
110
..Self :: default ( )
86
111
}
87
112
}
88
113
114
+ pub fn visibility ( mut self , visibility : Visibility ) -> Self {
115
+ self . visibility = visibility;
116
+ self
117
+ }
118
+
89
119
pub fn optional ( mut self , optional : bool ) -> Self {
90
120
self . optional = optional;
91
121
self
@@ -96,15 +126,20 @@ impl FieldBuilder {
96
126
self
97
127
}
98
128
99
- pub fn build ( self ) -> Field {
100
- let mut attributes = Vec :: new ( ) ;
129
+ pub fn add_rasn_attribute ( mut self , attributes : Vec < Rasn > ) -> Self {
130
+ self . attributes . push ( Attribute :: Rasn ( attributes) ) ;
131
+ self
132
+ }
101
133
134
+ pub fn build ( mut self ) -> Field {
102
135
if let Some ( default_value) = self . default_value {
103
- attributes. push ( Attribute :: Serde ( Serde :: Default ( default_value) ) ) ;
136
+ self . attributes
137
+ . push ( Attribute :: Serde ( Serde :: Default ( default_value) ) ) ;
104
138
}
105
139
106
140
Field {
107
- attributes,
141
+ visibility : self . visibility ,
142
+ attributes : self . attributes ,
108
143
name : self . name ,
109
144
optional : self . optional ,
110
145
ty : self . ty ,
@@ -115,6 +150,7 @@ impl FieldBuilder {
115
150
pub enum Attribute {
116
151
Serde ( Serde ) ,
117
152
Derive ( Vec < Derive > ) ,
153
+ Rasn ( Vec < Rasn > ) ,
118
154
}
119
155
120
156
impl fmt:: Display for Attribute {
@@ -127,6 +163,10 @@ impl fmt::Display for Attribute {
127
163
"#[derive({})]" ,
128
164
itertools:: join( defaults. iter( ) . map( ToString :: to_string) , ", " )
129
165
) ,
166
+ Attribute :: Rasn ( defaults) => format ! (
167
+ "#[rasn({})]" ,
168
+ itertools:: join( defaults. iter( ) . map( ToString :: to_string) , ", " )
169
+ ) ,
130
170
} ;
131
171
132
172
attribute. fmt ( f)
@@ -138,17 +178,70 @@ pub enum Serde {
138
178
}
139
179
140
180
pub enum Derive {
141
- Serialize ,
142
- Deserialize ,
181
+ AsnType ,
182
+ Encode ,
183
+ Decode ,
184
+ PartialEq ,
185
+ PartialOrd ,
186
+ Eq ,
187
+ Ord ,
188
+ Debug ,
143
189
}
144
190
145
191
impl fmt:: Display for Derive {
146
192
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
147
193
let derive = match self {
148
- Derive :: Serialize => "Serialize" ,
149
- Derive :: Deserialize => "Deserialize" ,
194
+ Derive :: AsnType => "AsnType" ,
195
+ Derive :: Encode => "Encode" ,
196
+ Derive :: Decode => "Decode" ,
197
+ Derive :: PartialEq => "PartialEq" ,
198
+ Derive :: PartialOrd => "PartialOrd" ,
199
+ Derive :: Eq => "Eq" ,
200
+ Derive :: Ord => "Ord" ,
201
+ Derive :: Debug => "Debug" ,
150
202
} ;
151
203
152
204
derive. fmt ( f)
153
205
}
154
206
}
207
+
208
+ pub enum Rasn {
209
+ Type ( & ' static str ) ,
210
+ Prefix ( Prefix ) ,
211
+ }
212
+
213
+ impl fmt:: Display for Rasn {
214
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
215
+ let rasn = match self {
216
+ Rasn :: Type ( type_name) => type_name. to_string ( ) ,
217
+ Rasn :: Prefix ( prefix) => match prefix. kind {
218
+ TagKind :: Explicit => format ! (
219
+ "tag(explicit({class}{number}))" ,
220
+ class = prefix
221
+ . class
222
+ . map( |x| x. to_string( ) . to_lowercase( ) + ", " )
223
+ . unwrap_or( String :: from( "" ) ) ,
224
+ number = prefix. number
225
+ ) ,
226
+ TagKind :: Implicit => format ! (
227
+ "tag(implicit({class}{number}))" ,
228
+ class = prefix
229
+ . class
230
+ . map( |x| x. to_string( ) . to_lowercase( ) + ", " )
231
+ . unwrap_or( String :: from( "" ) ) ,
232
+ number = prefix. number
233
+ ) ,
234
+ _ => format ! (
235
+ "tag({class}{number})" ,
236
+ class = prefix
237
+ . class
238
+ . map( |x| x. to_string( ) . to_lowercase( ) + ", " )
239
+ . unwrap_or( String :: from( "" ) ) ,
240
+ number = prefix. number
241
+ ) ,
242
+ } ,
243
+ } ;
244
+
245
+ rasn. fmt ( f)
246
+ }
247
+ }
0 commit comments