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