rico 0.1.7

A high-performance Apache Thrift IDL parser that converts Thrift IDL files to JSON AST
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use serde::{Deserialize, Serialize};

use super::types::{Common, NodeType, LOC};

/// Represents a comment in the Thrift IDL.
///
/// Comments can be either single-line (//) or multi-line (/* */),
/// and are preserved in the AST for documentation purposes.
#[derive(Serialize, Deserialize, Debug)]
pub struct Comment {
    /// The type of the node (CommentLine or CommentBlock)
    pub kind: NodeType,
    /// The text content of the comment
    pub value: String,
    /// The location of the comment in the source code
    pub loc: LOC,
}

/// Represents a single annotation in the Thrift IDL.
///
/// Annotations provide metadata for Thrift definitions and can be used
/// to customize code generation or add runtime behavior.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always Annotation)
#[serde(tag = "kind", rename = "Annotation")]
pub struct Annotation {
    /// The value of the annotation
    pub value: Common<String>,
    /// The location of the annotation in the source code
    pub loc: LOC,
    /// The name of the annotation
    pub name: Common<String>,
}

/// A collection of annotations attached to a Thrift definition.
///
/// Multiple annotations can be specified in parentheses after a definition.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always Annotations)
#[serde(tag = "kind", rename = "Annotations")]
pub struct Annotations {
    /// The location of the annotations block in the source code
    pub loc: LOC,
    /// The list of individual annotations
    pub members: Vec<Annotation>,
}

/// Represents a collection type (list or set) in a field definition.
///
/// Collection types can hold multiple values of the same type.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always ListType)
#[serde(tag = "kind", rename = "ListType")]
pub struct FieldListType {
    /// The location in the source code
    pub loc: LOC,
    /// The type name ("list")
    pub value: String,
    /// The type of elements in the collection
    #[serde(rename = "valueType")]
    pub value_type: Box<FieldType>,
}

/// Represents a collection type (list or set) in a field definition.
///
/// Collection types can hold multiple values of the same type.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always SetType)
#[serde(tag = "kind", rename = "SetType")]
pub struct FieldSetType {
    /// The location in the source code
    pub loc: LOC,
    /// The type name ("set")
    pub value: String,
    /// The type of elements in the collection
    #[serde(rename = "valueType")]
    pub value_type: Box<FieldType>,
}

/// Represents a map type in a field definition.
///
/// Map types associate keys with values, where both key and value
/// types can be specified.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always MapType)
#[serde(tag = "kind", rename = "MapType")]
pub struct FieldMapType {
    /// The location in the source code
    pub loc: LOC,
    /// The type name ("map")
    pub value: String,
    /// The type of values in the map
    #[serde(rename = "valueType")]
    pub value_type: Box<FieldType>,
    /// The type of keys in the map
    #[serde(rename = "keyType")]
    pub key_type: Box<FieldType>,
}

/// Represents a list of constant values.
///
/// Used for list literals in constant definitions and default values.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always ConstList)
#[serde(tag = "kind", rename = "ConstList")]
pub struct ConstList {
    /// The location in the source code
    pub loc: LOC,
    /// The list elements
    pub elements: Vec<FieldInitialValue>,
}

/// Represents a key-value pair in a map constant.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always PropertyAssignment)
#[serde(tag = "kind", rename = "PropertyAssignment")]
pub struct MapProperty {
    /// The location in the source code
    pub loc: LOC,
    /// The property value
    pub value: FieldInitialValue,
    /// The property key
    pub name: FieldInitialValue,
}

/// Represents a map of constant values.
///
/// Used for map literals in constant definitions and default values.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always ConstMap)
#[serde(tag = "kind", rename = "ConstMap")]
pub struct ConstMap {
    /// The location in the source code
    pub loc: LOC,
    /// The map entries
    pub properties: Vec<MapProperty>,
}

/// Represents an initial value for a field or constant.
///
/// Can be a simple value, a list, or a map.
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
/// enum will deserialize by order,so we should put the most complex type first
pub enum FieldInitialValue {
    /// A map of key-value pairs
    ConstMap(ConstMap),
    /// A list of values
    ConstList(ConstList),
    /// A simple constant value
    ConstValue(Common<String>),
}

