#[derive(Clone, Debug)]
pub enum K2TreeError {
SmallStemKValue {
stem_k: u8,
},
SmallLeafKValue {
leaf_k: u8,
},
TraverseError {
x: usize,
y: usize
},
OutOfBounds{
x_y: [usize; 2],
min_x_y: [usize; 2],
max_x_y: [usize; 2],
},
StemInsertionError {
pos: usize,
len: usize
},
StemRemovalError {
pos: usize,
len: usize
},
LeafInsertionError {
pos: usize,
len: usize
},
LeafRemovalError {
pos: usize,
len: usize
},
CouldNotShrink {
reason: String
},
CorruptedK2Tree {
source: Box<K2TreeError>
},
Read {
source: Box<K2TreeError>
},
Write {
source: Box<K2TreeError>
},
BitMatrixError {
source: Box<BitMatrixError>,
},
SubRangesError {
source: Box<SubRangesError>,
}
}
impl std::error::Error for K2TreeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use K2TreeError::*;
match self {
CorruptedK2Tree{source} => Some(source),
Read{source} => Some(source),
Write{source} => Some(source),
BitMatrixError{source} => Some(source),
SubRangesError{source} => Some(source),
_ => None,
}
}
}
impl std::fmt::Display for K2TreeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use K2TreeError::*;
match self {
SmallStemKValue{stem_k} => write!(f, "Attempt to create a K2Tree with a stem_k value of {}, which less than the minimum of 2.", stem_k),
SmallLeafKValue{leaf_k} => write!(f, "Attempt to create a K2Tree with a leaf_k value of {}, which less than the minimum of 2.", leaf_k),
TraverseError{x, y} => write!(f, "Error encountered while traversing K2Tree for value at coordinates ({}, {})", x, y),
OutOfBounds {
x_y: [x, y],
min_x_y: [min_x, min_y],
max_x_y: [max_x, max_y]
} => write!(f, "Attempt to access a bit at coordiantes ({}, {}) which are not in the range of the matrix represented by the K2Tree: ({}, {}) -> ({}, {})", x, y, min_x, min_y, max_x, max_y),
StemInsertionError{pos, len} => write!(f, "Could not insert stem of length {} at index {}", len, pos),
StemRemovalError{pos, len} => write!(f, "Could not remove stem of length {} at index {}", len, pos),
LeafInsertionError{pos, len} => write!(f, "Could not insert leaf of length {} at index {}", len, pos),
LeafRemovalError{pos, len} => write!(f, "Could not remove leaf of length {} at index {}", len, pos),
CouldNotShrink{reason} => write!(f, "Could not shrink the matrix a K2Tree represents: {}", reason),
CorruptedK2Tree{source} => write!(f, "The K2Tree's contents are corrupted as a result of the following error: {}", source),
Read{source} => write!(f, "Error during read: {}", source),
Write{source} => write!(f, "Error during write: {}", source),
BitMatrixError{source} => write!(f, "{}", source),
SubRangesError{source} => write!(f, "{}", source),
}
}
}
impl From<BitMatrixError> for K2TreeError {
fn from(error: BitMatrixError) -> Self {
K2TreeError::BitMatrixError {
source: Box::new(error)
}
}
}
impl From<SubRangesError> for K2TreeError {
fn from(error: SubRangesError) -> Self {
K2TreeError::SubRangesError {
source: Box::new(error),
}
}
}
#[derive(Clone, Debug)]
pub enum BitMatrixError {
OutOfBounds {
x_y: [usize; 2],
max_x_y: [usize; 2],
}
}
impl std::error::Error for BitMatrixError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl std::fmt::Display for BitMatrixError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use BitMatrixError::*;
match self {
OutOfBounds {
x_y: [x, y],
max_x_y: [max_x, max_y],
} => write!(f, "Attempts to access a bit at coordinates({}, {}) which are not in the range of the matrix: (0, 0) -> ({}, {})", x, y, max_x, max_y),
}
}
}
#[derive(Clone, Debug)]
pub enum SubRangesError {
CannotSubdivideRange {
range: [[usize; 2]; 2],
horizontal_subdivisions: usize,
vertical_subdivisions: usize,
}
}
impl std::error::Error for SubRangesError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl std::fmt::Display for SubRangesError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use SubRangesError::*;
match self {
CannotSubdivideRange {
range: [[min_x, max_x], [min_y, max_y]],
horizontal_subdivisions: hs,
vertical_subdivisions: vs,
} => write!(f, "The 2D range ({}, {}) -> ({}, {}) could not be evenly subdivided {} times horizontally or {} times vertically.", min_x, min_y, max_x, max_y, hs, vs),
}
}
}