2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
/// A name. This is just a `Token` of kind `Identifier`, but we give it its own
|
|
|
|
/// type for clarity.
|
2019-11-07 10:55:15 -08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
|
|
|
|
pub(crate) struct Name<'src> {
|
2024-01-08 13:26:33 -08:00
|
|
|
pub(crate) token: Token<'src>,
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Name<'src> {
|
2024-01-08 13:26:33 -08:00
|
|
|
pub(crate) fn from_identifier(token: Token<'src>) -> Self {
|
2019-11-07 10:55:15 -08:00
|
|
|
assert_eq!(token.kind, TokenKind::Identifier);
|
2024-01-08 13:26:33 -08:00
|
|
|
Self { token }
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
2024-01-08 13:26:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Deref for Name<'src> {
|
|
|
|
type Target = Token<'src>;
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.token
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Name<'_> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.lexeme())
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 00:07:48 -08:00
|
|
|
|
|
|
|
impl<'src> Serialize for Name<'src> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2022-01-30 12:16:10 -08:00
|
|
|
serializer.serialize_str(self.lexeme())
|
2021-11-17 00:07:48 -08:00
|
|
|
}
|
|
|
|
}
|