Skip to main content

mago_codex/metadata/
constant.rs

1use serde::Deserialize;
2use serde::Serialize;
3
4use mago_atom::Atom;
5use mago_reporting::Issue;
6use mago_span::HasSpan;
7use mago_span::Span;
8
9use crate::metadata::attribute::AttributeMetadata;
10use crate::metadata::flags::MetadataFlags;
11use crate::metadata::ttype::TypeMetadata;
12use crate::ttype::union::TUnion;
13
14/// Contains metadata associated with a global constant defined using `const`.
15///
16/// Represents a single constant declaration item, potentially within a grouped declaration,
17/// like `MAX_RETRIES = 3` in `const MAX_RETRIES = 3;` or `B = 2` in `const A = 1, B = 2;`.
18#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
19#[non_exhaustive]
20pub struct ConstantMetadata {
21    pub attributes: Vec<AttributeMetadata>,
22    pub name: Atom,
23    pub span: Span,
24    pub type_metadata: Option<TypeMetadata>,
25    pub inferred_type: Option<TUnion>,
26    pub flags: MetadataFlags,
27    pub issues: Vec<Issue>,
28}
29
30impl ConstantMetadata {
31    /// Creates new `ConstantMetadata` for a non-deprecated, non-internal global constant item.
32    ///
33    /// # Arguments
34    ///
35    /// * `name`: The identifier (name) of the constant.
36    /// * `span`: The source code location of this specific constant's definition item (`NAME = value`).
37    #[inline]
38    #[must_use]
39    pub fn new(name: Atom, span: Span, flags: MetadataFlags) -> Self {
40        Self { attributes: Vec::new(), name, span, flags, type_metadata: None, inferred_type: None, issues: Vec::new() }
41    }
42
43    /// Returns a mutable slice of docblock issues.
44    #[inline]
45    pub fn take_issues(&mut self) -> Vec<Issue> {
46        std::mem::take(&mut self.issues)
47    }
48}
49
50impl HasSpan for ConstantMetadata {
51    fn span(&self) -> Span {
52        self.span
53    }
54}