1use super::functions::Annotation;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub enum TypeAnnotation {
8 Basic(String),
10 Array(Box<TypeAnnotation>),
12 Tuple(Vec<TypeAnnotation>),
14 Object(Vec<ObjectTypeField>),
16 Function {
18 params: Vec<FunctionParam>,
19 returns: Box<TypeAnnotation>,
20 },
21 Union(Vec<TypeAnnotation>),
23 Intersection(Vec<TypeAnnotation>),
26 Optional(Box<TypeAnnotation>),
28 Generic {
30 name: String,
31 args: Vec<TypeAnnotation>,
32 },
33 Reference(String),
35 Void,
37 Any,
39 Never,
41 Null,
43 Undefined,
45 Dyn(Vec<String>),
48}
49
50impl TypeAnnotation {
51 pub fn as_simple_name(&self) -> Option<&str> {
59 match self {
60 TypeAnnotation::Reference(name) => Some(name.as_str()),
61 TypeAnnotation::Basic(name) => Some(name.as_str()),
62 _ => None,
63 }
64 }
65
66 pub fn to_type_string(&self) -> String {
68 match self {
69 TypeAnnotation::Basic(name) | TypeAnnotation::Reference(name) => name.clone(),
70 TypeAnnotation::Array(inner) => format!("Array<{}>", inner.to_type_string()),
71 TypeAnnotation::Optional(inner) => format!("{}?", inner.to_type_string()),
72 TypeAnnotation::Generic { name, args } => {
73 let args_str: Vec<String> = args.iter().map(|a| a.to_type_string()).collect();
74 format!("{}<{}>", name, args_str.join(", "))
75 }
76 TypeAnnotation::Tuple(items) => {
77 let items_str: Vec<String> = items.iter().map(|t| t.to_type_string()).collect();
78 format!("[{}]", items_str.join(", "))
79 }
80 TypeAnnotation::Union(items) => {
81 let items_str: Vec<String> = items.iter().map(|t| t.to_type_string()).collect();
82 items_str.join(" | ")
83 }
84 TypeAnnotation::Void => "void".to_string(),
85 TypeAnnotation::Any => "any".to_string(),
86 TypeAnnotation::Never => "never".to_string(),
87 TypeAnnotation::Null => "null".to_string(),
88 TypeAnnotation::Undefined => "undefined".to_string(),
89 TypeAnnotation::Object(fields) => {
90 let fields_str: Vec<String> = fields
91 .iter()
92 .map(|f| {
93 let opt = if f.optional { "?" } else { "" };
94 let alias = f
97 .annotations
98 .iter()
99 .find(|a| a.name == "alias")
100 .and_then(|a| a.args.first())
101 .and_then(|arg| match arg {
102 super::expressions::Expr::Literal(
103 super::literals::Literal::String(s),
104 _,
105 ) => Some(s.as_str()),
106 _ => None,
107 });
108 let alias_str = alias.map(|a| format!("@\"{}\" ", a)).unwrap_or_default();
109 format!(
110 "{}{}{}: {}",
111 alias_str,
112 f.name,
113 opt,
114 f.type_annotation.to_type_string()
115 )
116 })
117 .collect();
118 format!("{{{}}}", fields_str.join(", "))
119 }
120 _ => "any".to_string(),
121 }
122 }
123}
124
125#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
126pub struct ObjectTypeField {
127 pub name: String,
128 pub optional: bool,
129 pub type_annotation: TypeAnnotation,
130 #[serde(default)]
132 pub annotations: Vec<Annotation>,
133}
134
135#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
136pub struct FunctionParam {
137 pub name: Option<String>,
138 pub optional: bool,
139 pub type_annotation: TypeAnnotation,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct TypeParam {
144 pub name: String,
145 pub default_type: Option<TypeAnnotation>,
147 #[serde(default)]
149 pub trait_bounds: Vec<String>,
150}
151
152#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
154pub struct WherePredicate {
155 pub type_name: String,
156 pub bounds: Vec<String>,
157}
158
159impl PartialEq for TypeParam {
160 fn eq(&self, other: &Self) -> bool {
161 self.name == other.name
162 && self.default_type == other.default_type
163 && self.trait_bounds == other.trait_bounds
164 }
165}
166
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct TypeAliasDef {
169 pub name: String,
170 pub type_params: Option<Vec<TypeParam>>,
171 pub type_annotation: TypeAnnotation,
172 pub meta_param_overrides: Option<std::collections::HashMap<String, super::expressions::Expr>>,
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct InterfaceDef {
178 pub name: String,
179 pub type_params: Option<Vec<TypeParam>>,
180 pub members: Vec<InterfaceMember>,
181}
182
183#[derive(Debug, Clone, Serialize, Deserialize)]
184pub enum InterfaceMember {
185 Property {
187 name: String,
188 optional: bool,
189 type_annotation: TypeAnnotation,
190 },
191 Method {
193 name: String,
194 optional: bool,
195 params: Vec<FunctionParam>,
196 return_type: TypeAnnotation,
197 is_async: bool,
199 },
200 IndexSignature {
202 param_name: String,
203 param_type: String, return_type: TypeAnnotation,
205 },
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct EnumDef {
210 pub name: String,
211 pub type_params: Option<Vec<TypeParam>>,
212 pub members: Vec<EnumMember>,
213 #[serde(default)]
215 pub annotations: Vec<super::Annotation>,
216}
217
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct EnumMember {
220 pub name: String,
221 pub kind: EnumMemberKind,
222}
223
224#[derive(Debug, Clone, Serialize, Deserialize)]
225pub enum EnumMemberKind {
226 Unit { value: Option<EnumValue> },
228 Tuple(Vec<TypeAnnotation>),
230 Struct(Vec<ObjectTypeField>),
232}
233
234#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
235pub enum EnumValue {
236 String(String),
237 Number(f64),
238}
239
240#[derive(Debug, Clone, Serialize, Deserialize)]
242pub enum TraitMember {
243 Required(InterfaceMember),
245 Default(MethodDef),
247 AssociatedType {
249 name: String,
250 bounds: Vec<TypeAnnotation>,
251 },
252}
253
254#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct AssociatedTypeBinding {
258 pub name: String,
259 pub concrete_type: TypeAnnotation,
260}
261
262#[derive(Debug, Clone, Serialize, Deserialize)]
273pub struct TraitDef {
274 pub name: String,
275 pub type_params: Option<Vec<TypeParam>>,
276 pub members: Vec<TraitMember>,
277 #[serde(default)]
279 pub annotations: Vec<super::Annotation>,
280}
281
282#[derive(Debug, Clone, Serialize, Deserialize)]
294pub struct ImplBlock {
295 pub trait_name: TypeName,
297 pub target_type: TypeName,
299 pub impl_name: Option<String>,
302 pub methods: Vec<MethodDef>,
304 pub associated_type_bindings: Vec<AssociatedTypeBinding>,
306 pub where_clause: Option<Vec<WherePredicate>>,
308}
309
310#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
312pub struct ExtendStatement {
313 pub type_name: TypeName,
315 pub methods: Vec<MethodDef>,
317}
318
319#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
320pub struct MethodDef {
321 pub name: String,
323 pub params: Vec<super::functions::FunctionParameter>,
325 pub when_clause: Option<Box<super::expressions::Expr>>,
327 pub return_type: Option<TypeAnnotation>,
329 pub body: Vec<super::statements::Statement>,
331 pub is_async: bool,
333}
334
335#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
336pub enum TypeName {
337 Simple(String),
339 Generic {
341 name: String,
342 type_args: Vec<TypeAnnotation>,
343 },
344}
345
346#[derive(Debug, Clone, Serialize, Deserialize)]
357pub struct StructTypeDef {
358 pub name: String,
359 pub type_params: Option<Vec<TypeParam>>,
360 pub fields: Vec<StructField>,
361 pub annotations: Vec<Annotation>,
363 #[serde(default)]
365 pub native_layout: Option<NativeLayoutBinding>,
366}
367
368#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
370pub struct NativeLayoutBinding {
371 pub abi: String,
373}
374
375#[derive(Debug, Clone, Serialize, Deserialize)]
389pub struct StructField {
390 pub annotations: Vec<Annotation>,
391 pub is_comptime: bool,
392 pub name: String,
393 pub type_annotation: TypeAnnotation,
394 pub default_value: Option<super::expressions::Expr>,
395}