1use rustc_hash::FxHashMap as HashMap;
2
3use ecow::EcoString;
4
5use crate::ast::{
6 Annotation, EnumVariant, Generic, Literal, Span, StructFieldDefinition, StructKind,
7};
8use crate::types::Type;
9
10#[derive(Debug, Clone)]
11pub struct Definition {
12 pub visibility: Visibility,
13 pub ty: Type,
14 pub name: Option<EcoString>,
15 pub name_span: Option<Span>,
16 pub doc: Option<String>,
17 pub body: DefinitionBody,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub enum TypeAttribute {
23 Display,
24 ClosedDomain,
25 AnonStruct,
26 HiddenEmbed,
29 Serialized,
30}
31
32pub type Attributes = HashMap<TypeAttribute, ()>;
33
34#[derive(Debug, Clone)]
35pub enum DefinitionBody {
36 TypeAlias {
37 generics: Vec<Generic>,
38 annotation: Annotation,
39 methods: MethodSignatures,
40 attributes: Attributes,
41 },
42 Enum {
43 generics: Vec<Generic>,
44 variants: Vec<EnumVariant>,
45 methods: MethodSignatures,
46 attributes: Attributes,
47 },
48 Struct {
49 generics: Vec<Generic>,
50 fields: Vec<StructFieldDefinition>,
51 kind: StructKind,
52 methods: MethodSignatures,
53 constructor: Option<Type>,
54 attributes: Attributes,
55 },
56 Interface {
57 definition: Interface,
58 },
59 Value {
60 allowed_lints: Vec<String>,
61 go_hints: Vec<String>,
62 go_name: Option<String>,
63 go_type_param_recipe: Option<String>,
68 const_value: Option<Literal>,
72 },
73}
74
75impl DefinitionBody {
76 pub fn generics(&self) -> Option<&[Generic]> {
77 match self {
78 Self::TypeAlias { generics, .. }
79 | Self::Enum { generics, .. }
80 | Self::Struct { generics, .. } => Some(generics),
81 Self::Interface { definition } => Some(&definition.generics),
82 Self::Value { .. } => None,
83 }
84 }
85
86 pub fn generics_mut(&mut self) -> Option<&mut [Generic]> {
87 match self {
88 Self::TypeAlias { generics, .. }
89 | Self::Enum { generics, .. }
90 | Self::Struct { generics, .. } => Some(generics),
91 Self::Interface { definition } => Some(&mut definition.generics),
92 Self::Value { .. } => None,
93 }
94 }
95}
96
97impl Definition {
98 pub fn ty(&self) -> &Type {
99 &self.ty
100 }
101
102 pub fn visibility(&self) -> &Visibility {
103 &self.visibility
104 }
105
106 pub fn name_span(&self) -> Option<Span> {
107 self.name_span
108 }
109
110 pub fn doc(&self) -> Option<&String> {
111 self.doc.as_ref()
112 }
113
114 pub fn is_newtype(&self) -> bool {
119 matches!(
120 &self.body,
121 DefinitionBody::Struct {
122 kind: StructKind::Tuple,
123 fields,
124 generics,
125 ..
126 } if fields.len() == 1 && generics.is_empty()
127 )
128 }
129
130 pub fn is_pointer_backed_newtype<F>(&self, is_alias: F) -> bool
131 where
132 F: Fn(&str) -> bool,
133 {
134 self.is_newtype()
135 && matches!(
136 &self.body,
137 DefinitionBody::Struct { fields, .. }
138 if crate::types::peel_alias(&fields[0].ty, is_alias).is_ref()
139 )
140 }
141
142 pub fn allowed_lints(&self) -> &[String] {
143 match &self.body {
144 DefinitionBody::Value { allowed_lints, .. } => allowed_lints,
145 _ => &[],
146 }
147 }
148
149 pub fn go_hints(&self) -> &[String] {
150 match &self.body {
151 DefinitionBody::Value { go_hints, .. } => go_hints,
152 _ => &[],
153 }
154 }
155
156 pub fn go_name(&self) -> Option<&str> {
157 match &self.body {
158 DefinitionBody::Value { go_name, .. } => go_name.as_deref(),
159 _ => None,
160 }
161 }
162
163 pub fn go_type_param_recipe(&self) -> Option<&str> {
164 match &self.body {
165 DefinitionBody::Value {
166 go_type_param_recipe,
167 ..
168 } => go_type_param_recipe.as_deref(),
169 _ => None,
170 }
171 }
172
173 pub fn const_value(&self) -> Option<&Literal> {
174 match &self.body {
175 DefinitionBody::Value { const_value, .. } => const_value.as_ref(),
176 _ => None,
177 }
178 }
179
180 pub fn methods_mut(&mut self) -> Option<&mut MethodSignatures> {
181 match &mut self.body {
182 DefinitionBody::Struct { methods, .. } => Some(methods),
183 DefinitionBody::TypeAlias { methods, .. } => Some(methods),
184 DefinitionBody::Enum { methods, .. } => Some(methods),
185 _ => None,
186 }
187 }
188
189 pub fn attributes(&self) -> Option<&Attributes> {
190 match &self.body {
191 DefinitionBody::Struct { attributes, .. }
192 | DefinitionBody::Enum { attributes, .. }
193 | DefinitionBody::TypeAlias { attributes, .. } => Some(attributes),
194 _ => None,
195 }
196 }
197
198 pub fn is_display(&self) -> bool {
199 self.attributes()
200 .is_some_and(|a| a.contains_key(&TypeAttribute::Display))
201 }
202
203 pub fn is_closed_domain(&self) -> bool {
204 self.attributes()
205 .is_some_and(|a| a.contains_key(&TypeAttribute::ClosedDomain))
206 }
207
208 pub fn is_serialized(&self) -> bool {
209 self.attributes()
210 .is_some_and(|a| a.contains_key(&TypeAttribute::Serialized))
211 }
212
213 pub fn is_anon_struct(&self) -> bool {
214 self.attributes()
215 .is_some_and(|a| a.contains_key(&TypeAttribute::AnonStruct))
216 }
217
218 pub fn has_hidden_embed(&self) -> bool {
219 self.attributes()
220 .is_some_and(|a| a.contains_key(&TypeAttribute::HiddenEmbed))
221 }
222
223 pub fn is_type_definition(&self) -> bool {
224 matches!(
225 self.body,
226 DefinitionBody::Struct { .. }
227 | DefinitionBody::Enum { .. }
228 | DefinitionBody::TypeAlias { .. }
229 )
230 }
231
232 pub fn is_type_alias(&self) -> bool {
233 matches!(self.body, DefinitionBody::TypeAlias { .. })
234 }
235
236 pub fn is_value(&self, qualified_name: &str) -> bool {
237 matches!(self.body, DefinitionBody::Value { .. })
238 && self.ty.unwrap_forall().get_qualified_id() != Some(qualified_name)
239 }
240}
241
242pub type MethodSignatures = HashMap<EcoString, Type>;
243
244#[derive(Debug, Clone, PartialEq)]
245pub enum Visibility {
246 Public,
247 Private,
248 Local,
249}
250
251impl Visibility {
252 pub fn is_public(&self) -> bool {
253 matches!(self, Visibility::Public)
254 }
255}
256
257#[derive(Debug, Clone, PartialEq)]
258pub struct Interface {
259 pub name: EcoString,
260 pub generics: Vec<Generic>,
261 pub parents: Vec<Type>,
262 pub methods: HashMap<EcoString, Type>,
263}