use super::{ContentError, Size};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AreaValues {
pub x1: u16,
pub y1: u16,
pub x2: u16,
pub y2: u16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Area(AreaValues);
impl Area {
#[must_use]
pub const fn left(&self) -> u16 {
self.0.x1
}
#[must_use]
pub const fn top(&self) -> u16 {
self.0.y1
}
#[must_use]
pub const fn width(&self) -> u16 {
self.0.x2 + 1 - self.0.x1
}
#[must_use]
pub const fn height(&self) -> u16 {
self.0.y2 + 1 - self.0.y1
}
#[must_use]
pub fn size(&self) -> Size {
Size {
w: usize::from(self.width()),
h: usize::from(self.height()),
}
}
}
impl TryFrom<AreaValues> for Area {
type Error = ContentError;
fn try_from(coords_value: AreaValues) -> Result<Self, Self::Error> {
if coords_value.x2 <= coords_value.x1 || coords_value.y2 <= coords_value.y1 {
Err(ContentError::InvalidAreaBounding)
} else {
Ok(Self(coords_value))
}
}
}