use bytes::Bytes;
use chrono::{DateTime, Utc};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValueBytes(Bytes);
impl ValueBytes {
#[must_use]
pub fn new(bytes: impl Into<Bytes>) -> Self {
Self(bytes.into())
}
#[must_use]
pub fn as_slice(&self) -> &[u8] {
&self.0
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn into_inner(self) -> Bytes {
self.0
}
}
impl From<Vec<u8>> for ValueBytes {
fn from(value: Vec<u8>) -> Self {
Self::new(value)
}
}
impl From<Bytes> for ValueBytes {
fn from(value: Bytes) -> Self {
Self::new(value)
}
}
impl AsRef<[u8]> for ValueBytes {
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValueMetadata {
pub codec: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub expires_at: Option<DateTime<Utc>>,
}
impl ValueMetadata {
#[must_use]
pub fn new(
codec: impl Into<String>,
now: DateTime<Utc>,
expires_at: Option<DateTime<Utc>>,
) -> Self {
Self {
codec: codec.into(),
created_at: now,
updated_at: now,
expires_at,
}
}
#[must_use]
pub fn is_expired_at(&self, now: DateTime<Utc>) -> bool {
self.expires_at.is_some_and(|expires_at| expires_at <= now)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoredValue {
pub bytes: ValueBytes,
pub metadata: ValueMetadata,
}
impl StoredValue {
#[must_use]
pub fn new(bytes: ValueBytes, metadata: ValueMetadata) -> Self {
Self { bytes, metadata }
}
#[must_use]
pub fn is_expired(&self) -> bool {
self.metadata.is_expired_at(Utc::now())
}
}