1
0
dust/dust-lang/src/identifier.rs

56 lines
1.2 KiB
Rust
Raw Normal View History

2024-03-25 04:16:55 +00:00
use std::{
fmt::{self, Display, Formatter},
sync::Arc,
};
2024-06-04 18:47:15 +00:00
use serde::{de::Visitor, Deserialize, Serialize};
2024-03-25 04:16:55 +00:00
#[derive(Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct Identifier(Arc<String>);
impl Identifier {
pub fn new<T: ToString>(string: T) -> Self {
Identifier(Arc::new(string.to_string()))
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
2024-06-04 18:47:15 +00:00
impl Serialize for Identifier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.0.as_str())
}
}
impl<'de> Deserialize<'de> for Identifier {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Identifier(Arc::new(
deserializer.deserialize_string(StringVisitor)?,
)))
}
}
struct StringVisitor;
impl<'de> Visitor<'de> for StringVisitor {
type Value = String;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a UTF-8 string")
}
}