mago_syntax/ast/ast/
identifier.rs

1use serde::Deserialize;
2use serde::Serialize;
3use strum::Display;
4
5use mago_interner::StringIdentifier;
6use mago_span::HasSpan;
7use mago_span::Span;
8
9/// Represents an identifier.
10///
11/// An identifier can be a local, qualified, or fully qualified identifier.
12#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord, Display)]
13#[serde(tag = "type", content = "value")]
14#[repr(C, u8)]
15pub enum Identifier {
16    Local(LocalIdentifier),
17    Qualified(QualifiedIdentifier),
18    FullyQualified(FullyQualifiedIdentifier),
19}
20
21/// Represents a local, unqualified identifier.
22///
23/// Example: `foo`, `Bar`, `BAZ`
24#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
25#[repr(C)]
26pub struct LocalIdentifier {
27    pub span: Span,
28    pub value: StringIdentifier,
29}
30
31/// Represents a qualified identifier.
32///
33/// Example: `Foo\bar`, `Bar\Baz`, `Baz\QUX`
34#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
35#[repr(C)]
36pub struct QualifiedIdentifier {
37    pub span: Span,
38    pub value: StringIdentifier,
39}
40
41/// Represents a fully qualified identifier.
42///
43/// Example: `\Foo\bar`, `\Bar\Baz`, `\Baz\QUX`
44#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
45#[repr(C)]
46pub struct FullyQualifiedIdentifier {
47    pub span: Span,
48    pub value: StringIdentifier,
49}
50
51impl Identifier {
52    #[inline]
53    pub const fn is_fully_qualified(&self) -> bool {
54        matches!(self, Identifier::FullyQualified(_))
55    }
56
57    #[inline]
58    pub const fn value(&self) -> &StringIdentifier {
59        match &self {
60            Identifier::Local(local_identifier) => &local_identifier.value,
61            Identifier::Qualified(qualified_identifier) => &qualified_identifier.value,
62            Identifier::FullyQualified(fully_qualified_identifier) => &fully_qualified_identifier.value,
63        }
64    }
65}
66
67impl HasSpan for Identifier {
68    fn span(&self) -> Span {
69        match self {
70            Identifier::Local(local) => local.span(),
71            Identifier::Qualified(qualified) => qualified.span(),
72            Identifier::FullyQualified(fully_qualified) => fully_qualified.span(),
73        }
74    }
75}
76
77impl HasSpan for LocalIdentifier {
78    fn span(&self) -> Span {
79        self.span
80    }
81}
82
83impl HasSpan for QualifiedIdentifier {
84    fn span(&self) -> Span {
85        self.span
86    }
87}
88
89impl HasSpan for FullyQualifiedIdentifier {
90    fn span(&self) -> Span {
91        self.span
92    }
93}