text_utils/
errors.rs

1use std::{
2    error::Error,
3    fmt::{self, Debug, Display, Formatter},
4};
5
6/// Result of text progress
7pub type Result<T> = std::result::Result<T, TextError>;
8
9/// Error of text progress
10#[allow(missing_docs)]
11#[derive(Debug, Clone, Eq, PartialEq)]
12pub enum TextError {
13    UnescapeError { offset: usize, chars: String },
14}
15
16impl TextError {
17    /// Constructor of [`TextError::UnescapeError`]
18    pub fn unescape_error<T>(offset: usize, msg: impl Into<String>) -> Result<T> {
19        Err(Self::UnescapeError { offset, chars: msg.into() })
20    }
21}
22
23impl Error for TextError {}
24
25impl Display for TextError {
26    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
27        match self {
28            TextError::UnescapeError { offset, chars: message } => {
29                write!(f, "UnescapeError: Fail to unescape `{}` at position {}", message, offset)
30            }
31        }
32    }
33}