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