/// Represents a field type in the Thrift IDL.
///
/// Field types can be:
/// - Base types (i32, string, etc.)
/// - Collections (list, set)
/// - Maps
/// - User-defined types
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum FieldType {
    /// A map type
    MapType(FieldMapType),
    /// A list type
    ListType(FieldListType),
    /// A set type
    SetType(FieldSetType),
    /// A base type or user-defined type
    CommonType(Common<String>),
}

/// Represents an initializer for an enum value.
///
/// Enum values can optionally be assigned explicit integer values.
#[derive(Serialize, Deserialize, Debug)]
pub struct Initializer {
    pub kind: NodeType,
    /// The explicit value assigned to the enum member
    pub value: Common<String>,
    /// The location in the source code
    pub loc: LOC,
}

/// Represents a member of an enum definition.
///
/// Each enum member can have an optional explicit value and annotations.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always EnumMember)
#[serde(tag = "kind", rename = "EnumMember")]
pub struct EnumMember {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the enum member
    pub name: Common<String>,
    /// Optional explicit value assignment
    pub initializer: Option<Initializer>,
    /// Associated comments
    pub comments: Vec<Comment>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
}

/// Represents a field in a struct, union, exception, or function parameter.
///
/// Fields have an optional field ID, type, and various modifiers.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always FieldDefinition)
#[serde(tag = "kind", rename = "FieldDefinition")]
pub struct Field {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the field
    pub name: Common<String>,
    /// Optional field ID (1, 2, etc.)
    #[serde(rename = "fieldID")]
    pub field_id: Option<Common<String>>,
    /// The type of the field
    #[serde(rename = "fieldType")]
    pub field_type: FieldType,
    /// Required/Optional/Default modifier
    #[serde(rename = "requiredType")]
    pub required_type: String,
    /// Optional default value
    #[serde(rename = "defaultValue")]
    pub default_value: Option<FieldInitialValue>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
    /// Associated comments
    pub comments: Vec<Comment>,
}

/// Represents a function definition in a service.
///
/// Functions define the methods that can be called on a service.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always FunctionDefinition)
#[serde(tag = "kind", rename = "FunctionDefinition")]
pub struct Function {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the function
    pub name: Common<String>,
    /// The return type of the function
    #[serde(rename = "returnType")]
    pub return_type: FieldType,
    /// The function parameters
    pub params: Vec<Field>,
    /// Optional exceptions that can be thrown
    pub throws: Option<Vec<Field>>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
    /// Associated comments
    pub comments: Vec<Comment>,
    /// Whether the function is oneway
    pub oneway: bool,
}

/// Represents a namespace declaration in the Thrift IDL.
///
/// Namespaces specify the package/module name for generated code in
/// different target languages.
#[derive(Serialize, Deserialize, Debug)]
pub struct Namespace {
    /// The location of the namespace declaration in the source code
    pub loc: LOC,
    /// The namespace identifier
    pub name: Common<String>,
    /// The target language scope (e.g., "py", "java", "rs")
    pub scope: Common<String>,
    /// Associated comments
    pub comments: Vec<Comment>,
}

/// Represents an include statement in the Thrift IDL.
///
/// Include statements allow splitting Thrift definitions across multiple
/// files for better organization.
#[derive(Serialize, Deserialize, Debug)]
pub struct Include {
    /// The location of the include statement in the source code
    pub loc: LOC,
    /// The name/path of the included file
    pub name: Common<String>,
    /// Associated comments
    pub comments: Vec<Comment>,
}

/// Represents a constant definition in the Thrift IDL.
///
/// Constants can be used to define shared values of any type.
#[derive(Serialize, Deserialize, Debug)]
pub struct Const {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the constant
    pub name: Common<String>,
    /// The value of the constant
    pub value: FieldInitialValue,
    /// The type of the constant
    #[serde(rename = "fieldType")]
    pub field_type: FieldType,
    /// Associated comments
    pub comments: Vec<Comment>,
}

