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