-
Notifications
You must be signed in to change notification settings - Fork 499
/
name.rs
38 lines (32 loc) · 833 Bytes
/
name.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use super::*;
/// A name. This is just a `Token` of kind `Identifier`, but we give it its own
/// type for clarity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
pub(crate) struct Name<'src> {
pub(crate) token: Token<'src>,
}
impl<'src> Name<'src> {
pub(crate) fn from_identifier(token: Token<'src>) -> Self {
assert_eq!(token.kind, TokenKind::Identifier);
Self { token }
}
}
impl<'src> Deref for Name<'src> {
type Target = Token<'src>;
fn deref(&self) -> &Self::Target {
&self.token
}
}
impl Display for Name<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.lexeme())
}
}
impl Serialize for Name<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.lexeme())
}
}