Skip to main content

mago_type_syntax/ast/
identifier.rs

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