use crate::error::{Error, Result};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CellSize {
x: f64,
y: f64,
}
impl CellSize {
#[inline]
pub fn new(x: f64, y: f64) -> Result<Self> {
if x.is_finite() && y.is_finite() && x > 0.0 && y > 0.0 {
Ok(Self { x, y })
} else {
Err(Error::InvalidCellSize { x, y })
}
}
#[inline]
pub fn square(size: f64) -> Result<Self> {
Self::new(size, size)
}
#[inline]
pub fn x(&self) -> f64 {
self.x
}
#[inline]
pub fn y(&self) -> f64 {
self.y
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_square() {
let cs = CellSize::square(30.0).unwrap();
assert_eq!(cs.x(), 30.0);
assert_eq!(cs.y(), 30.0);
}
#[test]
fn test_non_square() {
let cs = CellSize::new(25.0, 30.0).unwrap();
assert_eq!(cs.x(), 25.0);
assert_eq!(cs.y(), 30.0);
}
#[test]
fn rejects_invalid_dimensions() {
for (x, y) in [
(0.0, 1.0),
(1.0, 0.0),
(-1.0, 1.0),
(1.0, -1.0),
(f64::NAN, 1.0),
(1.0, f64::NAN),
(f64::INFINITY, 1.0),
(1.0, f64::NEG_INFINITY),
] {
assert!(CellSize::new(x, y).is_err(), "accepted ({x}, {y})");
}
assert!(CellSize::square(0.0).is_err());
}
}