Skip to content

Commit

Permalink
Merge pull request #43 from matt-duch/error-return-val
Browse files Browse the repository at this point in the history
OverlapError now contains the attempted inserted value
  • Loading branch information
ripytide authored Dec 26, 2023
2 parents 77ace3f + 36997d9 commit 509080c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
56 changes: 31 additions & 25 deletions src/discrete_range_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ pub struct DiscreteRangeMap<I, K, V> {
phantom: PhantomData<I>,
}

/// An error type to represent a range overlapping another range when
/// it should not have.
/// The error returned when inserting a range that overlaps another range when
/// it should not have. Contains the value that was not inserted.
#[derive(PartialEq, Debug)]
pub struct OverlapError;
pub struct OverlapError<V> {
/// The value which was not inserted, because of the overlap error.
pub value: V,
}

/// A compatibility type used in [`RangeType`] for allowing the library to
/// create the custom K type used in the map when necessary.
Expand Down Expand Up @@ -768,18 +771,21 @@ where
/// let mut map = DiscreteRangeMap::new();
///
/// assert_eq!(map.insert_strict(ie(5, 10), 9), Ok(()));
/// assert_eq!(map.insert_strict(ie(5, 10), 2), Err(OverlapError));
/// assert_eq!(
/// map.insert_strict(ie(5, 10), 2),
/// Err(OverlapError { value: 2 })
/// );
/// assert_eq!(map.len(), 1);
/// ```
pub fn insert_strict(
&mut self,
range: K,
value: V,
) -> Result<(), OverlapError> {
) -> Result<(), OverlapError<V>> {
invalid_range_panic(range);

if self.overlaps(range) {
return Err(OverlapError);
return Err(OverlapError { value });
}

self.insert_unchecked(range, value);
Expand Down Expand Up @@ -877,7 +883,7 @@ where
/// // Overlapping
/// assert_eq!(
/// map.insert_merge_touching(ie(4, 8), false),
/// Err(OverlapError),
/// Err(OverlapError { value: false }),
/// );
///
/// // Neither Touching or Overlapping
Expand All @@ -895,11 +901,11 @@ where
&mut self,
range: K,
value: V,
) -> Result<K, OverlapError> {
) -> Result<K, OverlapError<V>> {
invalid_range_panic(range);

if self.overlaps(range) {
return Err(OverlapError);
return Err(OverlapError { value });
}

Ok(self.insert_merge_with_comps(
Expand Down Expand Up @@ -965,7 +971,7 @@ where
/// // Overlapping
/// assert_eq!(
/// map.insert_merge_touching_if_values_equal(ie(4, 8), false),
/// Err(OverlapError),
/// Err(OverlapError { value: false }),
/// );
///
/// // Neither Touching or Overlapping
Expand All @@ -983,14 +989,14 @@ where
&mut self,
range: K,
value: V,
) -> Result<K, OverlapError>
) -> Result<K, OverlapError<V>>
where
V: Eq,
{
invalid_range_panic(range);

if self.overlaps(range) {
return Err(OverlapError);
return Err(OverlapError { value });
}

let get_start = |selfy: &Self, value: &V| {
Expand Down Expand Up @@ -1260,7 +1266,7 @@ where
/// ```
pub fn from_slice_strict<const N: usize>(
slice: [(K, V); N],
) -> Result<DiscreteRangeMap<I, K, V>, OverlapError> {
) -> Result<DiscreteRangeMap<I, K, V>, OverlapError<V>> {
let mut map = DiscreteRangeMap::new();
for (range, value) in slice {
map.insert_strict(range, value)?;
Expand Down Expand Up @@ -1296,7 +1302,7 @@ where
/// ```
pub fn from_iter_strict(
iter: impl Iterator<Item = (K, V)>,
) -> Result<DiscreteRangeMap<I, K, V>, OverlapError> {
) -> Result<DiscreteRangeMap<I, K, V>, OverlapError<V>> {
let mut map = DiscreteRangeMap::new();
for (range, value) in iter {
map.insert_strict(range, value)?;
Expand Down Expand Up @@ -1511,16 +1517,16 @@ pub trait InclusiveRange<I> {
/// The end of the range, inclusive.
fn end(&self) -> I;

/// Does the range contain the given point?
/// Does the range contain the given point?
fn contains(&self, point: I) -> bool
where
I: PointType,
{
point >= self.start() && point <= self.end()
}

/// Is the range is valid, which according to this crate means `start()`
/// <= `end()`
/// Is the range is valid, which according to this crate means `start()`
/// <= `end()`
fn is_valid(&self) -> bool
where
I: PointType,
Expand Down Expand Up @@ -1740,19 +1746,19 @@ mod tests {
assert_insert_strict(
basic(),
(ii(0, 4), false),
Err(OverlapError),
Err(OverlapError { value: false }),
basic_slice(),
);
assert_insert_strict(
basic(),
(ii(5, 6), false),
Err(OverlapError),
Err(OverlapError { value: false }),
basic_slice(),
);
assert_insert_strict(
basic(),
(ii(4, 5), true),
Err(OverlapError),
Err(OverlapError { value: true }),
basic_slice(),
);
assert_insert_strict(
Expand All @@ -1771,7 +1777,7 @@ mod tests {
fn assert_insert_strict<const N: usize>(
mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>,
to_insert: (InclusiveInterval<i8>, bool),
result: Result<(), OverlapError>,
result: Result<(), OverlapError<bool>>,
after: [(InclusiveInterval<i8>, bool); N],
) {
assert_eq!(before.insert_strict(to_insert.0, to_insert.1), result);
Expand Down Expand Up @@ -1988,7 +1994,7 @@ mod tests {
assert_insert_merge_touching(
basic(),
(ii(0, 4), false),
Err(OverlapError),
Err(OverlapError { value: false }),
[
(ui(4), false),
(ee(5, 7), true),
Expand Down Expand Up @@ -2028,7 +2034,7 @@ mod tests {
fn assert_insert_merge_touching<const N: usize>(
mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>,
to_insert: (InclusiveInterval<i8>, bool),
result: Result<InclusiveInterval<i8>, OverlapError>,
result: Result<InclusiveInterval<i8>, OverlapError<bool>>,
after: [(InclusiveInterval<i8>, bool); N],
) {
assert_eq!(
Expand All @@ -2042,7 +2048,7 @@ mod tests {
assert_insert_merge_touching_if_values_equal(
basic(),
(ii(0, 4), false),
Err(OverlapError),
Err(OverlapError { value: false }),
basic_slice(),
);
dbg!("hererere");
Expand Down Expand Up @@ -2084,7 +2090,7 @@ mod tests {
fn assert_insert_merge_touching_if_values_equal<const N: usize>(
mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>,
to_insert: (InclusiveInterval<i8>, bool),
result: Result<InclusiveInterval<i8>, OverlapError>,
result: Result<InclusiveInterval<i8>, OverlapError<bool>>,
after: [(InclusiveInterval<i8>, bool); N],
) {
assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions src/discrete_range_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ where
self.inner.contains_range(range)
}
/// See [`DiscreteRangeMap::insert_strict()`] for more details.
pub fn insert_strict(&mut self, range: K) -> Result<(), OverlapError> {
pub fn insert_strict(&mut self, range: K) -> Result<(), OverlapError<()>> {
self.inner.insert_strict(range, ())
}
/// See [`DiscreteRangeMap::insert_merge_touching()`] for more details.
pub fn insert_merge_touching(
&mut self,
range: K,
) -> Result<K, OverlapError> {
) -> Result<K, OverlapError<()>> {
self.inner.insert_merge_touching(range, ())
}
/// See [`DiscreteRangeMap::insert_merge_overlapping()`] for more details.
Expand All @@ -135,7 +135,7 @@ where
/// See [`DiscreteRangeMap::from_slice_strict()`] for more details.
pub fn from_slice_strict<const N: usize>(
slice: [K; N],
) -> Result<DiscreteRangeSet<I, K>, OverlapError> {
) -> Result<DiscreteRangeSet<I, K>, OverlapError<()>> {
let mut set = DiscreteRangeSet::new();
for range in slice {
set.insert_strict(range)?;
Expand All @@ -145,7 +145,7 @@ where
/// See [`DiscreteRangeMap::from_iter_strict()`] for more details.
pub fn from_iter_strict(
iter: impl Iterator<Item = K>,
) -> Result<DiscreteRangeSet<I, K>, OverlapError> {
) -> Result<DiscreteRangeSet<I, K>, OverlapError<()>> {
let mut set = DiscreteRangeSet::new();
for range in iter {
set.insert_strict(range)?;
Expand Down

0 comments on commit 509080c

Please sign in to comment.