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
//! Support crate for Swagger codegen.
#![warn(missing_docs, missing_debug_implementations)]
#![deny(unused_extern_crates)]

use std::error;
use std::fmt;

/// Module for encoding API properties in base64.
pub mod base64_format;
pub use base64_format::ByteArray;

/// Module for encoding Nullable properties.
pub mod nullable_format;
pub use nullable_format::Nullable;

pub mod auth;
pub use auth::{AuthData, Authorization};

pub mod context;
pub use context::{
    ContextBuilder, ContextWrapper, ContextualPayload, EmptyContext, Has, Pop, Push,
};

pub mod add_context;
pub use add_context::{AddContextMakeService, AddContextService};

pub mod drop_context;
pub use drop_context::{DropContextMakeService, DropContextService};

pub mod request_parser;
pub use request_parser::RequestParser;

mod header;
pub use header::{IntoHeaderValue, XSpanId};

#[cfg(feature = "multipart")]
pub mod multipart;

/// Helper Bound for Errors for MakeService/Service wrappers
pub trait ErrorBound: Into<Box<dyn std::error::Error + Send + Sync>> {}

impl<T> ErrorBound for T where T: Into<Box<dyn std::error::Error + Send + Sync>> {}

/// Very simple error type - just holds a description of the error. This is useful for human
/// diagnosis and troubleshooting, but not for applications to parse. The justification for this
/// is to deny applications visibility into the communication layer, forcing the application code
/// to act solely on the logical responses that the API provides, promoting abstraction in the
/// application code.
#[derive(Clone, Debug)]
pub struct ApiError(pub String);

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let debug: &dyn fmt::Debug = self;
        debug.fmt(f)
    }
}

impl error::Error for ApiError {
    fn description(&self) -> &str {
        "Failed to produce a valid response."
    }
}

impl<'a> From<&'a str> for ApiError {
    fn from(e: &str) -> Self {
        ApiError(e.to_string())
    }
}

impl From<String> for ApiError {
    fn from(e: String) -> Self {
        ApiError(e)
    }
}

#[cfg(feature = "serdejson")]
impl From<serde_json::Error> for ApiError {
    fn from(e: serde_json::Error) -> Self {
        ApiError(format!("Response body did not match the schema: {}", e))
    }
}