#![no_std]
extern crate no_std_compat as std;
extern crate alloc;
use std::prelude::v1::*;
use std::collections::BTreeMap;
use std::fmt;
use serde::{Deserialize, Serialize};
pub type Callbacks = BTreeMap<String, BTreeMap<String, BTreeMap<String, PathItem>>>;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProductOSOpenAPI {
pub openapi: Option<String>,
pub info: Info,
pub json_schema_dialect: Option<String>,
pub servers: Option<Vec<Server>>,
pub paths: Option<BTreeMap<String, PathItem>>,
pub webhooks: Option<BTreeMap<String, PathItem>>,
pub components: Option<Components>,
pub definitions: Option<BTreeMap<String, Schema>>,
pub security: Option<Vec<BTreeMap<String, Vec<String>>>>,
pub tags: Option<Vec<Tag>>,
pub external_docs: Option<ExternalDocs>,
pub swagger: Option<String>,
pub host: Option<String>,
pub base_path: Option<String>,
pub schemes: Option<Vec<String>>,
pub consumes: Option<Vec<String>>,
pub produces: Option<Vec<String>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Info {
pub title: String,
pub summary: Option<String>,
pub description: Option<String>,
pub terms_of_service: Option<String>,
pub contact: Option<Contact>,
pub license: Option<License>,
pub version: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Contact {
pub name: Option<String>,
pub url: Option<String>,
pub email: String
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct License {
pub name: String,
pub identifier: Option<String>,
pub url: Option<String>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Server {
pub url: String,
pub description: Option<String>,
pub variables: Option<BTreeMap<String, ServerVariable>>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerVariable {
#[serde(rename = "enum")]
pub enumeration: Option<Vec<String>>,
pub default: String,
pub description: Option<String>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Components {
pub schemas: Option<BTreeMap<String, Schema>>,
pub responses: Option<BTreeMap<String, Response>>,
pub parameters: Option<BTreeMap<String, Parameter>>,
pub examples: Option<BTreeMap<String, Example>>,
pub request_bodies: Option<BTreeMap<String, RequestBody>>,
pub headers: Option<BTreeMap<String, Header>>,
pub security_schemes: Option<BTreeMap<String, SecurityScheme>>,
pub links: Option<BTreeMap<String, Link>>,
pub callbacks: Option<Callbacks>,
pub path_items: Option<BTreeMap<String, PathItem>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PathItem {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub get: Option<Operation>,
pub put: Option<Operation>,
pub post: Option<Operation>,
pub delete: Option<Operation>,
pub options: Option<Operation>,
pub head: Option<Operation>,
pub patch: Option<Operation>,
pub trace: Option<Operation>,
pub servers: Option<Vec<Server>>,
pub parameters: Option<Vec<Parameter>>
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum SchemaType {
Object,
Number,
Integer,
String,
Boolean,
Array
}
impl fmt::Display for SchemaType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SchemaType::Object => write!(f, "object"),
SchemaType::Number => write!(f, "number"),
SchemaType::Integer => write!(f, "integer"),
SchemaType::String => write!(f, "string"),
SchemaType::Boolean => write!(f, "boolean"),
SchemaType::Array => write!(f, "array")
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum SchemaLocation {
Path,
Body,
Query,
Cookie
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Schema {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub title: Option<String>,
pub description: Option<String>,
pub all_of: Option<Vec<Box<Schema>>>,
#[serde(rename = "type")]
pub kind: Option<SchemaType>,
pub format: Option<String>,
pub properties: Option<BTreeMap<String, SchemaProperty>>,
pub minimum: Option<u64>,
pub maximum: Option<u64>,
pub items: Option<Box<Schema>>,
pub min_items: Option<u64>,
pub max_items: Option<u64>,
pub unique_items: Option<bool>,
pub min_length: Option<u64>,
pub max_length: Option<u64>,
pub required: Option<Vec<String>>,
pub discriminator: Option<Discriminator>,
pub xml: Option<XML>,
pub external_docs: Option<ExternalDocs>,
pub example: Option<serde_json::Value>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaProperty {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub description: Option<String>,
pub all_of: Option<Vec<Box<SchemaProperty>>>,
#[serde(rename = "type")]
pub kind: Option<SchemaType>,
pub minimum: Option<u64>,
pub items: Option<Box<Schema>>,
pub min_items: Option<u64>,
pub unique_items: Option<bool>,
pub min_length: Option<u64>,
pub required: Option<Vec<String>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Discriminator {
pub property_name: String,
pub mapping: Option<BTreeMap<String, String>>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct XML {
pub name: Option<String>,
pub namespace: Option<String>,
pub prefix: Option<String>,
pub attribute: Option<bool>,
pub wrapped: Option<bool>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Operation {
pub tags: Option<Vec<String>>,
pub summary: Option<String>,
pub description: Option<String>,
pub external_docs: Option<ExternalDocs>,
pub operation_id: Option<String>,
pub parameters: Option<Vec<Parameter>>,
pub request_body: Option<RequestBody>,
pub responses: Option<BTreeMap<String, Response>>,
pub callbacks: Option<BTreeMap<String, BTreeMap<String, PathItem>>>,
pub deprecated: Option<bool>,
pub security: Option<Vec<BTreeMap<String, Vec<String>>>>,
pub servers: Option<Server>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub headers: Option<BTreeMap<String, Header>>,
pub content: Option<BTreeMap<String, MediaType>>,
pub links: Option<BTreeMap<String, Link>>,
pub schema: Option<Schema>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Parameter {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub name: Option<String>,
#[serde(rename = "in")]
pub location: Option<String>,
pub required: Option<bool>,
pub deprecated: Option<bool>,
pub allow_empty_value: Option<bool>,
pub style: Option<String>,
pub explode: Option<bool>,
pub allow_reserved: Option<bool>,
pub schema: Option<Schema>,
pub example: Option<serde_json::Value>,
pub examples: Option<BTreeMap<String, Example>>,
pub content: Option<BTreeMap<String, MediaType>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Example {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub value: Option<serde_json::Value>,
pub external_value: Option<String>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestBody {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub content: BTreeMap<String, MediaType>,
pub required: Option<bool>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Header {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub required: Option<bool>,
pub deprecated: Option<bool>,
pub allow_empty_value: Option<bool>,
pub style: Option<String>,
pub explode: Option<bool>,
pub allow_reserved: Option<bool>,
pub schema: Option<Schema>,
pub example: Option<serde_json::Value>,
pub examples: Option<BTreeMap<String, Example>>,
pub content: Option<BTreeMap<String, MediaType>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MediaType {
pub schema: Option<Schema>,
pub example: Option<serde_json::Value>,
pub examples: Option<BTreeMap<String, Example>>,
pub encoding: Option<BTreeMap<String, Encoding>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Encoding {
pub content_type: Option<String>,
pub headers: Option<BTreeMap<String, Header>>,
pub style: Option<String>,
pub explode: Option<bool>,
pub allow_reserved: Option<bool>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SecurityScheme {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
#[serde(rename = "type")]
pub kind: String,
pub api_key: Option<String>,
#[serde(rename = "in")]
pub location: Option<String>,
pub scheme: String,
pub bearer_format: Option<String>,
pub flows: Option<OAuthFlows>,
pub open_id_connect_url: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OAuthFlows {
pub implicit: Option<OAuthFlow>,
pub password: Option<OAuthFlow>,
pub client_credentials: Option<OAuthFlow>,
pub authorization_code: Option<OAuthFlow>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OAuthFlow {
pub authorization_url: String,
pub token_url: String,
pub refresh_url: Option<String>,
pub scopes: BTreeMap<String, String>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Link {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub operation_ref: Option<String>,
pub operation_id: Option<String>,
pub parameters: Option<BTreeMap<String, serde_json::Value>>,
pub request_body: Option<serde_json::Value>,
pub server: Option<Server>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
pub name: String,
pub description: Option<String>,
pub external_docs: Option<ExternalDocs>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalDocs {
pub url: String,
pub description: Option<String>
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Reference {
#[serde(rename = "$ref")]
pub reference: Option<String>,
pub summary: Option<String>,
pub description: Option<String>
}
impl ProductOSOpenAPI {
pub fn is_openapi_v3(&self) -> bool {
self.openapi.is_some()
}
pub fn is_swagger_v2(&self) -> bool {
self.swagger.is_some()
}
pub fn version(&self) -> Option<&str> {
self.openapi.as_deref().or(self.swagger.as_deref())
}
}