fenwick_tree/
errors.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter, Result as FmtResult};
3use std::ops::Bound;
4
5/// An error in calculating a partial sum.
6#[derive(Debug, PartialEq, Eq)]
7pub enum SumError {
8    // Range is not within the range of the tree.
9    OutOfRange {
10        bounds: (Bound<usize>, Bound<usize>),
11        len: usize,
12    },
13}
14
15/// An error in adding a delta to a tree element.
16#[derive(Debug, PartialEq, Eq)]
17pub enum AddError {
18    /// Index is greater than the size of the tree.
19    IndexOutOfRange { index: usize, size: usize },
20}
21
22impl Display for SumError {
23    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
24        match *self {
25            SumError::OutOfRange { bounds, len } => {
26                write!(f, "Bounds {:#?} are not in range (0..{})", bounds, len)
27            }
28        }
29    }
30}
31
32impl Display for AddError {
33    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
34        match *self {
35            AddError::IndexOutOfRange { index, size } => {
36                write!(f, "Index `{}` is greater than the size `{}`", index, size)
37            }
38        }
39    }
40}
41
42impl Error for SumError {
43    fn source(&self) -> Option<&(dyn Error + 'static)> {
44        None
45    }
46}
47
48impl Error for AddError {
49    fn source(&self) -> Option<&(dyn Error + 'static)> {
50        None
51    }
52}