use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(super) struct ProjectAuthority {
root: [u8; 32],
revision: [u8; 32],
}
impl ProjectAuthority {
pub(super) fn new(root: [u8; 32], revision: [u8; 32]) -> Self {
Self { root, revision }
}
pub(super) fn same_root(&self, other: &Self) -> bool {
self.root == other.root
}
pub(super) fn same_revision(&self, other: &Self) -> bool {
self.revision == other.revision
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PackageId {
authority: ProjectAuthority,
index: u32,
}
impl PackageId {
pub(super) fn new(authority: ProjectAuthority, index: u32) -> Self {
Self { authority, index }
}
pub(super) fn authority(&self) -> ProjectAuthority {
self.authority
}
pub(super) fn index(&self) -> u32 {
self.index
}
}
impl fmt::Debug for PackageId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write_identity(formatter, "PackageId", self.index)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TargetId {
authority: ProjectAuthority,
index: u32,
}
impl TargetId {
pub(super) fn new(authority: ProjectAuthority, index: u32) -> Self {
Self { authority, index }
}
pub(super) fn authority(&self) -> ProjectAuthority {
self.authority
}
pub(super) fn index(&self) -> u32 {
self.index
}
}
impl fmt::Debug for TargetId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write_identity(formatter, "TargetId", self.index)
}
}
fn write_identity(formatter: &mut fmt::Formatter<'_>, label: &str, index: u32) -> fmt::Result {
write!(formatter, "{label}({index})")
}
pub(super) fn index_of(value: u32) -> usize {
usize::try_from(value).unwrap_or(usize::MAX)
}
pub(super) fn position(value: usize) -> u32 {
u32::try_from(value).unwrap_or(u32::MAX)
}