#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
#[cfg(feature = "std")]
pub const EPOCH: std::time::SystemTime = std::time::SystemTime::UNIX_EPOCH;
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BlobMetadata {
len: u64,
#[cfg(feature = "alloc")]
media_type: Option<Cow<'static, str>>,
created: Option<u64>,
#[cfg_attr(feature = "serde", serde(default))]
updated: Option<u64>,
accessed: Option<u64>,
expires: Option<u64>,
}
impl core::fmt::Display for BlobMetadata {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut result = f.debug_tuple("");
result.field(&self.len);
#[cfg(feature = "alloc")]
result.field(&self.media_type);
result
.field(&self.created)
.field(&self.updated)
.field(&self.accessed)
.field(&self.expires)
.finish()
}
}
impl BlobMetadata {
pub fn new(len: u64) -> Self {
Self::default().with_len(len)
}
pub fn len(&self) -> u64 {
self.len
}
#[cfg(feature = "alloc")]
pub fn media_type(&self) -> Option<&str> {
self.media_type.as_deref()
}
#[cfg(feature = "std")]
pub fn created(&self) -> Option<std::time::SystemTime> {
use std::time::Duration;
self.created
.and_then(|t| EPOCH.checked_add(Duration::from_nanos(t)))
}
pub fn created_nanos(&self) -> Option<u64> {
self.created
}
pub fn created_millis(&self) -> Option<u64> {
self.created.map(|t| t / 1e6 as u64)
}
pub fn created_secs(&self) -> Option<u64> {
self.created.map(|t| t / 1e9 as u64)
}
#[cfg(feature = "std")]
pub fn updated(&self) -> Option<std::time::SystemTime> {
use std::time::Duration;
self.updated
.and_then(|t| EPOCH.checked_add(Duration::from_nanos(t)))
}
pub fn updated_nanos(&self) -> Option<u64> {
self.updated
}
pub fn updated_millis(&self) -> Option<u64> {
self.updated.map(|t| t / 1e6 as u64)
}
pub fn updated_secs(&self) -> Option<u64> {
self.updated.map(|t| t / 1e9 as u64)
}
#[cfg(feature = "std")]
pub fn accessed(&self) -> Option<std::time::SystemTime> {
use std::time::Duration;
self.accessed
.and_then(|t| EPOCH.checked_add(Duration::from_nanos(t)))
}
pub fn accessed_nanos(&self) -> Option<u64> {
self.accessed
}
pub fn accessed_millis(&self) -> Option<u64> {
self.accessed.map(|t| t / 1e6 as u64)
}
pub fn accessed_secs(&self) -> Option<u64> {
self.accessed.map(|t| t / 1e9 as u64)
}
#[cfg(feature = "std")]
pub fn expires(&self) -> Option<std::time::SystemTime> {
use std::time::Duration;
self.expires
.and_then(|t| EPOCH.checked_add(Duration::from_nanos(t)))
}
pub fn expires_nanos(&self) -> Option<u64> {
self.expires
}
pub fn expires_millis(&self) -> Option<u64> {
self.expires.map(|t| t / 1e6 as u64)
}
pub fn expires_secs(&self) -> Option<u64> {
self.expires.map(|t| t / 1e9 as u64)
}
pub fn with_len(mut self, input: impl Into<u64>) -> Self {
self.len = input.into();
self
}
#[cfg(feature = "alloc")]
pub fn with_media_type(mut self, input: Option<Cow<'static, str>>) -> Self {
self.media_type = input;
self
}
#[cfg(feature = "std")]
pub fn with_created(mut self, input: impl Into<Option<std::time::SystemTime>>) -> Self {
self.created = input
.into()
.map(|t| t.duration_since(EPOCH).unwrap().as_nanos() as u64);
self
}
pub fn with_created_nanos(mut self, input: impl Into<Option<u64>>) -> Self {
self.created = input.into();
self
}
#[cfg(feature = "std")]
pub fn with_updated(mut self, input: impl Into<Option<std::time::SystemTime>>) -> Self {
self.updated = input
.into()
.map(|t| t.duration_since(EPOCH).unwrap().as_nanos() as u64);
self
}
pub fn with_updated_nanos(mut self, input: impl Into<Option<u64>>) -> Self {
self.updated = input.into();
self
}
#[cfg(feature = "std")]
pub fn with_accessed(mut self, input: impl Into<Option<std::time::SystemTime>>) -> Self {
self.accessed = input
.into()
.map(|t| t.duration_since(EPOCH).unwrap().as_nanos() as u64);
self
}
pub fn with_accessed_nanos(mut self, input: impl Into<Option<u64>>) -> Self {
self.accessed = input.into();
self
}
#[cfg(feature = "std")]
pub fn with_expires(mut self, input: impl Into<Option<std::time::SystemTime>>) -> Self {
self.expires = input
.into()
.map(|t| t.duration_since(EPOCH).unwrap().as_nanos() as u64);
self
}
pub fn with_expires_nanos(mut self, input: impl Into<Option<u64>>) -> Self {
self.expires = input.into();
self
}
}