use core::fmt;
use calandria::RetainedBytes;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TransportPressure {
inbound: RetainedBytes,
outbound: RetainedBytes,
plaintext: RetainedBytes,
protocol: RetainedBytes,
total: RetainedBytes,
}
impl TransportPressure {
pub const ZERO: Self = Self {
inbound: RetainedBytes::ZERO,
outbound: RetainedBytes::ZERO,
plaintext: RetainedBytes::ZERO,
protocol: RetainedBytes::ZERO,
total: RetainedBytes::ZERO,
};
pub const MAX: Self = Self {
inbound: RetainedBytes::ZERO,
outbound: RetainedBytes::ZERO,
plaintext: RetainedBytes::ZERO,
protocol: RetainedBytes::new(u64::MAX),
total: RetainedBytes::new(u64::MAX),
};
pub const fn new(
inbound: RetainedBytes,
outbound: RetainedBytes,
plaintext: RetainedBytes,
protocol: RetainedBytes,
) -> Result<Self, TransportPressureError> {
let Some(total) = inbound.checked_add(outbound) else {
return Err(TransportPressureError);
};
let Some(total) = total.checked_add(plaintext) else {
return Err(TransportPressureError);
};
let Some(total) = total.checked_add(protocol) else {
return Err(TransportPressureError);
};
Ok(Self {
inbound,
outbound,
plaintext,
protocol,
total,
})
}
pub const fn inbound(self) -> RetainedBytes {
self.inbound
}
pub const fn outbound(self) -> RetainedBytes {
self.outbound
}
pub const fn plaintext(self) -> RetainedBytes {
self.plaintext
}
pub const fn protocol(self) -> RetainedBytes {
self.protocol
}
pub const fn total(self) -> RetainedBytes {
self.total
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct TransportPressureError;
impl fmt::Display for TransportPressureError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("transport retained-byte pressure overflowed")
}
}
impl core::error::Error for TransportPressureError {}