use crate::util::buffer::Buffer;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct BufferSize<const MIN_SIZE: usize = 249, const DEFAULT_SIZE: usize = 2048> {
size: usize,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum BufferSizeError {
TooSmall(usize, usize),
}
impl<const MIN_SIZE: usize, const DEFAULT_SIZE: usize> BufferSize<MIN_SIZE, DEFAULT_SIZE> {
pub const MIN: usize = MIN_SIZE;
pub const DEFAULT: usize = DEFAULT_SIZE;
pub(crate) fn create_buffer(&self) -> Buffer {
Buffer::new(self.size)
}
pub fn value(&self) -> usize {
self.size
}
pub fn min() -> Self {
Self { size: Self::MIN }
}
pub fn new(size: usize) -> Result<Self, BufferSizeError> {
if size < Self::MIN {
return Err(BufferSizeError::TooSmall(size, Self::MIN));
}
Ok(Self { size })
}
}
impl<const MIN_SIZE: usize, const DEFAULT_SIZE: usize> Default
for BufferSize<MIN_SIZE, DEFAULT_SIZE>
{
fn default() -> Self {
Self {
size: Self::DEFAULT,
}
}
}
impl std::fmt::Display for BufferSizeError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::TooSmall(provided_size, min_size) => write!(
f,
"provided size {provided_size} is less than the minimum allowed size of {min_size}",
),
}
}
}
impl std::error::Error for BufferSizeError {}