use core::fmt;
use core::ops::RangeBounds;
#[cfg(feature = "dev")]
use arbitrary::Arbitrary;
use crate::prelude::*;
mod willow_range;
pub use willow_range::*;
mod keylike;
pub use keylike::*;
mod coordinatelike;
pub use coordinatelike::*;
mod namespaced;
pub use namespaced::*;
mod range_3d;
pub use range_3d::*;
mod area;
pub use area::*;
mod area_of_interest;
pub use area_of_interest::*;
pub mod private_context;
pub type SubspaceRange = WillowRange<SubspaceId>;
pub type PathRange = WillowRange<Path>;
pub type TimeRange = WillowRange<Timestamp>;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "dev", derive(Arbitrary))]
pub struct EmptyGrouping;
impl fmt::Display for EmptyGrouping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"An operation would have resulted in an empty grouping of Willow entries."
)
}
}
impl core::error::Error for EmptyGrouping {}
pub trait Grouping: PartialOrd + Sized {
fn includes<Coord>(&self, coord: &Coord) -> bool
where
Coord: Coordinatelike + ?Sized;
fn includes_grouping(&self, other: &Self) -> bool {
self >= other
}
fn strictly_includes_grouping(&self, other: &Self) -> bool {
self > other
}
fn intersection(&self, other: &Self) -> Result<Self, EmptyGrouping>;
fn includes_in_intersection<Coord>(&self, other: &Self, coord: &Coord) -> bool
where
Coord: Coordinatelike + ?Sized,
{
self.includes(coord) && other.includes(coord)
}
}
pub fn subspace_includes_subspace<S: PartialEq>(outer: Option<&S>, inner: Option<&S>) -> bool {
match (outer, inner) {
(Some(s1), Some(s2)) => s1 == s2,
(Some(_), None) => false,
_ => true,
}
}
impl Grouping for SubspaceRange {
fn includes<Coord>(&self, coord: &Coord) -> bool
where
Coord: Coordinatelike + ?Sized,
{
self.contains(coord.subspace_id())
}
fn intersection(&self, other: &Self) -> Result<Self, EmptyGrouping> {
self.intersection_willow_range(other)
}
}
impl Grouping for PathRange {
fn includes<Coord>(&self, coord: &Coord) -> bool
where
Coord: Coordinatelike + ?Sized,
{
self.contains(coord.path())
}
fn intersection(&self, other: &Self) -> Result<Self, EmptyGrouping> {
self.intersection_willow_range(other)
}
}
impl Grouping for TimeRange {
fn includes<Coord>(&self, coord: &Coord) -> bool
where
Coord: Coordinatelike + ?Sized,
{
self.contains(&coord.timestamp())
}
fn intersection(&self, other: &Self) -> Result<Self, EmptyGrouping> {
self.intersection_willow_range(other)
}
}