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)]
19pub struct ConstantMetadata {
20    pub attributes: Vec<AttributeMetadata>,
21    pub name: Atom,
22    pub span: Span,
23    pub type_metadata: Option<TypeMetadata>,
24    pub inferred_type: Option<TUnion>,
25    pub flags: MetadataFlags,
26    pub issues: Vec<Issue>,
27}
28
29impl ConstantMetadata {
30    /// Creates new `ConstantMetadata` for a non-deprecated, non-internal global constant item.
31    ///
32    /// # Arguments
33    ///
34    /// * `name`: The identifier (name) of the constant.
35    /// * `span`: The source code location of this specific constant's definition item (`NAME = value`).
36    #[inline]
37    #[must_use]
38    pub fn new(name: Atom, span: Span, flags: MetadataFlags) -> Self {
39        Self { attributes: Vec::new(), name, span, flags, type_metadata: None, inferred_type: None, issues: Vec::new() }
40    }
41
42    /// Returns a mutable slice of docblock issues.
43    #[inline]
44    pub fn take_issues(&mut self) -> Vec<Issue> {
45        std::mem::take(&mut self.issues)
46    }
47}
48
49impl HasSpan for ConstantMetadata {
50    fn span(&self) -> Span {
51        self.span
52    }
53}