aldrin_parser/ast/
type_name.rs1use super::{ArrayLen, KeyTypeName, NamedRef};
2use crate::error::{ExpectedTypeFoundConst, ExpectedTypeFoundService, TypeNotFound};
3use crate::grammar::Rule;
4use crate::validate::Validate;
5use crate::Span;
6use pest::iterators::Pair;
7
8#[derive(Debug, Clone)]
9pub struct TypeName {
10 span: Span,
11 kind: TypeNameKind,
12}
13
14impl TypeName {
15 pub(crate) fn parse(pair: Pair<Rule>) -> Self {
16 assert_eq!(pair.as_rule(), Rule::type_name);
17
18 let span = Span::from_pair(&pair);
19
20 let mut pairs = pair.into_inner();
21 let pair = pairs.next().unwrap();
22 let kind = TypeNameKind::parse(pair);
23
24 Self { span, kind }
25 }
26
27 pub(crate) fn validate(&self, validate: &mut Validate) {
28 self.kind.validate(validate);
29 }
30
31 pub fn span(&self) -> Span {
32 self.span
33 }
34
35 pub fn kind(&self) -> &TypeNameKind {
36 &self.kind
37 }
38}
39
40#[derive(Debug, Clone)]
41pub enum TypeNameKind {
42 Bool,
43 U8,
44 I8,
45 U16,
46 I16,
47 U32,
48 I32,
49 U64,
50 I64,
51 F32,
52 F64,
53 String,
54 Uuid,
55 ObjectId,
56 ServiceId,
57 Value,
58 Option(Box<TypeName>),
59 Box(Box<TypeName>),
60 Vec(Box<TypeName>),
61 Bytes,
62 Map(KeyTypeName, Box<TypeName>),
63 Set(KeyTypeName),
64 Sender(Box<TypeName>),
65 Receiver(Box<TypeName>),
66 Lifetime,
67 Unit,
68 Result(Box<TypeName>, Box<TypeName>),
69 Array(Box<TypeName>, ArrayLen),
70 Ref(NamedRef),
71}
72
73impl TypeNameKind {
74 fn parse(pair: Pair<Rule>) -> Self {
75 match pair.as_rule() {
76 Rule::kw_bool => Self::Bool,
77 Rule::kw_u8 => Self::U8,
78 Rule::kw_i8 => Self::I8,
79 Rule::kw_u16 => Self::U16,
80 Rule::kw_i16 => Self::I16,
81 Rule::kw_u32 => Self::U32,
82 Rule::kw_i32 => Self::I32,
83 Rule::kw_u64 => Self::U64,
84 Rule::kw_i64 => Self::I64,
85 Rule::kw_f32 => Self::F32,
86 Rule::kw_f64 => Self::F64,
87 Rule::kw_string => Self::String,
88 Rule::kw_uuid => Self::Uuid,
89 Rule::kw_object_id => Self::ObjectId,
90 Rule::kw_service_id => Self::ServiceId,
91 Rule::kw_value => Self::Value,
92 Rule::kw_lifetime => Self::Lifetime,
93 Rule::kw_unit => Self::Unit,
94
95 Rule::option_type => {
96 let mut pairs = pair.into_inner();
97 pairs.next().unwrap(); pairs.next().unwrap(); let pair = pairs.next().unwrap();
100
101 Self::Option(Box::new(TypeName::parse(pair)))
102 }
103
104 Rule::box_type => {
105 let mut pairs = pair.into_inner();
106 pairs.next().unwrap(); pairs.next().unwrap(); let pair = pairs.next().unwrap();
109
110 Self::Box(Box::new(TypeName::parse(pair)))
111 }
112
113 Rule::vec_type => {
114 let mut pairs = pair.into_inner();
115 pairs.next().unwrap(); pairs.next().unwrap(); let pair = pairs.next().unwrap();
118
119 Self::Vec(Box::new(TypeName::parse(pair)))
120 }
121
122 Rule::kw_bytes => Self::Bytes,
123
124 Rule::map_type => {
125 let mut pairs = pair.into_inner();
126 pairs.next().unwrap(); pairs.next().unwrap(); let key_pair = pairs.next().unwrap();
129 pairs.next().unwrap(); let type_pair = pairs.next().unwrap();
131
132 Self::Map(
133 KeyTypeName::parse(key_pair),
134 Box::new(TypeName::parse(type_pair)),
135 )
136 }
137
138 Rule::set_type => {
139 let mut pairs = pair.into_inner();
140 pairs.next().unwrap(); pairs.next().unwrap(); let pair = pairs.next().unwrap();
143
144 Self::Set(KeyTypeName::parse(pair))
145 }
146
147 Rule::sender_type => {
148 let mut pairs = pair.into_inner();
149 pairs.next().unwrap(); pairs.next().unwrap(); let pair = pairs.next().unwrap();
152
153 Self::Sender(Box::new(TypeName::parse(pair)))
154 }
155
156 Rule::receiver_type => {
157 let mut pairs = pair.into_inner();
158 pairs.next().unwrap(); pairs.next().unwrap(); let pair = pairs.next().unwrap();
161
162 Self::Receiver(Box::new(TypeName::parse(pair)))
163 }
164
165 Rule::result_type => {
166 let mut pairs = pair.into_inner();
167 pairs.next().unwrap(); pairs.next().unwrap(); let ok_pair = pairs.next().unwrap();
170 pairs.next().unwrap(); let err_pair = pairs.next().unwrap();
172
173 Self::Result(
174 Box::new(TypeName::parse(ok_pair)),
175 Box::new(TypeName::parse(err_pair)),
176 )
177 }
178
179 Rule::array_type => {
180 let mut pairs = pair.into_inner();
181 pairs.next().unwrap(); let type_pair = pairs.next().unwrap();
183 pairs.next().unwrap(); let len_pair = pairs.next().unwrap();
185
186 Self::Array(
187 Box::new(TypeName::parse(type_pair)),
188 ArrayLen::parse(len_pair),
189 )
190 }
191
192 Rule::named_ref => Self::Ref(NamedRef::parse(pair)),
193
194 _ => unreachable!(),
195 }
196 }
197
198 fn validate(&self, validate: &mut Validate) {
199 match self {
200 Self::Option(ty)
201 | Self::Box(ty)
202 | Self::Vec(ty)
203 | Self::Map(_, ty)
204 | Self::Sender(ty)
205 | Self::Receiver(ty) => ty.validate(validate),
206
207 Self::Result(ok, err) => {
208 ok.validate(validate);
209 err.validate(validate);
210 }
211
212 Self::Array(ty, len) => {
213 ty.validate(validate);
214 len.validate(validate);
215 }
216
217 Self::Ref(ty) => {
218 TypeNotFound::validate(ty, validate);
219 ExpectedTypeFoundService::validate(ty, validate);
220 ExpectedTypeFoundConst::validate(ty, validate);
221 ty.validate(validate);
222 }
223
224 Self::Bool
225 | Self::U8
226 | Self::I8
227 | Self::U16
228 | Self::I16
229 | Self::U32
230 | Self::I32
231 | Self::U64
232 | Self::I64
233 | Self::F32
234 | Self::F64
235 | Self::String
236 | Self::Uuid
237 | Self::ObjectId
238 | Self::ServiceId
239 | Self::Value
240 | Self::Bytes
241 | Self::Set(_)
242 | Self::Lifetime
243 | Self::Unit => {}
244 }
245 }
246}