use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result;
use serde;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(from = "String", into = "String")]
#[repr(transparent)]
pub struct Fragment(String);
impl Fragment {
const PREFIX: char = '#';
pub fn new(value: impl Into<String>) -> Self {
let value = value.into();
if value.starts_with(Self::PREFIX) {
Self(value)
} else {
Self(format!("{}{}", Self::PREFIX, value))
}
}
pub fn identifier(&self) -> &str {
&self.0
}
pub fn name(&self) -> &str {
assert!(!self.0.is_empty());
&self.0[1..]
}
}
impl Debug for Fragment {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_fmt(format_args!("Fragment({})", self.name()))
}
}
impl Display for Fragment {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(self.name())
}
}
impl From<String> for Fragment {
fn from(other: String) -> Self {
Self::new(other)
}
}
impl From<Fragment> for String {
fn from(other: Fragment) -> Self {
other.0
}
}