Skip to main content

archivist_lib/
identity.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use diagnostics_message::MonikerWithUrl;
6use flyweights::FlyStr;
7use moniker::{ExtendedMoniker, Moniker};
8
9#[derive(Debug, PartialEq, Eq, Hash)]
10pub struct ComponentIdentity {
11    /// Moniker of the component that this artifacts container is representing.
12    pub moniker: ExtendedMoniker,
13
14    /// The url with which the associated component was launched.
15    pub url: FlyStr,
16}
17
18impl PartialEq<Moniker> for ComponentIdentity {
19    fn eq(&self, other: &Moniker) -> bool {
20        matches!(&self.moniker, ExtendedMoniker::ComponentInstance(moniker) if moniker == other)
21    }
22}
23
24impl ComponentIdentity {
25    pub fn new(moniker: ExtendedMoniker, url: impl Into<FlyStr>) -> Self {
26        ComponentIdentity { moniker, url: url.into() }
27    }
28
29    /// Returns generic metadata, suitable for providing a uniform ID to unattributed data.
30    pub fn unknown() -> Self {
31        Self::new(
32            ExtendedMoniker::parse_str("/UNKNOWN").expect("Unknown is valid"),
33            "fuchsia-pkg://UNKNOWN",
34        )
35    }
36}
37
38#[cfg(test)]
39impl From<Vec<&str>> for ComponentIdentity {
40    fn from(moniker_segments: Vec<&str>) -> Self {
41        let moniker = moniker::Moniker::try_from(moniker_segments.as_slice()).unwrap();
42        Self { moniker: ExtendedMoniker::from(moniker), url: "".into() }
43    }
44}
45
46impl From<ComponentIdentity> for MonikerWithUrl {
47    fn from(identity: ComponentIdentity) -> Self {
48        Self { moniker: identity.moniker, url: identity.url }
49    }
50}
51
52impl From<&ComponentIdentity> for MonikerWithUrl {
53    fn from(identity: &ComponentIdentity) -> Self {
54        Self { moniker: identity.moniker.clone(), url: identity.url.clone() }
55    }
56}
57
58impl std::fmt::Display for ComponentIdentity {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        self.moniker.fmt(f)
61    }
62}