guppy/
package_id.rs

1// Copyright (c) The cargo-guppy Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::fmt;
5
6/// An "opaque" identifier for a package.
7#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
8#[allow(clippy::derived_hash_with_manual_eq)] // safe because the same PartialEq impl is used everywhere
9pub struct PackageId {
10    /// The underlying string representation of an ID.
11    repr: Box<str>,
12}
13
14impl PackageId {
15    /// Creates a new `PackageId`.
16    pub fn new(s: impl Into<Box<str>>) -> Self {
17        Self { repr: s.into() }
18    }
19
20    pub(super) fn from_metadata(id: cargo_metadata::PackageId) -> Self {
21        Self {
22            repr: id.repr.into_boxed_str(),
23        }
24    }
25
26    /// Returns the inner representation of a package ID. This is generally an opaque string and its
27    /// precise format is subject to change.
28    pub fn repr(&self) -> &str {
29        &self.repr
30    }
31}
32
33impl fmt::Display for PackageId {
34    fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
35        fmt::Display::fmt(&self.repr, f)
36    }
37}
38
39impl PartialEq<&PackageId> for PackageId {
40    fn eq(&self, other: &&PackageId) -> bool {
41        self.eq(*other)
42    }
43}
44
45impl PartialEq<PackageId> for &PackageId {
46    fn eq(&self, other: &PackageId) -> bool {
47        (*self).eq(other)
48    }
49}