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}
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}