texter/
error.rs

1use std::{fmt::Display, num::NonZeroUsize};
2
3/// A type alias for the libraries result type. ([`Result<(), Error>`])
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// The error type returned upon failed conversions and edits across the library.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum Error {
10    OutOfBoundsRow { max: usize, current: usize },
11    InBetweenCharBoundries { encoding: Encoding },
12}
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum Encoding {
16    UTF8,
17    UTF16,
18    UTF32,
19}
20
21impl Display for Error {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::OutOfBoundsRow { max, current } => {
25                write!(f, "Current max row index is {max}, {current} was provided.")
26            }
27            Self::InBetweenCharBoundries { encoding } => {
28                write!(
29                    f,
30                    "Provided column position is between char boundries for {encoding:?}."
31                )
32            }
33        }
34    }
35}
36
37impl Error {
38    #[inline]
39    pub(crate) fn oob_row(row_count: NonZeroUsize, current: usize) -> Self {
40        Self::OutOfBoundsRow {
41            max: row_count.get() - 1,
42            current,
43        }
44    }
45}
46
47impl std::error::Error for Error {}