openapi_context/
lib.rs

1//! Support crate for Swagger codegen.
2#![warn(missing_docs, missing_debug_implementations)]
3#![deny(unused_extern_crates)]
4
5use std::error;
6use std::fmt;
7
8/// Module for encoding API properties in base64.
9pub mod base64_format;
10pub use base64_format::ByteArray;
11
12/// Module for encoding Nullable properties.
13pub mod nullable_format;
14pub use nullable_format::Nullable;
15
16pub mod auth;
17pub use auth::{AuthData, Authorization};
18
19pub mod context;
20pub use context::{
21    ContextBuilder, ContextWrapper, ContextualPayload, EmptyContext, Has, Pop, Push,
22};
23
24pub mod add_context;
25pub use add_context::{AddContextMakeService, AddContextService};
26
27pub mod drop_context;
28pub use drop_context::{DropContextMakeService, DropContextService};
29
30pub mod request_parser;
31pub use request_parser::RequestParser;
32
33mod header;
34pub use header::{IntoHeaderValue, XSpanId};
35
36#[cfg(feature = "multipart")]
37pub mod multipart;
38
39/// Helper Bound for Errors for MakeService/Service wrappers
40pub trait ErrorBound: Into<Box<dyn std::error::Error + Send + Sync>> {}
41
42impl<T> ErrorBound for T where T: Into<Box<dyn std::error::Error + Send + Sync>> {}
43
44/// Very simple error type - just holds a description of the error. This is useful for human
45/// diagnosis and troubleshooting, but not for applications to parse. The justification for this
46/// is to deny applications visibility into the communication layer, forcing the application code
47/// to act solely on the logical responses that the API provides, promoting abstraction in the
48/// application code.
49#[derive(Clone, Debug)]
50pub struct ApiError(pub String);
51
52impl fmt::Display for ApiError {
53    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54        let debug: &dyn fmt::Debug = self;
55        debug.fmt(f)
56    }
57}
58
59impl error::Error for ApiError {
60    fn description(&self) -> &str {
61        "Failed to produce a valid response."
62    }
63}
64
65impl<'a> From<&'a str> for ApiError {
66    fn from(e: &str) -> Self {
67        ApiError(e.to_string())
68    }
69}
70
71impl From<String> for ApiError {
72    fn from(e: String) -> Self {
73        ApiError(e)
74    }
75}
76
77#[cfg(feature = "serdejson")]
78impl From<serde_json::Error> for ApiError {
79    fn from(e: serde_json::Error) -> Self {
80        ApiError(format!("Response body did not match the schema: {}", e))
81    }
82}