use crate::Result;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Tolerance {
pub lower: f64,
pub upper: f64,
}
impl Tolerance {
pub fn new_unchecked(lower: f64, upper: f64) -> Self {
Self { lower, upper }
}
pub fn try_new(lower: f64, upper: f64) -> Result<Self> {
if lower <= upper {
Ok(Self { lower, upper })
} else {
Err("Invalid tolerance zone bounds".into())
}
}
pub fn symmetrical(center: f64, half_width: f64) -> Self {
Self {
lower: center - half_width.abs(),
upper: center + half_width.abs(),
}
}
pub fn conforms(&self, x: f64) -> bool {
x >= self.lower && x <= self.upper
}
pub fn size(&self) -> f64 {
self.upper - self.lower
}
pub fn center(&self) -> f64 {
(self.upper + self.lower) / 2.0
}
}