1use serde::Deserialize;
22
23#[derive(Debug, Clone, Deserialize)]
27pub struct Program {
28 pub body: Vec<Statement>,
29 #[serde(default)]
30 pub errors: Vec<ParseError>,
31}
32
33#[derive(Debug, Clone, Deserialize)]
35pub struct ParseError {
36 pub message: String,
37 pub loc: Option<SourceLocation>,
38}
39
40#[derive(Debug, Clone, Deserialize)]
42pub struct SourceLocation {
43 pub start: Position,
44 pub end: Position,
45}
46
47#[derive(Debug, Clone, Deserialize)]
49pub struct Position {
50 pub line: u32,
51 pub column: u32,
52}
53
54#[derive(Debug, Clone, Deserialize)]
58#[serde(tag = "type")]
59pub enum Statement {
60 ExportNamedDeclaration {
61 declaration: Option<Declaration>,
62 },
63 DeclareExportDeclaration {
64 declaration: Option<Declaration>,
65 },
66 #[serde(other)]
68 Other,
69}
70
71#[derive(Debug, Clone, Deserialize)]
75#[serde(tag = "type")]
76pub enum Declaration {
77 TypeAlias {
78 id: Identifier,
79 right: TypeAnnotation,
80 },
81 OpaqueType {
82 id: Identifier,
83 supertype: Option<TypeAnnotation>,
84 },
85 DeclareOpaqueType {
86 id: Identifier,
87 supertype: Option<TypeAnnotation>,
88 },
89 #[serde(other)]
90 Other,
91}
92
93#[derive(Debug, Clone, Deserialize)]
97#[serde(tag = "type")]
98pub enum TypeAnnotation {
99 StringTypeAnnotation,
101 NumberTypeAnnotation,
102 BooleanTypeAnnotation,
103 VoidTypeAnnotation,
104 MixedTypeAnnotation,
105 AnyTypeAnnotation,
106 EmptyTypeAnnotation,
107 NullLiteralTypeAnnotation,
108 BigIntTypeAnnotation,
109 SymbolTypeAnnotation,
110
111 StringLiteralTypeAnnotation {
113 value: String,
114 },
115 NumberLiteralTypeAnnotation {
116 value: f64,
117 },
118 BooleanLiteralTypeAnnotation {
119 value: bool,
120 },
121
122 NullableTypeAnnotation {
124 #[serde(rename = "typeAnnotation")]
125 type_annotation: Box<TypeAnnotation>,
126 },
127
128 ObjectTypeAnnotation {
130 #[serde(default)]
131 properties: Vec<ObjectMember>,
132 #[serde(default)]
133 indexers: Vec<ObjectTypeIndexer>,
134 #[serde(default)]
135 exact: bool,
136 },
137
138 UnionTypeAnnotation {
140 types: Vec<TypeAnnotation>,
141 },
142
143 IntersectionTypeAnnotation {
145 types: Vec<TypeAnnotation>,
146 },
147
148 GenericTypeAnnotation {
150 id: Identifier,
151 #[serde(rename = "typeParameters")]
152 type_parameters: Option<TypeParameterInstantiation>,
153 },
154
155 TupleTypeAnnotation {
157 #[serde(alias = "types", default)]
158 #[serde(rename = "elementTypes")]
159 element_types: Vec<TypeAnnotation>,
160 },
161
162 ArrayTypeAnnotation {
164 #[serde(rename = "elementType")]
165 element_type: Box<TypeAnnotation>,
166 },
167
168 TypeofTypeAnnotation {
170 argument: Box<TypeAnnotation>,
171 },
172
173 #[serde(other)]
174 Other,
175}
176
177impl TypeAnnotation {
178 pub fn type_name(&self) -> &'static str {
180 match self {
181 Self::StringTypeAnnotation => "StringTypeAnnotation",
182 Self::NumberTypeAnnotation => "NumberTypeAnnotation",
183 Self::BooleanTypeAnnotation => "BooleanTypeAnnotation",
184 Self::VoidTypeAnnotation => "VoidTypeAnnotation",
185 Self::MixedTypeAnnotation => "MixedTypeAnnotation",
186 Self::AnyTypeAnnotation => "AnyTypeAnnotation",
187 Self::EmptyTypeAnnotation => "EmptyTypeAnnotation",
188 Self::NullLiteralTypeAnnotation => "NullLiteralTypeAnnotation",
189 Self::BigIntTypeAnnotation => "BigIntTypeAnnotation",
190 Self::SymbolTypeAnnotation => "SymbolTypeAnnotation",
191 Self::StringLiteralTypeAnnotation { .. } => "StringLiteralTypeAnnotation",
192 Self::NumberLiteralTypeAnnotation { .. } => "NumberLiteralTypeAnnotation",
193 Self::BooleanLiteralTypeAnnotation { .. } => "BooleanLiteralTypeAnnotation",
194 Self::NullableTypeAnnotation { .. } => "NullableTypeAnnotation",
195 Self::ObjectTypeAnnotation { .. } => "ObjectTypeAnnotation",
196 Self::UnionTypeAnnotation { .. } => "UnionTypeAnnotation",
197 Self::IntersectionTypeAnnotation { .. } => "IntersectionTypeAnnotation",
198 Self::GenericTypeAnnotation { .. } => "GenericTypeAnnotation",
199 Self::TupleTypeAnnotation { .. } => "TupleTypeAnnotation",
200 Self::ArrayTypeAnnotation { .. } => "ArrayTypeAnnotation",
201 Self::TypeofTypeAnnotation { .. } => "TypeofTypeAnnotation",
202 Self::Other => "Other",
203 }
204 }
205}
206
207#[derive(Debug, Clone, Deserialize)]
211#[serde(tag = "type")]
212pub enum ObjectMember {
213 ObjectTypeProperty {
214 key: PropertyKey,
215 value: TypeAnnotation,
216 variance: Option<Variance>,
217 #[serde(default)]
218 optional: bool,
219 },
220 ObjectTypeSpreadProperty {
221 argument: TypeAnnotation,
222 },
223 #[serde(other)]
224 Other,
225}
226
227#[derive(Debug, Clone, Deserialize)]
229pub struct ObjectTypeIndexer {
230 pub key: TypeAnnotation,
231 pub value: TypeAnnotation,
232}
233
234#[derive(Debug, Clone, Deserialize)]
238pub struct Identifier {
239 pub name: String,
240}
241
242#[derive(Debug, Clone, Deserialize)]
244#[serde(tag = "type")]
245pub enum PropertyKey {
246 Identifier {
247 name: String,
248 },
249 Literal {
251 value: serde_json::Value,
252 },
253 #[serde(other)]
254 Other,
255}
256
257impl PropertyKey {
258 pub fn name(&self) -> Option<&str> {
260 match self {
261 Self::Identifier { name } => Some(name),
262 Self::Literal { value } => value.as_str(),
263 Self::Other => None,
264 }
265 }
266
267 pub fn is_quoted(&self) -> bool {
269 matches!(self, Self::Literal { .. })
270 }
271}
272
273#[derive(Debug, Clone, Deserialize)]
275pub struct Variance {
276 pub kind: VarianceKind,
277}
278
279#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
281#[serde(rename_all = "lowercase")]
282pub enum VarianceKind {
283 Plus,
284 Minus,
285}
286
287#[derive(Debug, Clone, Deserialize)]
289pub struct TypeParameterInstantiation {
290 pub params: Vec<TypeAnnotation>,
291}