1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::io;
use std::path::PathBuf;

macro_rules! impl_err_from {
    ($err:ident :: $type:ty > $variant:ident) => {
        impl From<$type> for $err {
            fn from(s: $type) -> Self {
                $err::$variant(s)
            }
        }
    };
}

/// Generic result used throughout this library.
pub type PaperClipResult<T> = Result<T, PaperClipError>;

/// Global error which encapsulates all related errors.
#[derive(Debug, Fail)]
pub enum PaperClipError {
    /// Failed to resolve the schema because an invalid URI was provided for
    /// `$ref` field.
    ///
    /// Currently, we only support `#/definitions/YourType` in `$ref` field.
    #[fail(
        display = "Invalid $ref URI: {}. Only relative URIs for definitions are supported right now.",
        _0
    )]
    InvalidRefURI(String),
    /// The given schema object is an array, but the `items` field is missing.
    #[fail(display = "Mising item schema for array: {:?}", _0)]
    MissingArrayItem(Option<String>),
    /// The name for the given definition is missing or invalid.
    #[fail(display = "Invalid name for definition")]
    InvalidDefinitionName,
    /// A valid path cannot be obtained for the given defition.
    #[fail(display = "Invalid path for definition: {:?}", _0)]
    InvalidDefinitionPath(PathBuf),
    /// A definition has been referenced but it's missing.
    #[fail(display = "Definition missing: {}", _0)]
    MissingDefinition(String),
    /// If a parameter uses a schema, then we expect it to exist in
    /// the definition (for now).
    #[fail(
        display = "Parameter {:?} in path {:?} defines a new schema, which is unsupported at this point.",
        _0, _1
    )]
    UnsupportedParameterDefinition(String, String),
    /// If a parameter specifies body, then schema must be specified.
    #[fail(
        display = "Parameter {:?} in path {:?} is a body but the schema is missing",
        _0, _1
    )]
    MissingSchemaForBodyParameter(String, String),
    /// If a parameter doesn't specify a body, then it must have a type.
    #[fail(display = "Parameter {:?} in path {:?} must have a type", _0, _1)]
    MissingParameterType(String, String),
    /// The type of this parameter is not known.
    #[fail(
        display = "Parameter {:?} in path {:?} doesn't have a known type",
        _0, _1
    )]
    UnknownParameterType(String, String),
    /// I/O errors.
    #[fail(display = "I/O error: {}", _0)]
    Io(io::Error),
    /// JSON coding errors.
    #[fail(display = "JSON error: {}", _0)]
    Json(serde_json::Error),
    /// YAML coding errors.
    #[fail(display = "YAML error: {}", _0)]
    Yaml(serde_yaml::Error),
    #[cfg(feature = "codegen-fmt")]
    /// Errors from rustfmt.
    #[fail(display = "Rustfmt formatting error: {}", _0)]
    RustFmt(rustfmt_nightly::ErrorKind),
}

impl_err_from!(PaperClipError::io::Error > Io);
impl_err_from!(PaperClipError::serde_json::Error > Json);
impl_err_from!(PaperClipError::serde_yaml::Error > Yaml);
#[cfg(feature = "codegen-fmt")]
impl_err_from!(PaperClipError::rustfmt_nightly::ErrorKind > RustFmt);