Skip to main content

lindera_dictionary/
error.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
6pub enum LinderaErrorKind {
7    Args,
8    Content,
9    Decode,
10    Deserialize,
11    Io,
12    Parse,
13    Serialize,
14    NotFound,
15    Build,
16    Dictionary,
17    Mode,
18    FeatureDisabled,
19}
20
21impl LinderaErrorKind {
22    pub fn with_error<E>(self, source: E) -> LinderaError
23    where
24        anyhow::Error: From<E>,
25    {
26        LinderaError {
27            kind: self,
28            source: From::from(source),
29        }
30    }
31}
32
33#[derive(thiserror::Error, Debug)]
34#[error("LinderaError(kind={kind:?}, source={source})")]
35pub struct LinderaError {
36    pub kind: LinderaErrorKind,
37    #[source]
38    source: anyhow::Error,
39}
40
41impl LinderaError {
42    pub fn add_context<C>(self, ctx: C) -> Self
43    where
44        C: fmt::Display + Send + Sync + 'static,
45    {
46        LinderaError {
47            kind: self.kind,
48            source: self.source.context(ctx),
49        }
50    }
51
52    pub fn kind(&self) -> LinderaErrorKind {
53        self.kind
54    }
55}