mago_codex/metadata/
constant.rs

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