use core::fmt;
use crate::MAX_LENGTH;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CreateError {
Order {
zero: char,
first: char,
last: char,
},
Character(char),
Length {
ln: usize,
lz: usize,
},
Threshold(f64),
FewCharacters(usize),
Unsorted(usize),
}
impl fmt::Display for CreateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Order { zero, first, last } => {
write!(
f,
"The characters must be zero < first <= last (got '{zero}', '{first}', '{last}')."
)
}
Self::Character(ch) => write!(
f,
"The character U+{:04X} can't be used: use characters up to U+FFFF and leave the surrogates (U+D800 to U+DFFF) out of the range.",
*ch as u32
),
Self::Length { ln, lz } => write!(
f,
"The lengths must be ln >= 1, lz >= 1 and ln + lz <= {MAX_LENGTH} (got ln = {ln}, lz = {lz})."
),
Self::Threshold(factor) => write!(
f,
"The threshold factor must be from 0 to 1 (got {factor})."
),
Self::FewCharacters(count) => write!(
f,
"A character set needs the zero and at least one digit (got {count} characters)."
),
Self::Unsorted(index) => write!(
f,
"The characters of a set must be sorted by their code without repeating any (the character at index {index} is not greater than the one before it)."
),
}
}
}
impl std::error::Error for CreateError {}