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.
45use diagnostics_message::MonikerWithUrl;
6use flyweights::FlyStr;
7use moniker::ExtendedMoniker;
8use std::hash::{Hash, Hasher};
910#[derive(Debug)]
11pub struct ComponentIdentity {
12/// Moniker of the component that this artifacts container is representing.
13pub moniker: ExtendedMoniker,
1415/// The url with which the associated component was launched.
16pub url: FlyStr,
17}
1819// Just use the moniker when hashing and checking equality. That's the unique ID we need. The URL
20// is just used for metadata purposes. Typically both can be used and we can
21// derive(ComponentIdentity), but as long as we have the workaround for netstack2 diagnostics we
22// should just use the moniker as we don't know the runtime URL for this component.
23// TODO(https://fxbug.dev/324494668): consider just derive(Hash) when Netstack2 is gone.
24impl Hash for ComponentIdentity {
25fn hash<H: Hasher>(&self, state: &mut H) {
26self.moniker.hash(state);
27 }
28}
2930impl PartialEq for ComponentIdentity {
31fn eq(&self, other: &Self) -> bool {
32self.moniker == other.moniker
33 }
34}
3536impl Eq for ComponentIdentity {}
3738impl ComponentIdentity {
39pub fn new(moniker: ExtendedMoniker, url: impl Into<FlyStr>) -> Self {
40 ComponentIdentity { moniker, url: url.into() }
41 }
4243/// Returns generic metadata, suitable for providing a uniform ID to unattributed data.
44pub fn unknown() -> Self {
45Self::new(
46 ExtendedMoniker::parse_str("/UNKNOWN").expect("Unknown is valid"),
47"fuchsia-pkg://UNKNOWN",
48 )
49 }
50}
5152#[cfg(test)]
53impl From<Vec<&str>> for ComponentIdentity {
54fn from(moniker_segments: Vec<&str>) -> Self {
55let moniker = moniker::Moniker::try_from(moniker_segments).unwrap();
56Self { moniker: ExtendedMoniker::from(moniker), url: "".into() }
57 }
58}
5960impl From<ComponentIdentity> for MonikerWithUrl {
61fn from(identity: ComponentIdentity) -> Self {
62Self { moniker: identity.moniker, url: identity.url }
63 }
64}
6566impl From<&ComponentIdentity> for MonikerWithUrl {
67fn from(identity: &ComponentIdentity) -> Self {
68Self { moniker: identity.moniker.clone(), url: identity.url.clone() }
69 }
70}
7172impl std::fmt::Display for ComponentIdentity {
73fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74self.moniker.fmt(f)
75 }
76}