serde_json_schema/
error.rs

1//! Errors 🤷
2
3use std::fmt;
4
5#[derive(Clone, Copy, Debug)]
6pub struct InvalidFragment;
7
8#[derive(Clone, Copy, Debug)]
9pub struct InvalidPath;
10
11/// Convenient wrapper around `std::Result`.
12pub type Result<T> = ::std::result::Result<T, Error>;
13
14/// The Error type.
15#[derive(Debug)]
16pub struct Error {
17    kind: ErrorKind,
18}
19
20impl std::error::Error for Error {}
21
22impl fmt::Display for Error {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match self.kind {
25            ErrorKind::SerdeJson(ref e) => write!(f, "{}", e),
26        }
27    }
28}
29
30/// The kind of an error.
31#[derive(Debug)]
32pub enum ErrorKind {
33    SerdeJson(serde_json::Error),
34}
35
36impl From<serde_json::Error> for Error {
37    fn from(e: serde_json::Error) -> Error {
38        Error {
39            kind: ErrorKind::SerdeJson(e),
40        }
41    }
42}
43
44impl From<ErrorKind> for Error {
45    fn from(kind: ErrorKind) -> Error {
46        Error { kind }
47    }
48}
49
50/// Just the usual bail macro
51#[macro_export]
52#[doc(hidden)]
53macro_rules! bail {
54    ($e:expr) => {
55        return Err($e.into());
56    };
57    ($fmt:expr, $($arg:tt)+) => {
58        return Err(format!($fmt, $($arg)+).into());
59    };
60}
61
62/// Exits a function early with an `Error` if the condition is not satisfied.
63///
64/// Similar to `assert!`, `ensure!` takes a condition and exits the function
65/// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error`,
66/// it does not panic.
67#[macro_export(local_inner_macros)]
68#[doc(hidden)]
69macro_rules! ensure {
70    ($cond:expr, $e:expr) => {
71        if !($cond) {
72            bail!($e);
73        }
74    };
75    ($cond:expr, $fmt:expr, $($arg:tt)*) => {
76        if !($cond) {
77            bail!($fmt, $($arg)*);
78        }
79    };
80}