just/src/name.rs

39 lines
841 B
Rust
Raw Normal View History

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())
}
}
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
}
}