mago_reflection/constant.rs
1use serde::Deserialize;
2use serde::Serialize;
3
4use mago_reporting::IssueCollection;
5use mago_source::HasSource;
6use mago_source::SourceCategory;
7use mago_source::SourceIdentifier;
8use mago_span::HasSpan;
9use mago_span::Span;
10
11use crate::Reflection;
12use crate::attribute::AttributeReflection;
13use crate::identifier::Name;
14use crate::r#type::TypeReflection;
15
16/// Represents a constant reflection in the codebase.
17///
18/// A `ConstantReflection` provides metadata about a single constant, including its
19/// name, type, and location in the source code. Constants are uniquely identified
20/// and separated even when declared in a single statement, such as `const A = 1, B = 2;`.
21#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
22pub struct ConstantReflection {
23 /// Collection of attributes applied to the constant.
24 pub attribute_reflections: Vec<AttributeReflection>,
25
26 /// The name of the constant.
27 pub name: Name,
28
29 /// The type reflection of the constant.
30 pub type_reflection: TypeReflection,
31
32 /// The span of the specific constant item (`A = 1` in `const A = 1, B = 2;`).
33 pub item_span: Span,
34
35 /// The span of the entire constant definition (`const A = 1, B = 2;`).
36 pub definition_span: Span,
37
38 /// Whether the reflection's metadata is fully populated.
39 pub is_populated: bool,
40
41 /// Collection of issues related to the constant.
42 pub issues: IssueCollection,
43}
44
45impl HasSpan for ConstantReflection {
46 /// Returns the span of the constant item in the source code.
47 ///
48 /// This span includes just the `A = 1` part of the constant definition.
49 fn span(&self) -> Span {
50 self.item_span
51 }
52}
53
54impl HasSource for ConstantReflection {
55 /// Returns the source identifier of the file containing this constant.
56 ///
57 /// The source identifier includes metadata about the file or context where the constant
58 /// is defined, such as whether it is a user-defined, vendor-provided, or built-in constant.
59 fn source(&self) -> SourceIdentifier {
60 self.span().source()
61 }
62}
63
64impl Reflection for ConstantReflection {
65 /// Returns the category of the source where the constant is defined.
66 ///
67 /// The category indicates whether the constant is part of the project (`UserDefined`),
68 /// comes from a external library (`External`), or is built into the language (`BuiltIn`).
69 fn get_category(&self) -> SourceCategory {
70 self.source().category()
71 }
72
73 /// Indicates whether the constant's reflection data is fully populated.
74 ///
75 /// If `is_populated` is `false`, additional processing may be required to resolve
76 /// the constant's metadata completely.
77 fn is_populated(&self) -> bool {
78 self.is_populated
79 }
80
81 fn take_issues(&mut self) -> IssueCollection {
82 std::mem::take(&mut self.issues)
83 }
84}