openapi-nexus 0.2.0

OpenAPI 3.x multi-language code generator
Documentation
/// Errors that can occur during API calls.
#[derive(Debug)]
pub enum Error {
    /// Network-level error from reqwest.
    Network(reqwest::Error),
    /// API returned a non-success status code.
    Api {
        status: reqwest::StatusCode,
        body: String,
    },
    /// Failed to deserialize the response body.
    Deserialize(serde_json::Error),
    /// Failed to serialize the request body as XML.
    Xml(serde_xml_rs::Error),
    /// Unsupported generated operation.
    Unsupported(&'static str),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Network(e) => write!(f, "network error: {e}"),
            Error::Api { status, body } => write!(f, "API error {status}: {body}"),
            Error::Deserialize(e) => write!(f, "deserialization error: {e}"),
            Error::Xml(e) => write!(f, "XML serialization error: {e}"),
            Error::Unsupported(e) => write!(f, "Unsupported operation: {e}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Network(e) => Some(e),
            Error::Deserialize(e) => Some(e),
            Error::Xml(e) => Some(e),
            Error::Api { .. } => None,
            Error::Unsupported(_) => None,
        }
    }
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Error::Network(e)
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Error::Deserialize(e)
    }
}

impl From<serde_xml_rs::Error> for Error {
    fn from(e: serde_xml_rs::Error) -> Self {
        Error::Xml(e)
    }
}

/// Typed HTTP error response payload.
#[derive(Debug)]
pub struct ApiError<T> {
    status_code: u16,
    headers: Vec<(String, String)>,
    raw_body: Vec<u8>,
    body: Result<T, Error>,
}

impl<T> ApiError<T> {
    pub fn new(
        status_code: u16,
        headers: Vec<(String, String)>,
        raw_body: Vec<u8>,
        body: Result<T, Error>,
    ) -> Self {
        Self {
            status_code,
            headers,
            raw_body,
            body,
        }
    }

    pub fn status_code(&self) -> u16 {
        self.status_code
    }

    pub fn headers(&self) -> &[(String, String)] {
        &self.headers
    }

    pub fn raw_body(&self) -> &[u8] {
        &self.raw_body
    }

    pub fn body(&self) -> Result<&T, &Error> {
        self.body.as_ref()
    }
}