use core::fmt;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Size {
rows: u16,
cols: u16,
}
impl Size {
pub const MAX_DIMENSION: u16 = i16::MAX as u16;
pub const fn try_new(cols: u16, rows: u16) -> Result<Self> {
if rows == 0 || cols == 0 || rows > Self::MAX_DIMENSION || cols > Self::MAX_DIMENSION {
return Err(Error::invalid_size(rows, cols));
}
Ok(Self { rows, cols })
}
#[must_use]
pub const fn rows(&self) -> u16 {
self.rows
}
#[must_use]
pub const fn cols(&self) -> u16 {
self.cols
}
#[must_use]
#[cfg(any(feature = "blocking", feature = "tokio", test))]
pub(super) const fn to_i16_pair(self) -> (i16, i16) {
(
i16::from_ne_bytes(self.cols.to_ne_bytes()),
i16::from_ne_bytes(self.rows.to_ne_bytes()),
)
}
}
impl Default for Size {
fn default() -> Self {
Self { rows: 24, cols: 80 }
}
}
impl fmt::Display for Size {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.cols, self.rows)
}
}
#[cfg(test)]
pub(super) fn test_size(rows: u16, cols: u16) -> Size {
Size::try_new(cols, rows).expect("the hard-coded test size is valid")
}
#[cfg(test)]
#[path = "size_tests.rs"]
mod tests;