use super::{CONTENT_ACCESS_BARRIER_ID_VERSION, Error, Result, StorageDomainId, fmt, write_hex};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ContentAccessBarrierId([u8; 16]);
impl ContentAccessBarrierId {
pub fn generate() -> Result<Self> {
let mut bytes = [0_u8; 16];
getrandom::fill(&mut bytes).map_err(|error| {
Error::runtime_busy(format!("content access-barrier entropy: {error}"))
})?;
bytes[0] = CONTENT_ACCESS_BARRIER_ID_VERSION;
Ok(Self(bytes))
}
pub fn from_bytes(bytes: [u8; 16]) -> Result<Self> {
if bytes[0] != CONTENT_ACCESS_BARRIER_ID_VERSION {
return Err(Error::UnsupportedFormat {
message: format!(
"unsupported content access-barrier identity version {}",
bytes[0]
),
});
}
Ok(Self(bytes))
}
#[must_use]
pub const fn to_bytes(self) -> [u8; 16] {
self.0
}
}
impl fmt::Debug for ContentAccessBarrierId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("ContentAccessBarrierId(")?;
write_hex(formatter, &self.0)?;
formatter.write_str(")")
}
}
impl fmt::Display for ContentAccessBarrierId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write_hex(formatter, &self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContentAccessMode {
CompatibleUnleased,
LeasedOnly {
barrier_id: ContentAccessBarrierId,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContentAccessBarrier {
storage_domain_id: StorageDomainId,
barrier_id: ContentAccessBarrierId,
enforced_at: crate::ReadVersion,
}
impl ContentAccessBarrier {
pub(crate) const fn new(
storage_domain_id: StorageDomainId,
barrier_id: ContentAccessBarrierId,
enforced_at: crate::ReadVersion,
) -> Self {
Self {
storage_domain_id,
barrier_id,
enforced_at,
}
}
#[must_use]
pub const fn storage_domain_id(self) -> StorageDomainId {
self.storage_domain_id
}
#[must_use]
pub const fn barrier_id(self) -> ContentAccessBarrierId {
self.barrier_id
}
#[must_use]
pub const fn enforced_at(self) -> crate::ReadVersion {
self.enforced_at
}
}