Skip to main content

lisette_syntax/program/
definition.rs

1use rustc_hash::FxHashMap as HashMap;
2
3use ecow::EcoString;
4
5use crate::ast::{
6    Annotation, EnumVariant, Generic, Span, StructFieldDefinition, StructKind, ValueEnumVariant,
7};
8use crate::types::Type;
9
10#[derive(Debug, Clone)]
11pub enum Definition {
12    TypeAlias {
13        visibility: Visibility,
14        name: EcoString,
15        name_span: Span,
16        generics: Vec<Generic>,
17        annotation: Annotation,
18        ty: Type,
19        methods: MethodSignatures,
20        doc: Option<String>,
21    },
22    Enum {
23        visibility: Visibility,
24        ty: Type,
25        name: EcoString,
26        name_span: Span,
27        generics: Vec<Generic>,
28        variants: Vec<EnumVariant>,
29        methods: MethodSignatures,
30        doc: Option<String>,
31    },
32    ValueEnum {
33        visibility: Visibility,
34        ty: Type,
35        name: EcoString,
36        name_span: Span,
37        variants: Vec<ValueEnumVariant>,
38        underlying_ty: Option<Type>,
39        methods: MethodSignatures,
40        doc: Option<String>,
41    },
42    Struct {
43        visibility: Visibility,
44        ty: Type,
45        name: EcoString,
46        name_span: Span,
47        generics: Vec<Generic>,
48        fields: Vec<StructFieldDefinition>,
49        kind: StructKind,
50        methods: MethodSignatures,
51        constructor: Option<Type>,
52        doc: Option<String>,
53    },
54    Interface {
55        visibility: Visibility,
56        ty: Type,
57        name_span: Span,
58        definition: Interface,
59        doc: Option<String>,
60    },
61    Value {
62        visibility: Visibility,
63        ty: Type,
64        name_span: Option<Span>,
65        allowed_lints: Vec<String>,
66        go_hints: Vec<String>,
67        go_name: Option<String>,
68        doc: Option<String>,
69    },
70}
71
72impl Definition {
73    pub fn ty(&self) -> &Type {
74        match self {
75            Definition::TypeAlias { ty, .. } => ty,
76            Definition::Enum { ty, .. } => ty,
77            Definition::ValueEnum { ty, .. } => ty,
78            Definition::Struct { ty, .. } => ty,
79            Definition::Interface { ty, .. } => ty,
80            Definition::Value { ty, .. } => ty,
81        }
82    }
83
84    pub fn visibility(&self) -> &Visibility {
85        match self {
86            Definition::TypeAlias { visibility, .. } => visibility,
87            Definition::Enum { visibility, .. } => visibility,
88            Definition::ValueEnum { visibility, .. } => visibility,
89            Definition::Struct { visibility, .. } => visibility,
90            Definition::Interface { visibility, .. } => visibility,
91            Definition::Value { visibility, .. } => visibility,
92        }
93    }
94
95    pub fn allowed_lints(&self) -> &[String] {
96        match self {
97            Definition::Value { allowed_lints, .. } => allowed_lints,
98            _ => &[],
99        }
100    }
101
102    pub fn go_hints(&self) -> &[String] {
103        match self {
104            Definition::Value { go_hints, .. } => go_hints,
105            _ => &[],
106        }
107    }
108
109    pub fn go_name(&self) -> Option<&str> {
110        match self {
111            Definition::Value { go_name, .. } => go_name.as_deref(),
112            _ => None,
113        }
114    }
115
116    pub fn methods_mut(&mut self) -> Option<&mut MethodSignatures> {
117        match self {
118            Definition::Struct { methods, .. } => Some(methods),
119            Definition::TypeAlias { methods, .. } => Some(methods),
120            Definition::Enum { methods, .. } => Some(methods),
121            Definition::ValueEnum { methods, .. } => Some(methods),
122            _ => None,
123        }
124    }
125
126    pub fn is_type_definition(&self) -> bool {
127        matches!(
128            self,
129            Definition::Struct { .. }
130                | Definition::Enum { .. }
131                | Definition::ValueEnum { .. }
132                | Definition::TypeAlias { .. }
133        )
134    }
135
136    pub fn name_span(&self) -> Option<Span> {
137        match self {
138            Definition::TypeAlias { name_span, .. } => Some(*name_span),
139            Definition::Enum { name_span, .. } => Some(*name_span),
140            Definition::ValueEnum { name_span, .. } => Some(*name_span),
141            Definition::Struct { name_span, .. } => Some(*name_span),
142            Definition::Interface { name_span, .. } => Some(*name_span),
143            Definition::Value { name_span, .. } => *name_span,
144        }
145    }
146
147    pub fn doc(&self) -> Option<&String> {
148        match self {
149            Definition::TypeAlias { doc, .. }
150            | Definition::Enum { doc, .. }
151            | Definition::ValueEnum { doc, .. }
152            | Definition::Struct { doc, .. }
153            | Definition::Interface { doc, .. }
154            | Definition::Value { doc, .. } => doc.as_ref(),
155        }
156    }
157}
158
159pub type MethodSignatures = HashMap<EcoString, Type>;
160
161#[derive(Debug, Clone, PartialEq)]
162pub enum Visibility {
163    Public,
164    Private,
165    Local,
166}
167
168impl Visibility {
169    pub fn is_public(&self) -> bool {
170        matches!(self, Visibility::Public)
171    }
172}
173
174#[derive(Debug, Clone, PartialEq)]
175pub struct Interface {
176    pub name: EcoString,
177    pub generics: Vec<Generic>,
178    pub parents: Vec<Type>,
179    pub methods: HashMap<EcoString, Type>,
180}