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
use std::collections::HashSet;
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 {
    /// Error encountered during spec validation.
    #[fail(display = "{}", _0)]
    Validation(paperclip_core::ValidationError),
    /// The given directory cannot be used for generating code.
    #[fail(display = "Cannot generate code in the given directory")]
    InvalidCodegenDirectory,
    /// Currently, we only support OpenAPI v2, and eventually v3.
    #[fail(display = "This version of OpenAPI is unsupported.")]
    UnsupportedOpenAPIVersion,
    /// Paths listed in the spec must be unique.
    #[fail(display = "Path similar to {:?} already exists.", _0)]
    RelativePathNotUnique(String),
    #[fail(
        display = "Parameter(s) {:?} aren't defined for templated path {:?}",
        _1, _0
    )]
    MissingParametersInPath(String, HashSet<String>),
    /// Invalid host for URL.
    #[fail(display = "Cannot parse host {:?}: {}", _0, _1)]
    InvalidHost(String, url::ParseError),
    /// Invalid base path URL.
    #[fail(display = "Cannot set URL {:?}: {}", _0, _1)]
    InvalidBasePathURL(String, url::ParseError),
    /// 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 definition.
    #[fail(display = "Invalid path for definition: {:?}", _0)]
    InvalidDefinitionPath(PathBuf),
    /// I/O errors.
    #[fail(display = "I/O error: {}", _0)]
    Io(std::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),
    #[cfg(feature = "codegen")]
    /// Errors in templating.
    #[fail(display = "Templating error: {}", _0)]
    Templating(tinytemplate::error::Error),
}

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