openapi_types/
error.rs

1use crate::yaml::YamlLoaderError;
2use crate::{json_schema, v3_0};
3use std::fmt::Debug;
4
5pub type Result<A> = std::result::Result<A, Error>;
6
7#[derive(Debug)]
8pub enum Error {
9    Enclosed { key: String, causes: Vec<Error> },
10    Multiple { causes: Vec<Error> },
11    SpecViolation(SpecViolation),
12    Unsupported(Unsupported),
13    YamlLoader(YamlLoaderError),
14}
15
16impl Error {
17    pub fn multiple(mut causes: Vec<Self>) -> Self {
18        if causes.len() == 1 {
19            causes.remove(0)
20        } else {
21            Self::Multiple { causes }
22        }
23    }
24}
25
26impl From<Vec<Error>> for Error {
27    fn from(mut causes: Vec<Error>) -> Self {
28        if causes.len() == 1 {
29            causes.remove(0)
30        } else {
31            Error::Multiple { causes }
32        }
33    }
34}
35
36pub fn by_key(key: impl Into<String>) -> impl FnOnce(Error) -> Error {
37    move |cause| Error::Enclosed {
38        key: key.into(),
39        causes: vec![cause],
40    }
41}
42
43pub fn with_key(key: impl Into<String>) -> impl FnOnce(Vec<Error>) -> Error {
44    move |causes| Error::Enclosed {
45        key: key.into(),
46        causes,
47    }
48}
49
50pub type Output<A> = crate::core::Output<A, Error>;
51
52#[derive(Clone, Debug, PartialEq)]
53pub enum SpecViolation {
54    V3_0(v3_0::SpecViolation),
55    JsonSchema(json_schema::SpecViolation),
56}
57
58#[derive(Clone, Debug, PartialEq)]
59pub enum Unsupported {
60    IncompatibleVersion { version: String },
61    UnknownType { found: String },
62}
63
64impl From<Unsupported> for Error {
65    fn from(unsupported: Unsupported) -> Self {
66        Error::Unsupported(unsupported)
67    }
68}