Skip to main content

mago_type_syntax/ast/
variable.rs

1use serde::Serialize;
2
3use mago_database::file::FileId;
4use mago_span::HasSpan;
5use mago_span::Span;
6
7use crate::token::TypeToken;
8use crate::token::TypeTokenKind;
9
10#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
11pub struct VariableType<'arena> {
12    pub span: Span,
13    pub value: &'arena str,
14}
15
16impl HasSpan for VariableType<'_> {
17    fn span(&self) -> Span {
18        self.span
19    }
20}
21
22impl<'arena> VariableType<'arena> {
23    /// Creates a VariableType from a TypeToken and file_id.
24    #[inline]
25    #[must_use]
26    pub fn from_token(token: TypeToken<'arena>, file_id: FileId) -> Self {
27        debug_assert_eq!(token.kind, TypeTokenKind::Variable, "expected a Variable token");
28
29        VariableType { span: token.span_for(file_id), value: token.value }
30    }
31}
32
33impl std::fmt::Display for VariableType<'_> {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        write!(f, "{}", self.value)
36    }
37}