ligen_ir/identifier/
mod.rs1#[cfg(any(test, feature = "mocks"))]
2pub mod mock;
3
4pub mod naming_convention;
5use is_tree::IsPathSegment;
6pub use naming_convention::*;
7
8use crate::path::PathSegment;
9use crate::{prelude::*, Mutability};
10
11#[derive(Clone, Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Display, Serialize, Deserialize)]
13#[display(fmt = "{}", name)]
14pub struct Identifier {
15    pub name: String,
17}
18
19impl PartialEq<Identifier> for &str {
20    fn eq(&self, other: &Identifier) -> bool {
21        self.eq(&other.name)
22    }
23}
24
25impl Identifier {
26    pub fn new<S: AsRef<str>>(name: S) -> Self {
28        let name = String::from(name.as_ref());
29        Self { name }
30    }
31
32    pub fn reference(mutability: Mutability) -> Self {
34        match mutability {
35            Mutability::Constant => Self::constant_reference(),
36            Mutability::Mutable => Self::mutable_reference(),
37        }
38    }
39
40    pub fn mutable_reference() -> Self {
42        "MutableReference".into()
43    }
44
45    pub fn constant_reference() -> Self {
47        "Reference".into()
48    }
49
50    pub fn union() -> Self {
52        "Union".into()
53    }
54
55    pub fn variadic() -> Self {
57        "Variadic".into()
58    }
59
60    pub fn tuple() -> Self {
62        "Tuple".into()
63    }
64
65    pub fn dictionary() -> Self {
67        "Dictionary".into()
68    }
69
70    pub fn slice() -> Self {
72        "Slice".into()
73    }
74
75    pub fn array() -> Self {
77        "Array".into()
78    }
79
80    pub fn vector() -> Self {
82        "Vector".into()
83    }
84
85    pub fn date_time() -> Self {
87        "DateTime".into()
88    }
89
90    pub fn option() -> Self {
92        "Option".into()
93    }
94
95    pub fn opaque() -> Self {
97        "Opaque".into()
98    }
99
100    pub fn boolean() -> Self {
102        "Boolean".into()
103    }
104
105    pub fn character() -> Self {
107        "Character".into()
108    }
109
110    pub fn i8() -> Self {
112        "I8".into()
113    }
114
115    pub fn i16() -> Self {
117        "I16".into()
118    }
119
120    pub fn i32() -> Self {
122        "I32".into()
123    }
124
125    pub fn i64() -> Self {
127        "I64".into()
128    }
129
130    pub fn i128() -> Self {
132        "I128".into()
133    }
134
135    pub fn isize() -> Self {
137        "ISize".into()
138    }
139
140    pub fn u8() -> Self {
142        "U8".into()
143    }
144
145    pub fn u16() -> Self {
147        "U16".into()
148    }
149
150    pub fn u32() -> Self {
152        "U32".into()
153    }
154
155    pub fn u64() -> Self {
157        "U64".into()
158    }
159
160    pub fn u128() -> Self {
162        "U128".into()
163    }
164
165    pub fn usize() -> Self {
167        "USize".into()
168    }
169
170    pub fn f16() -> Self {
172        "F16".into()
173    }
174
175    pub fn f32() -> Self {
177        "F32".into()
178    }
179
180    pub fn f64() -> Self {
182        "F64".into()
183    }
184
185    pub fn f128() -> Self {
187        "F128".into()
188    }
189
190    pub fn string() -> Self {
192        "String".into()
193    }
194
195    pub fn root() -> Self {
197        "root".into()
198    }
199
200    pub fn super_() -> Self {
202        "super".into()
203    }
204
205    pub fn self_() -> Self {
207        "self".into()
208    }
209    
210}
211
212impl From<Identifier> for String {
213    fn from(value: Identifier) -> Self {
214        value.name
215    }
216}
217
218impl PartialEq<&str> for Identifier {
219    fn eq(&self, other: &&str) -> bool {
220        self.name == *other
221    }
222}
223
224impl From<&str> for Identifier {
225    fn from(name: &str) -> Self {
226        Self { name: name.to_string() }
227    }
228}
229
230impl From<String> for Identifier {
231    fn from(name: String) -> Self {
232        name.as_str().into()
233    }
234}
235
236impl From<PathSegment> for Identifier {
237    fn from(value: PathSegment) -> Self {
238        value.identifier
239    }
240}
241
242impl IsPathSegment for Identifier {
243    fn root() -> Self {
244        Self::new("root")
245    }
246
247    fn self_() -> Self {
248        Self::new("self")
249    }
250
251    fn super_() -> Self {
252        Self::new("super")
253    }
254}