1use std::fmt;
2
3use crate::input::SourceFormat;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum ToonifyError {
8 #[error("failed to read input: {0}")]
9 Io(#[from] std::io::Error),
10 #[error("{format:?} parsing error: {message}")]
11 Parse {
12 format: SourceFormat,
13 message: String,
14 },
15 #[error("number normalization error for `{value}`: {source}")]
16 NumberNormalization {
17 value: String,
18 #[source]
19 source: Box<dyn std::error::Error + Send + Sync>,
20 },
21 #[error("XML decoding error: {0}")]
22 Xml(String),
23 #[error("{0}")]
24 Encoding(String),
25 #[error("{0}")]
26 Decoding(String),
27 #[error("tokenization error: {0}")]
28 Tokenizer(String),
29}
30
31impl ToonifyError {
32 pub(crate) fn parse_err(
33 format: SourceFormat,
34 err: impl std::error::Error + Send + Sync + 'static,
35 ) -> Self {
36 Self::Parse {
37 format,
38 message: err.to_string(),
39 }
40 }
41
42 pub(crate) fn encoding(msg: impl fmt::Display) -> Self {
43 Self::Encoding(msg.to_string())
44 }
45
46 pub(crate) fn decoding(msg: impl fmt::Display) -> Self {
47 Self::Decoding(msg.to_string())
48 }
49
50 pub(crate) fn tokenizer(msg: impl fmt::Display) -> Self {
51 Self::Tokenizer(msg.to_string())
52 }
53}