/// Represents a typedef definition in the Thrift IDL.
///
/// Typedefs create aliases for existing types, which can be used
/// to provide more meaningful names or documentation.
#[derive(Serialize, Deserialize, Debug)]
pub struct Typedef {
    /// The location in the source code
    pub loc: LOC,
    /// The new type name
    pub name: Common<String>,
    /// The original type being aliased
    #[serde(rename = "fieldType")]
    pub field_type: FieldType,
    /// Associated comments
    pub comments: Vec<Comment>,
}

/// Represents an enum definition in the Thrift IDL.
///
/// Enums define a set of named constants that can be used as field types.
#[derive(Serialize, Deserialize, Debug)]
pub struct Enum {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the enum
    pub name: Common<String>,
    /// The enum members
    pub members: Vec<EnumMember>,
    /// Associated comments
    pub comments: Vec<Comment>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
}

/// Represents an exception definition in the Thrift IDL.
///
/// Exceptions are similar to structs but are used for error handling
/// in service methods.
#[derive(Serialize, Deserialize, Debug)]
pub struct Exception {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the exception
    pub name: Common<String>,
    /// The fields of the exception
    pub members: Vec<Field>,
    /// Associated comments
    pub comments: Vec<Comment>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
}

/// Represents a struct definition in the Thrift IDL.
///
/// Structs are the primary way to define complex data types in Thrift.
#[derive(Serialize, Deserialize, Debug)]
pub struct Struct {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the struct
    pub name: Common<String>,
    /// The fields of the struct
    pub members: Vec<Field>,
    /// Associated comments
    pub comments: Vec<Comment>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
}

/// Represents a union definition in the Thrift IDL.
///
/// Unions are similar to structs but only one field can be set at a time.
#[derive(Serialize, Deserialize, Debug)]
pub struct Union {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the union
    pub name: Common<String>,
    /// The fields of the union
    pub members: Vec<Field>,
    /// Associated comments
    pub comments: Vec<Comment>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
}

/// Represents a service definition in the Thrift IDL.
///
/// Services define interfaces that can be implemented by servers
/// and called by clients.
#[derive(Serialize, Deserialize, Debug)]
pub struct Service {
    /// The location in the source code
    pub loc: LOC,
    /// The name of the service
    pub name: Common<String>,
    /// Optional parent service (for inheritance)
    pub extends: Option<Common<String>>,
    /// The functions defined in the service
    pub members: Vec<Function>,
    /// Associated comments
    pub comments: Vec<Comment>,
    /// Optional annotations
    pub annotations: Option<Annotations>,
}

/// Represents a complete Thrift IDL document.
///
/// A document is the root node of the AST and contains all the
/// top-level definitions.
#[derive(Serialize, Deserialize, Debug)]
/// The type of the node (always ThriftDocument)
#[serde(tag = "kind", rename = "ThriftDocument")]
pub struct Document {
    /// The list of top-level definitions
    pub members: Vec<DocumentMembers>,
}

/// Represents a top-level definition in a Thrift document.
///
/// Each member can be one of several types of definitions that are
/// allowed at the document level.
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
pub enum DocumentMembers {
    /// A namespace declaration
    #[serde(rename = "NamespaceDefinition")]
    Namespace(Namespace),
    /// An include statement
    #[serde(rename = "IncludeDefinition")]
    Include(Include),
    /// A constant definition
    #[serde(rename = "ConstDefinition")]
    Const(Const),
    /// A typedef definition
    #[serde(rename = "TypedefDefinition")]
    Typedef(Typedef),
    /// An enum definition
    #[serde(rename = "EnumDefinition")]
    Enum(Enum),
    /// A struct definition
    #[serde(rename = "StructDefinition")]
    Struct(Struct),
    /// A service definition
    #[serde(rename = "ServiceDefinition")]
    Service(Service),
    /// An exception definition
    #[serde(rename = "ExceptionDefinition")]
    Exception(Exception),
    /// A union definition
    #[serde(rename = "UnionDefinition")]
    Union(Union),
}