json_gettext/
json_get_text_build_errors.rs1use std::{
2 error::Error,
3 fmt::{Display, Error as FmtError, Formatter},
4 io,
5};
6
7use crate::{Key, serde_json::Error as JSONError};
8
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum JSONGetTextBuildError {
12 DefaultKeyNotFound,
13 TextInKeyNotInDefaultKey {
14 key: Key,
15 text: String,
16 },
17 DuplicatedKey(Key),
18 NotObject,
20 IOError(io::Error),
21 SerdeJSONError(JSONError),
22}
23
24impl Display for JSONGetTextBuildError {
25 #[inline]
26 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
27 match self {
28 JSONGetTextBuildError::DefaultKeyNotFound => {
29 f.write_str("The default key is not found.")
30 },
31 JSONGetTextBuildError::TextInKeyNotInDefaultKey {
32 key,
33 text,
34 } => f.write_fmt(format_args!(
35 "The text `{}` in the key `{}` is not found in the default key.",
36 text, key
37 )),
38 JSONGetTextBuildError::DuplicatedKey(key) => {
39 f.write_fmt(format_args!("The key `{key}` is duplicated."))
40 },
41 JSONGetTextBuildError::NotObject => {
42 f.write_str("The added value does not represent a JSON map object.")
43 },
44 JSONGetTextBuildError::IOError(err) => Display::fmt(err, f),
45 JSONGetTextBuildError::SerdeJSONError(err) => Display::fmt(err, f),
46 }
47 }
48}
49
50impl Error for JSONGetTextBuildError {
51 #[inline]
52 fn source(&self) -> Option<&(dyn Error + 'static)> {
53 match self {
54 JSONGetTextBuildError::IOError(err) => Some(err),
55 JSONGetTextBuildError::SerdeJSONError(err) => Some(err),
56 _ => None,
57 }
58 }
59}
60
61impl From<io::Error> for JSONGetTextBuildError {
62 #[inline]
63 fn from(v: io::Error) -> JSONGetTextBuildError {
64 JSONGetTextBuildError::IOError(v)
65 }
66}
67
68impl From<JSONError> for JSONGetTextBuildError {
69 #[inline]
70 fn from(v: JSONError) -> JSONGetTextBuildError {
71 JSONGetTextBuildError::SerdeJSONError(v)
72 }
73}