use core::fmt;
use crate::thing::{FeatureId, PropertyId, ThingId};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PropertyPath {
#[cfg_attr(feature = "serde", serde(rename = "thingId"))]
pub thing: ThingId,
#[cfg_attr(feature = "serde", serde(rename = "featureId"))]
pub feature: FeatureId,
#[cfg_attr(feature = "serde", serde(rename = "propertyId"))]
pub property: PropertyId,
}
impl PropertyPath {
pub fn new(thing: ThingId, feature: FeatureId, property: PropertyId) -> Self {
Self {
thing,
feature,
property,
}
}
}
impl fmt::Display for PropertyPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}/{}/{}",
self.thing.as_str(),
self.feature.as_str(),
self.property.as_str()
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_canonical() {
let p = PropertyPath::new(
ThingId::new("acme:pump-7"),
FeatureId::new("temperature"),
PropertyId::new("value"),
);
assert_eq!(p.to_string(), "acme:pump-7/temperature/value");
}
}