use super::{Error, Result, fmt, write_hex};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UploadId(pub(in crate::content) [u8; 16]);
impl UploadId {
pub fn new() -> Result<Self> {
Self::generate()
}
pub(crate) fn generate() -> Result<Self> {
let mut bytes = [0_u8; 16];
getrandom::fill(&mut bytes)
.map_err(|error| Error::runtime_busy(format!("upload identity entropy: {error}")))?;
Ok(Self(bytes))
}
pub(crate) const fn bytes(self) -> [u8; 16] {
self.0
}
#[must_use]
pub const fn from_bytes(bytes: [u8; 16]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn to_bytes(self) -> [u8; 16] {
self.0
}
}
impl fmt::Debug for UploadId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "UploadId({self})")
}
}
impl fmt::Display for UploadId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write_hex(formatter, &self.0)
}
}