Skip to main content

lindera_core/
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    Compress,
15    DictionaryNotFound,
16    DictionaryLoadError,
17    DictionaryBuildError,
18    DictionaryKindError,
19    DictionarySourceTypeError,
20    ModeError,
21    MissingTokenDetail,
22}
23
24impl LinderaErrorKind {
25    pub fn with_error<E>(self, source: E) -> LinderaError
26    where
27        anyhow::Error: From<E>,
28    {
29        LinderaError {
30            kind: self,
31            source: From::from(source),
32        }
33    }
34}
35
36#[derive(thiserror::Error, Debug)]
37#[error("LinderaError(kind={kind:?}, source={source})")]
38pub struct LinderaError {
39    pub kind: LinderaErrorKind,
40    #[source]
41    source: anyhow::Error,
42}
43
44impl LinderaError {
45    pub fn add_context<C>(self, ctx: C) -> Self
46    where
47        C: fmt::Display + Send + Sync + 'static,
48    {
49        LinderaError {
50            kind: self.kind,
51            source: self.source.context(ctx),
52        }
53    }
54
55    pub fn kind(&self) -> LinderaErrorKind {
56        self.kind
57    }
58}