use core::{error, fmt, str};
use crate::MAX_LENGTH;
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
pub enum TkStrError {
TooBig(usize),
UnicodeError(str::Utf8Error),
OutOfBounds(usize),
TooMany(usize),
}
impl fmt::Display for TkStrError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
| Self::TooBig(n) => write!(
f,
"TokenString would be bigger than MAX_LENGTH, {n} > \
{MAX_LENGTH}"
),
| Self::UnicodeError(utf8_error) =>
write!(f, "encoding TokenString: {utf8_error}"),
| Self::OutOfBounds(i) => write!(f, "index {i} is out of bounds"),
| Self::TooMany(n) => write!(
f,
"too many strings concatenated, Builder has been configured \
for {n} strings"
),
}
}
}
impl error::Error for TkStrError {}
#[cfg(test)]
mod err_tests {
#![expect(
clippy::std_instead_of_alloc,
reason = "We are testing, this needs std"
)]
extern crate std;
use std::string::ToString as _;
use assert2::check;
use crate::TkStrError;
#[test]
fn string_too_big() {
check!(
TkStrError::TooBig(5).to_string()
== "TokenString would be bigger than MAX_LENGTH, 5 > 65535"
);
}
#[test]
fn string_out_of_bounds() {
check!(
TkStrError::OutOfBounds(5).to_string()
== "index 5 is out of bounds"
);
}
#[test]
fn string_too_many() {
check!(
TkStrError::TooMany(5).to_string()
== "too many strings concatenated, Builder has been \
configured for 5 strings"
);
}
}