use crate::error::{PyrucastError, Result};
#[derive(Clone, Copy)]
pub struct Band {
min: Option<f64>,
max: Option<f64>,
strict_min: bool,
strict_max: bool,
}
impl Band {
pub fn new(ge: Option<f64>, gt: Option<f64>, le: Option<f64>, lt: Option<f64>) -> Result<Self> {
let (min, strict_min) = match (ge, gt) {
(Some(_), Some(_)) => {
return Err(PyrucastError::Message(
"value band: give at most one lower bound (`ge` or `gt`, not both)".into(),
));
}
(Some(v), None) => (Some(v), false),
(None, Some(v)) => (Some(v), true),
(None, None) => (None, false),
};
let (max, strict_max) = match (le, lt) {
(Some(_), Some(_)) => {
return Err(PyrucastError::Message(
"value band: give at most one upper bound (`le` or `lt`, not both)".into(),
));
}
(Some(v), None) => (Some(v), false),
(None, Some(v)) => (Some(v), true),
(None, None) => (None, false),
};
if min.is_none() && max.is_none() {
return Err(PyrucastError::Message(
"value band: at least one bound (`ge`/`gt`/`le`/`lt`) must be given".into(),
));
}
if let (Some(lo), Some(hi)) = (min, max) {
if lo > hi {
return Err(PyrucastError::Message(format!(
"value band: lower bound ({lo}) is greater than upper bound ({hi})"
)));
}
}
Ok(Band {
min,
max,
strict_min,
strict_max,
})
}
pub fn contains(&self, v: f64) -> bool {
let lo_ok = self
.min
.is_none_or(|lo| if self.strict_min { v > lo } else { v >= lo });
let hi_ok = self
.max
.is_none_or(|hi| if self.strict_max { v < hi } else { v <= hi });
lo_ok && hi_ok
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inclusive_vs_strict() {
let b = Band::new(Some(1.0), None, Some(3.0), None).unwrap();
assert!(b.contains(1.0) && b.contains(3.0) && b.contains(2.0));
assert!(!b.contains(0.999) && !b.contains(3.001));
let b = Band::new(None, Some(1.0), None, Some(3.0)).unwrap();
assert!(!b.contains(1.0) && !b.contains(3.0));
assert!(b.contains(2.0));
}
#[test]
fn open_sides() {
assert!(Band::new(Some(0.0), None, None, None)
.unwrap()
.contains(1e9));
assert!(Band::new(None, None, Some(0.0), None)
.unwrap()
.contains(-1e9));
}
#[test]
fn rejects_bad_bounds() {
assert!(Band::new(None, None, None, None).is_err());
assert!(Band::new(Some(5.0), None, Some(1.0), None).is_err());
assert!(Band::new(Some(0.0), Some(0.0), None, None).is_err());
assert!(Band::new(None, None, Some(0.0), Some(0.0)).is_err());
}
}