use std::fmt;
use std::str::FromStr;
use crate::{Error, Result};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(String);
impl Id {
pub fn new(value: impl Into<String>) -> Result<Self> {
let value = value.into();
if value.is_empty() {
return Err(Error::EmptyId);
}
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for Id {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for Id {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
Self::new(value)
}
}
macro_rules! typed_id {
($name:ident) => {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name(Id);
impl $name {
pub fn new(value: impl Into<String>) -> Result<Self> {
Id::new(value).map(Self)
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl fmt::Display for $name {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for $name {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
Self::new(value)
}
}
};
}
typed_id!(ParameterId);
typed_id!(PartId);
typed_id!(DrawableId);