archivist_lib/
identity.rsuse diagnostics_message::MonikerWithUrl;
use flyweights::FlyStr;
use moniker::ExtendedMoniker;
use std::hash::{Hash, Hasher};
#[derive(Debug)]
pub struct ComponentIdentity {
pub moniker: ExtendedMoniker,
pub url: FlyStr,
}
impl Hash for ComponentIdentity {
fn hash<H: Hasher>(&self, state: &mut H) {
self.moniker.hash(state);
}
}
impl PartialEq for ComponentIdentity {
fn eq(&self, other: &Self) -> bool {
self.moniker == other.moniker
}
}
impl Eq for ComponentIdentity {}
impl ComponentIdentity {
pub fn new(moniker: ExtendedMoniker, url: impl Into<FlyStr>) -> Self {
ComponentIdentity { moniker, url: url.into() }
}
pub fn unknown() -> Self {
Self::new(
ExtendedMoniker::parse_str("/UNKNOWN").expect("Unknown is valid"),
"fuchsia-pkg://UNKNOWN",
)
}
}
#[cfg(test)]
impl From<Vec<&str>> for ComponentIdentity {
fn from(moniker_segments: Vec<&str>) -> Self {
let moniker = moniker::Moniker::try_from(moniker_segments).unwrap();
Self { moniker: ExtendedMoniker::from(moniker), url: "".into() }
}
}
impl From<ComponentIdentity> for MonikerWithUrl {
fn from(identity: ComponentIdentity) -> Self {
Self { moniker: identity.moniker, url: identity.url }
}
}
impl From<&ComponentIdentity> for MonikerWithUrl {
fn from(identity: &ComponentIdentity) -> Self {
Self { moniker: identity.moniker.clone(), url: identity.url.clone() }
}
}
impl std::fmt::Display for ComponentIdentity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.moniker.fmt(f)
}
}