pedant_core/resolution/rust/
identity.rs1use std::fmt;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub(super) struct ProjectAuthority {
13 root: [u8; 32],
14 revision: [u8; 32],
15}
16
17impl ProjectAuthority {
18 pub(super) fn new(root: [u8; 32], revision: [u8; 32]) -> Self {
20 Self { root, revision }
21 }
22
23 pub(super) fn same_root(&self, other: &Self) -> bool {
25 self.root == other.root
26 }
27
28 pub(super) fn same_revision(&self, other: &Self) -> bool {
30 self.revision == other.revision
31 }
32}
33
34#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub struct PackageId {
37 authority: ProjectAuthority,
38 index: u32,
39}
40
41impl PackageId {
42 pub(super) fn new(authority: ProjectAuthority, index: u32) -> Self {
44 Self { authority, index }
45 }
46
47 pub(super) fn authority(&self) -> ProjectAuthority {
49 self.authority
50 }
51
52 pub(super) fn index(&self) -> u32 {
54 self.index
55 }
56}
57
58impl fmt::Debug for PackageId {
59 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
60 write_identity(formatter, "PackageId", self.index)
61 }
62}
63
64#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
66pub struct TargetId {
67 authority: ProjectAuthority,
68 index: u32,
69}
70
71impl TargetId {
72 pub(super) fn new(authority: ProjectAuthority, index: u32) -> Self {
74 Self { authority, index }
75 }
76
77 pub(super) fn authority(&self) -> ProjectAuthority {
79 self.authority
80 }
81
82 pub(super) fn index(&self) -> u32 {
84 self.index
85 }
86}
87
88impl fmt::Debug for TargetId {
89 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
90 write_identity(formatter, "TargetId", self.index)
91 }
92}
93
94fn write_identity(formatter: &mut fmt::Formatter<'_>, label: &str, index: u32) -> fmt::Result {
96 write!(formatter, "{label}({index})")
97}
98
99pub(super) fn index_of(value: u32) -> usize {
106 usize::try_from(value).unwrap_or(usize::MAX)
107}
108
109pub(super) fn position(value: usize) -> u32 {
114 u32::try_from(value).unwrap_or(u32::MAX)
115}