Skip to main content

openapi_schema_to_json_schema/
error.rs

1//! Error types returned by the converters.
2
3use std::fmt;
4
5/// Errors raised during conversion.
6///
7/// The conversion functions are fallible. Three conditions stop a conversion:
8/// an input `type` value that is not a valid JSON Schema type, a root schema
9/// that is neither an object nor an array, and a parameter with neither a
10/// `schema` nor a `content` member while strict mode is on.
11///
12/// Match on the variant to branch on the cause. Each variant carries the full
13/// message, which is also what [`Display`](std::fmt::Display) prints.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum Error {
16    /// A node carried a `type` value outside the draft-04 type set while strict
17    /// mode was on. The string is the full message, including the offending
18    /// value rendered as JSON.
19    InvalidType(String),
20    /// A root schema was a scalar or null, or a parameter had no `schema` and no
21    /// `content` while strict mode was on.
22    InvalidInput(String),
23}
24
25impl Error {
26    /// The error message.
27    pub fn message(&self) -> &str {
28        match self {
29            Error::InvalidType(m) | Error::InvalidInput(m) => m,
30        }
31    }
32}
33
34impl fmt::Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        f.write_str(self.message())
37    }
38}
39
40impl std::error::Error for Error {}