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