openapi-to-rust 0.17.0

Generate typed Rust models, HTTP/SSE clients, and Axum servers from OpenAPI 3.0/3.1 (experimental 3.2)
Documentation
---
source: src/test_helpers.rs
expression: "&generated_code"
---
//! Generated types from OpenAPI specification
//!
//! This file contains all the generated types for the API.
//! Do not edit manually - regenerate using the appropriate script.
#![allow(clippy::large_enum_variant)]
#![allow(clippy::format_in_format_args)]
#![allow(clippy::let_unit_value)]
#![allow(unreachable_patterns)]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct DataBlock {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct MessageBlock {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub enum Response {
    Success(DataBlock),
    Error(MessageBlock),
}
impl serde::Serialize for Response {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            Self::Success(payload) => {
                let mut value = serde_json::to_value(payload)
                    .map_err(serde::ser::Error::custom)?;
                let object = value
                    .as_object_mut()
                    .ok_or_else(|| {
                        serde::ser::Error::custom(
                            concat!(
                                "discriminated union variant `", stringify!(Success),
                                "` did not serialize as an object",
                            ),
                        )
                    })?;
                match object.get("type") {
                    Some(
                        serde_json::Value::String(tag),
                    ) if matches!(tag.as_str(), "success") => {}
                    Some(serde_json::Value::String(tag)) => {
                        return Err(
                            serde::ser::Error::custom(
                                format!(
                                    "discriminator `{}` value `{tag}` is not valid for variant `{}`",
                                    "type", stringify!(Success),
                                ),
                            ),
                        );
                    }
                    Some(_) => {
                        return Err(
                            serde::ser::Error::custom(
                                concat!(
                                    "discriminator `", "type",
                                    "` did not serialize as a string",
                                ),
                            ),
                        );
                    }
                    None => {
                        object
                            .insert(
                                "type".to_string(),
                                serde_json::Value::String("success".to_string()),
                            );
                    }
                }
                value.serialize(serializer)
            }
            Self::Error(payload) => {
                let mut value = serde_json::to_value(payload)
                    .map_err(serde::ser::Error::custom)?;
                let object = value
                    .as_object_mut()
                    .ok_or_else(|| {
                        serde::ser::Error::custom(
                            concat!(
                                "discriminated union variant `", stringify!(Error),
                                "` did not serialize as an object",
                            ),
                        )
                    })?;
                match object.get("type") {
                    Some(
                        serde_json::Value::String(tag),
                    ) if matches!(tag.as_str(), "error") => {}
                    Some(serde_json::Value::String(tag)) => {
                        return Err(
                            serde::ser::Error::custom(
                                format!(
                                    "discriminator `{}` value `{tag}` is not valid for variant `{}`",
                                    "type", stringify!(Error),
                                ),
                            ),
                        );
                    }
                    Some(_) => {
                        return Err(
                            serde::ser::Error::custom(
                                concat!(
                                    "discriminator `", "type",
                                    "` did not serialize as a string",
                                ),
                            ),
                        );
                    }
                    None => {
                        object
                            .insert(
                                "type".to_string(),
                                serde_json::Value::String("error".to_string()),
                            );
                    }
                }
                value.serialize(serializer)
            }
        }
    }
}
impl<'de> serde::Deserialize<'de> for Response {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        let discriminator = match value.get("type") {
            Some(serde_json::Value::String(discriminator)) => {
                Some(discriminator.as_str())
            }
            Some(_) => {
                return Err(
                    serde::de::Error::custom(
                        concat!("non-string discriminator `", "type", "`",),
                    ),
                );
            }
            None => None,
        };
        match discriminator {
            Some(discriminator) => {
                match discriminator {
                    "success" => {
                        let primary_error = match serde_json::from_value::<
                            DataBlock,
                        >(value.clone()) {
                            Ok(payload) => return Ok(Self::Success(payload)),
                            Err(error) => error,
                        };
                        let mut structural_match: Option<(Self, &'static str)> = None;
                        if let Ok(payload) = serde_json::from_value::<
                            MessageBlock,
                        >(value.clone()) {
                            if let Some((_, first_name)) = &structural_match {
                                return Err(
                                    serde::de::Error::custom(
                                        format!(
                                            "discriminator `{}` value `{}` did not fit its mapped branch and structurally matched both `{}` and `{}`",
                                            "type", "success", first_name, stringify!(Error),
                                        ),
                                    ),
                                );
                            }
                            structural_match = Some((
                                Self::Error(payload),
                                stringify!(Error),
                            ));
                        }
                        match structural_match {
                            Some((payload, _)) => Ok(payload),
                            None => Err(serde::de::Error::custom(primary_error)),
                        }
                    }
                    "error" => {
                        let primary_error = match serde_json::from_value::<
                            MessageBlock,
                        >(value.clone()) {
                            Ok(payload) => return Ok(Self::Error(payload)),
                            Err(error) => error,
                        };
                        let mut structural_match: Option<(Self, &'static str)> = None;
                        if let Ok(payload) = serde_json::from_value::<
                            DataBlock,
                        >(value.clone()) {
                            if let Some((_, first_name)) = &structural_match {
                                return Err(
                                    serde::de::Error::custom(
                                        format!(
                                            "discriminator `{}` value `{}` did not fit its mapped branch and structurally matched both `{}` and `{}`",
                                            "type", "error", first_name, stringify!(Success),
                                        ),
                                    ),
                                );
                            }
                            structural_match = Some((
                                Self::Success(payload),
                                stringify!(Success),
                            ));
                        }
                        match structural_match {
                            Some((payload, _)) => Ok(payload),
                            None => Err(serde::de::Error::custom(primary_error)),
                        }
                    }
                    other => {
                        Err(
                            serde::de::Error::custom(
                                format!(
                                    "unknown discriminator value `{other}` for `{}`", "type",
                                ),
                            ),
                        )
                    }
                }
            }
            None => {
                let mut structural_match: Option<(Self, &'static str)> = None;
                if let Ok(payload) = serde_json::from_value::<DataBlock>(value.clone()) {
                    if let Some((_, first_name)) = &structural_match {
                        return Err(
                            serde::de::Error::custom(
                                format!(
                                    "missing discriminator `{}` structurally matched both `{}` and `{}`",
                                    "type", first_name, stringify!(Success),
                                ),
                            ),
                        );
                    }
                    structural_match = Some((
                        Self::Success(payload),
                        stringify!(Success),
                    ));
                }
                if let Ok(payload) = serde_json::from_value::<
                    MessageBlock,
                >(value.clone()) {
                    if let Some((_, first_name)) = &structural_match {
                        return Err(
                            serde::de::Error::custom(
                                format!(
                                    "missing discriminator `{}` structurally matched both `{}` and `{}`",
                                    "type", first_name, stringify!(Error),
                                ),
                            ),
                        );
                    }
                    structural_match = Some((Self::Error(payload), stringify!(Error)));
                }
                structural_match
                    .map(|(payload, _)| payload)
                    .ok_or_else(|| serde::de::Error::custom(
                        concat!(
                            "missing string discriminator `", "type",
                            "` and no tagless branch matched",
                        ),
                    ))
            }
        }
    }
}