mago_codex/metadata/ttype.rs
1use serde::Deserialize;
2use serde::Serialize;
3
4use mago_span::Span;
5
6use crate::ttype::union::TUnion;
7
8/// Contains metadata associated with a specific type instance within the type system.
9///
10/// This struct combines the core type information (`TUnion`) with contextual details
11/// about *how* and *where* this type information was determined or declared in the source code
12/// or related documentation.
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
14#[non_exhaustive]
15pub struct TypeMetadata {
16 /// The specific location (span) in the source code or documentation
17 /// that this type metadata corresponds to.
18 ///
19 /// This could be:
20 /// - The span of a type annotation (e.g., `: string`).
21 /// - The span of an expression whose type was inferred (e.g., `$x = 10` -> span of `10`).
22 /// - The span of a type mentioned in a documentation block (e.g., `@param int` -> span of `int`).
23 pub span: Span,
24
25 /// The core representation of the type itself.
26 pub type_union: TUnion,
27
28 /// Distinguishes whether this type information originated from analyzing
29 /// executable code constructs (e.g., type declarations, assignments)
30 /// or from documentation blocks.
31 ///
32 /// - `true` if the type information was extracted from a docblock comment.
33 /// - `false` if the type information came from actual code analysis.
34 pub from_docblock: bool,
35
36 /// Indicates whether this type was explicitly declared in the source
37 /// or deduced ("inferred") by the type checker based on context.
38 ///
39 /// - `true` if the type checker inferred this type (e.g., from a variable initialization like `$x = 10;`).
40 /// - `false` if the type was explicitly written by the user (e.g., `int $x;`).
41 pub inferred: bool,
42}
43
44impl TypeMetadata {
45 /// Creates new `TypeMetadata` for an explicitly declared type from code.
46 ///
47 /// This is a convenience constructor assuming the common case where a type
48 /// is directly specified in the code, and wasn't just inferred or from a docblock.
49 ///
50 /// # Arguments
51 ///
52 /// * `type_union`: The core type information (`TUnion`).
53 /// * `span`: The source code location associated with this type.
54 ///
55 /// # Returns
56 ///
57 /// A new `TypeMetadata` instance with `is_nullable`, `from_docblock`, and `inferred` set to `false`.
58 #[must_use]
59 pub fn new(type_union: TUnion, span: Span) -> Self {
60 Self { span, type_union, from_docblock: false, inferred: false }
61 }
62
63 /// Creates new `TypeMetadata` for a type extracted from a documentation block.
64 ///
65 /// This constructor is used when type information is sourced from
66 /// docblock comments rather than executable code.
67 ///
68 /// # Arguments
69 ///
70 /// * `type_union`: The core type information (`TUnion`).
71 /// * `span`: The source code location associated with this docblock type.
72 ///
73 /// # Returns
74 ///
75 /// A new `TypeMetadata` instance with `from_docblock` set to `true` and `inferred` set to `false`.
76 #[must_use]
77 pub fn from_docblock(type_union: TUnion, span: Span) -> Self {
78 Self { span, type_union, from_docblock: true, inferred: false }
79 }
80
81 /// Creates a new `TypeMetadata` by applying a function to the inner `TUnion`.
82 ///
83 /// This allows transforming the core type while preserving the surrounding metadata.
84 ///
85 /// # Arguments
86 ///
87 /// * `f`: A function that takes the current `TUnion` and returns a new `TUnion`.
88 ///
89 /// # Returns
90 ///
91 /// A new `TypeMetadata` instance with the transformed `TUnion` and the same metadata flags and span.
92 pub fn map_type_union<F>(self, f: F) -> Self
93 where
94 F: FnOnce(TUnion) -> TUnion,
95 {
96 Self {
97 span: self.span,
98 type_union: f(self.type_union),
99 from_docblock: self.from_docblock,
100 inferred: self.inferred,
101 }
102 }
103}