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