Skip to main content

oak_protobuf/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use crate::ProtobufTokenType;
3
4/// Alias for ProtobufTokenType for convenience.
5pub type ProtobufToken = ProtobufTokenType;
6
7/// Represents the root of a Protobuf document.
8#[derive(Debug, Clone, PartialEq)]
9pub struct ProtobufRoot {
10    /// The definitions in the file.
11    pub definitions: Vec<Definition>,
12}
13
14/// Represents a definition in a Protobuf file.
15#[derive(Debug, Clone, PartialEq)]
16pub enum Definition {
17    /// A message definition.
18    Message(Message),
19    /// A service definition.
20    Service(Service),
21    /// An enum definition.
22    Enum(Enum),
23    /// An import statement.
24    Import(Import),
25    /// A package statement.
26    Package(Package),
27    /// A top-level option statement.
28    Option(ProtobufOption),
29    /// A syntax statement.
30    Syntax(Syntax),
31}
32
33/// A message definition.
34#[derive(Debug, Clone, PartialEq)]
35pub struct Message {
36    /// The name of the message.
37    pub name: String,
38    /// The fields within the message.
39    pub fields: Vec<Field>,
40}
41
42/// A field within a message.
43#[derive(Debug, Clone, PartialEq)]
44pub struct Field {
45    /// The type of the field.
46    pub field_type: FieldType,
47    /// The name of the field.
48    pub name: String,
49    /// The field number.
50    pub number: u32,
51    /// Options associated with the field.
52    pub options: Vec<FieldOption>,
53}
54
55/// Represents the type of a field.
56#[derive(Debug, Clone, PartialEq)]
57pub enum FieldType {
58    /// A scalar type (e.g., int32, string).
59    Scalar(ScalarType),
60    /// A reference to another message type.
61    Message(String),
62    /// A repeated field.
63    Repeated(Box<FieldType>),
64    /// An optional field (proto2).
65    Optional(Box<FieldType>),
66    /// A required field (proto2).
67    Required(Box<FieldType>),
68}
69
70/// Scalar types in Protobuf.
71#[derive(Debug, Clone, PartialEq)]
72pub enum ScalarType {
73    /// double
74    Double,
75    /// float
76    Float,
77    /// int32
78    Int32,
79    /// int64
80    Int64,
81    /// uint32
82    Uint32,
83    /// uint64
84    Uint64,
85    /// sint32
86    Sint32,
87    /// sint64
88    Sint64,
89    /// fixed32
90    Fixed32,
91    /// fixed64
92    Fixed64,
93    /// sfixed32
94    Sfixed32,
95    /// sfixed64
96    Sfixed64,
97    /// bool
98    Bool,
99    /// string
100    String,
101    /// bytes
102    Bytes,
103}
104
105/// A service definition.
106#[derive(Debug, Clone, PartialEq)]
107pub struct Service {
108    /// The name of the service.
109    pub name: String,
110    /// The methods within the service.
111    pub methods: Vec<Method>,
112}
113
114/// A method definition within a service.
115#[derive(Debug, Clone, PartialEq)]
116pub struct Method {
117    /// The name of the method.
118    pub name: String,
119    /// The input message type.
120    pub input_type: String,
121    /// The output message type.
122    pub output_type: String,
123    /// Options associated with the method.
124    pub options: Vec<MethodOption>,
125}
126
127/// An enum definition.
128#[derive(Debug, Clone, PartialEq)]
129pub struct Enum {
130    /// The name of the enum.
131    pub name: String,
132    /// The values within the enum.
133    pub values: Vec<EnumValue>,
134}
135
136/// A value within an enum.
137#[derive(Debug, Clone, PartialEq)]
138pub struct EnumValue {
139    /// The name of the enum value.
140    pub name: String,
141    /// The numeric value.
142    pub number: i32,
143    /// Options associated with the enum value.
144    pub options: Vec<EnumValueOption>,
145}
146
147/// An import statement.
148#[derive(Debug, Clone, PartialEq)]
149pub struct Import {
150    /// The path to the imported file.
151    pub path: String,
152    /// Whether the import is public.
153    pub is_public: bool,
154    /// Whether the import is weak.
155    pub is_weak: bool,
156}
157
158/// A package statement.
159#[derive(Debug, Clone, PartialEq)]
160pub struct Package {
161    /// The name of the package.
162    pub name: String,
163}
164
165/// A top-level option statement.
166#[derive(Debug, Clone, PartialEq)]
167pub struct ProtobufOption {
168    /// The name of the option.
169    pub name: String,
170    /// The value of the option.
171    pub value: OptionValue,
172}
173
174/// A syntax statement.
175#[derive(Debug, Clone, PartialEq)]
176pub struct Syntax {
177    /// The version of the syntax (e.g., "proto2", "proto3").
178    pub version: String,
179}
180
181/// Represents the value of an option.
182#[derive(Debug, Clone, PartialEq)]
183pub enum OptionValue {
184    /// A string value.
185    String(String),
186    /// A numeric value.
187    Number(f64),
188    /// A boolean value.
189    Bool(bool),
190    /// An identifier value.
191    Identifier(String),
192}
193
194/// An option associated with a field.
195#[derive(Debug, Clone, PartialEq)]
196pub struct FieldOption {
197    /// The name of the option.
198    pub name: String,
199    /// The value of the option.
200    pub value: OptionValue,
201}
202
203/// An option associated with a method.
204#[derive(Debug, Clone, PartialEq)]
205pub struct MethodOption {
206    /// The name of the option.
207    pub name: String,
208    /// The value of the option.
209    pub value: OptionValue,
210}
211
212/// An option associated with an enum value.
213#[derive(Debug, Clone, PartialEq)]
214pub struct EnumValueOption {
215    /// The name of the option.
216    pub name: String,
217    /// The value of the option.
218    pub value: OptionValue,
219}