use alloc::sync::Arc;
use core::{borrow::Borrow, fmt, ops::Deref};
use crate::id::is_valid_id;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(Arc<str>);
impl Id {
pub fn new(s: &str) -> Option<Self> {
is_valid_id(s).then(|| Id(Arc::from(s)))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Borrow<str> for Id {
fn borrow(&self) -> &str {
&self.0
}
}
impl AsRef<str> for Id {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Deref for Id {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl PartialEq<str> for Id {
fn eq(&self, other: &str) -> bool {
&*self.0 == other
}
}
impl PartialEq<&str> for Id {
fn eq(&self, other: &&str) -> bool {
&*self.0 == *other
}
}
impl fmt::Debug for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&*self.0, f)
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PropertyAddr {
pub device: Id,
pub node: Id,
pub property: Id,
}
impl PropertyAddr {
pub fn new(device: &str, node: &str, property: &str) -> Option<Self> {
Some(PropertyAddr {
device: Id::new(device)?,
node: Id::new(node)?,
property: Id::new(property)?,
})
}
}