torn-api-codegen 0.8.1

Contains the v2 torn API model descriptions and codegen for the bindings
Documentation
use std::borrow::Cow;

use serde::{Deserialize, Deserializer, Serialize};

use super::parameter::OpenApiParameter;

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum OpenApiPathParameter<'a> {
    Link {
        #[serde(rename = "$ref")]
        ref_path: &'a str,
    },
    Inline(OpenApiParameter<'a>),
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct SchemaLink<'a> {
    #[serde(rename = "$ref")]
    pub ref_path: &'a str,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum OpenApiResponseBody<'a> {
    Schema(SchemaLink<'a>),
    Union {
        #[serde(borrow, rename = "anyOf")]
        any_of: Vec<SchemaLink<'a>>,
    },
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct OperationId(pub String);

fn deserialize_response_body<'de, D>(deserializer: D) -> Result<OpenApiResponseBody<'de>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    struct Json<'a> {
        #[serde(borrow)]
        schema: OpenApiResponseBody<'a>,
    }
    #[derive(Deserialize)]
    struct Content<'a> {
        #[serde(borrow, rename = "application/json")]
        json: Json<'a>,
    }
    #[derive(Deserialize)]
    struct StatusOk<'a> {
        #[serde(borrow)]
        content: Content<'a>,
    }
    #[derive(Deserialize)]
    struct Responses<'a> {
        #[serde(borrow, rename = "200")]
        ok: StatusOk<'a>,
    }

    let responses = Responses::deserialize(deserializer)?;

    Ok(responses.ok.content.json.schema)
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct OpenApiPathBody<'a> {
    pub summary: Option<Cow<'a, str>>,
    pub description: Option<Cow<'a, str>>,
    #[serde(borrow, default)]
    pub parameters: Vec<OpenApiPathParameter<'a>>,
    #[serde(
        borrow,
        rename = "responses",
        deserialize_with = "deserialize_response_body"
    )]
    pub response_content: OpenApiResponseBody<'a>,
    pub operation_id: Option<OperationId>,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct OpenApiPath<'a> {
    #[serde(borrow)]
    pub get: OpenApiPathBody<'a>,
}