ligen_ir/identifier/
mod.rs

1#[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/// Identifier structure
12#[derive(Clone, Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Display, Serialize, Deserialize)]
13#[display(fmt = "{}", name)]
14pub struct Identifier {
15    /// Name field of Identifier
16    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    /// Create a new Identifier
27    pub fn new<S: AsRef<str>>(name: S) -> Self {
28        let name = String::from(name.as_ref());
29        Self { name }
30    }
31
32    /// Returns a new `Identifier` representing a reference type.
33    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    /// Returns a new `Identifier` representing a mutable reference type.
41    pub fn mutable_reference() -> Self {
42        "MutableReference".into()
43    }
44
45    /// Returns a new `Identifier` representing a constant reference type.
46    pub fn constant_reference() -> Self {
47        "Reference".into()
48    }
49
50    /// Returns a new `Identifier` representing a union type.
51    pub fn union() -> Self {
52        "Union".into()
53    }
54
55    /// Returns a new `Identifier` representing a variadic type.
56    pub fn variadic() -> Self {
57        "Variadic".into()
58    }
59
60    /// Returns a new `Identifier` representing a tuple type.
61    pub fn tuple() -> Self {
62        "Tuple".into()
63    }
64
65    /// Returns a new `Identifier` representing a dictionary type.
66    pub fn dictionary() -> Self {
67        "Dictionary".into()
68    }
69
70    /// Returns a new `Identifier` representing a slice type.
71    pub fn slice() -> Self {
72        "Slice".into()
73    }
74
75    /// Returns a new `Identifier` representing an array type.
76    pub fn array() -> Self {
77        "Array".into()
78    }
79
80    /// Returns a new `Identifier` representing a vector type.
81    pub fn vector() -> Self {
82        "Vector".into()
83    }
84
85    /// Returns a new `Identifier` representing a date type.
86    pub fn date_time() -> Self {
87        "DateTime".into()
88    }
89
90    /// Returns a new `Identifier` representing an Option type.
91    pub fn option() -> Self {
92        "Option".into()
93    }
94
95    /// Returns a new `Identifier` representing an opaque type.
96    pub fn opaque() -> Self {
97        "Opaque".into()
98    }
99
100    /// Returns a new `Identifier` representing a boolean type.
101    pub fn boolean() -> Self {
102        "Boolean".into()
103    }
104
105    /// Returns a new `Identifier` representing a character type.
106    pub fn character() -> Self {
107        "Character".into()
108    }
109
110    /// Returns a new `Identifier` representing an 8-bit signed integer type.
111    pub fn i8() -> Self {
112        "I8".into()
113    }
114
115    /// Returns a new `Identifier` representing a 16-bit signed integer type.
116    pub fn i16() -> Self {
117        "I16".into()
118    }
119
120    /// Returns a new `Identifier` representing a 32-bit signed integer type.
121    pub fn i32() -> Self {
122        "I32".into()
123    }
124
125    /// Returns a new `Identifier` representing a 64-bit signed integer type.
126    pub fn i64() -> Self {
127        "I64".into()
128    }
129
130    /// Returns a new `Identifier` representing a 128-bit signed integer type.
131    pub fn i128() -> Self {
132        "I128".into()
133    }
134
135    /// Returns a new `Identifier` representing an pointer-sized integer type.
136    pub fn isize() -> Self {
137        "ISize".into()
138    }
139
140    /// Returns a new `Identifier` representing an 8-bit unsigned integer type.
141    pub fn u8() -> Self {
142        "U8".into()
143    }
144
145    /// Returns a new `Identifier` representing a 16-bit unsigned integer type.
146    pub fn u16() -> Self {
147        "U16".into()
148    }
149
150    /// Returns a new `Identifier` representing a 32-bit unsigned integer type.
151    pub fn u32() -> Self {
152        "U32".into()
153    }
154
155    /// Returns a new `Identifier` representing a 64-bit unsigned integer type.
156    pub fn u64() -> Self {
157        "U64".into()
158    }
159
160    /// Returns a new `Identifier` representing a 128-bit unsigned integer type.
161    pub fn u128() -> Self {
162        "U128".into()
163    }
164
165    /// Returns a new `Identifier` representing an pointer-sized unsigned integer type.
166    pub fn usize() -> Self {
167        "USize".into()
168    }
169
170    /// Returns a new `Identifier` representing a 16-bit floating-point type.
171    pub fn f16() -> Self {
172        "F16".into()
173    }
174
175    /// Returns a new `Identifier` representing a 32-bit floating-point type.
176    pub fn f32() -> Self {
177        "F32".into()
178    }
179
180    /// Returns a new `Identifier` representing a 64-bit floating-point type.
181    pub fn f64() -> Self {
182        "F64".into()
183    }
184
185    /// Returns a new `Identifier` representing a 128-bit floating-point type.
186    pub fn f128() -> Self {
187        "F128".into()
188    }
189
190    /// Returns a new `Identifier` representing a string type.
191    pub fn string() -> Self {
192        "String".into()
193    }
194
195    /// Returns a new `Identifier` representing the `root` keyword.
196    pub fn root() -> Self {
197        "root".into()
198    }
199
200    /// Returns a new `Identifier` representing the `super` keyword.
201    pub fn super_() -> Self {
202        "super".into()
203    }
204
205    /// Returns a new `Identifier` representing the `self` keyword.
206    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}