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}
30
31pub type Attributes = HashMap<TypeAttribute, ()>;
32
33#[derive(Debug, Clone)]
34pub enum DefinitionBody {
35 TypeAlias {
36 generics: Vec<Generic>,
37 annotation: Annotation,
38 methods: MethodSignatures,
39 attributes: Attributes,
40 },
41 Enum {
42 generics: Vec<Generic>,
43 variants: Vec<EnumVariant>,
44 methods: MethodSignatures,
45 attributes: Attributes,
46 },
47 Struct {
48 generics: Vec<Generic>,
49 fields: Vec<StructFieldDefinition>,
50 kind: StructKind,
51 methods: MethodSignatures,
52 constructor: Option<Type>,
53 attributes: Attributes,
54 },
55 Interface {
56 definition: Interface,
57 },
58 Value {
59 allowed_lints: Vec<String>,
60 go_hints: Vec<String>,
61 go_name: Option<String>,
62 go_type_param_recipe: Option<String>,
67 const_value: Option<Literal>,
71 },
72}
73
74impl Definition {
75 pub fn ty(&self) -> &Type {
76 &self.ty
77 }
78
79 pub fn visibility(&self) -> &Visibility {
80 &self.visibility
81 }
82
83 pub fn name_span(&self) -> Option<Span> {
84 self.name_span
85 }
86
87 pub fn doc(&self) -> Option<&String> {
88 self.doc.as_ref()
89 }
90
91 pub fn is_newtype(&self) -> bool {
96 matches!(
97 &self.body,
98 DefinitionBody::Struct {
99 kind: StructKind::Tuple,
100 fields,
101 generics,
102 ..
103 } if fields.len() == 1 && generics.is_empty()
104 )
105 }
106
107 pub fn is_pointer_backed_newtype<F>(&self, is_alias: F) -> bool
108 where
109 F: Fn(&str) -> bool,
110 {
111 self.is_newtype()
112 && matches!(
113 &self.body,
114 DefinitionBody::Struct { fields, .. }
115 if crate::types::peel_alias(&fields[0].ty, is_alias).is_ref()
116 )
117 }
118
119 pub fn allowed_lints(&self) -> &[String] {
120 match &self.body {
121 DefinitionBody::Value { allowed_lints, .. } => allowed_lints,
122 _ => &[],
123 }
124 }
125
126 pub fn go_hints(&self) -> &[String] {
127 match &self.body {
128 DefinitionBody::Value { go_hints, .. } => go_hints,
129 _ => &[],
130 }
131 }
132
133 pub fn go_name(&self) -> Option<&str> {
134 match &self.body {
135 DefinitionBody::Value { go_name, .. } => go_name.as_deref(),
136 _ => None,
137 }
138 }
139
140 pub fn go_type_param_recipe(&self) -> Option<&str> {
141 match &self.body {
142 DefinitionBody::Value {
143 go_type_param_recipe,
144 ..
145 } => go_type_param_recipe.as_deref(),
146 _ => None,
147 }
148 }
149
150 pub fn const_value(&self) -> Option<&Literal> {
151 match &self.body {
152 DefinitionBody::Value { const_value, .. } => const_value.as_ref(),
153 _ => None,
154 }
155 }
156
157 pub fn methods_mut(&mut self) -> Option<&mut MethodSignatures> {
158 match &mut self.body {
159 DefinitionBody::Struct { methods, .. } => Some(methods),
160 DefinitionBody::TypeAlias { methods, .. } => Some(methods),
161 DefinitionBody::Enum { methods, .. } => Some(methods),
162 _ => None,
163 }
164 }
165
166 pub fn attributes(&self) -> Option<&Attributes> {
167 match &self.body {
168 DefinitionBody::Struct { attributes, .. }
169 | DefinitionBody::Enum { attributes, .. }
170 | DefinitionBody::TypeAlias { attributes, .. } => Some(attributes),
171 _ => None,
172 }
173 }
174
175 pub fn is_display(&self) -> bool {
176 self.attributes()
177 .is_some_and(|a| a.contains_key(&TypeAttribute::Display))
178 }
179
180 pub fn is_closed_domain(&self) -> bool {
181 self.attributes()
182 .is_some_and(|a| a.contains_key(&TypeAttribute::ClosedDomain))
183 }
184
185 pub fn is_anon_struct(&self) -> bool {
186 self.attributes()
187 .is_some_and(|a| a.contains_key(&TypeAttribute::AnonStruct))
188 }
189
190 pub fn has_hidden_embed(&self) -> bool {
191 self.attributes()
192 .is_some_and(|a| a.contains_key(&TypeAttribute::HiddenEmbed))
193 }
194
195 pub fn is_type_definition(&self) -> bool {
196 matches!(
197 self.body,
198 DefinitionBody::Struct { .. }
199 | DefinitionBody::Enum { .. }
200 | DefinitionBody::TypeAlias { .. }
201 )
202 }
203
204 pub fn is_type_alias(&self) -> bool {
205 matches!(self.body, DefinitionBody::TypeAlias { .. })
206 }
207
208 pub fn is_value(&self, qualified_name: &str) -> bool {
209 matches!(self.body, DefinitionBody::Value { .. })
210 && self.ty.unwrap_forall().get_qualified_id() != Some(qualified_name)
211 }
212}
213
214pub type MethodSignatures = HashMap<EcoString, Type>;
215
216#[derive(Debug, Clone, PartialEq)]
217pub enum Visibility {
218 Public,
219 Private,
220 Local,
221}
222
223impl Visibility {
224 pub fn is_public(&self) -> bool {
225 matches!(self, Visibility::Public)
226 }
227}
228
229#[derive(Debug, Clone, PartialEq)]
230pub struct Interface {
231 pub name: EcoString,
232 pub generics: Vec<Generic>,
233 pub parents: Vec<Type>,
234 pub methods: HashMap<EcoString, Type>,
235}