use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiSpec {
pub filename: Option<String>,
pub format: SpecFormat,
pub version: String,
pub title: String,
pub api_version: String,
pub operations: Vec<Operation>,
pub global_middlewares: Vec<MiddlewareConfig>,
pub extensions: BTreeMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SpecFormat {
OpenApi,
AsyncApi,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Operation {
pub path: String,
pub method: String,
pub operation_id: Option<String>,
#[serde(default)]
pub summary: Option<String>,
#[serde(default)]
pub description: Option<String>,
pub parameters: Vec<Parameter>,
pub request_body: Option<RequestBody>,
pub dispatch: Option<DispatchConfig>,
pub middlewares: Option<Vec<MiddlewareConfig>>,
#[serde(default)]
pub deprecated: bool,
pub sunset: Option<String>,
pub extensions: BTreeMap<String, serde_json::Value>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub messages: Vec<Message>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub bindings: BTreeMap<String, serde_json::Value>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub responses: BTreeMap<String, ResponseContent>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseContent {
pub content: BTreeMap<String, ContentSchema>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Parameter {
pub name: String,
pub location: String,
pub required: bool,
pub schema: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DispatchConfig {
pub name: String,
#[serde(default)]
pub config: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MiddlewareConfig {
pub name: String,
#[serde(default)]
pub config: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestBody {
pub required: bool,
pub content: BTreeMap<String, ContentSchema>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentSchema {
pub schema: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub name: String,
pub payload: Option<serde_json::Value>,
pub content_type: Option<String>,
#[serde(default)]
pub bindings: BTreeMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Channel {
pub address: String,
pub messages: Vec<Message>,
pub parameters: Vec<Parameter>,
#[serde(default)]
pub bindings: BTreeMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AsyncAction {
Send,
Receive,
}
impl std::fmt::Display for AsyncAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AsyncAction::Send => write!(f, "SEND"),
AsyncAction::Receive => write!(f, "RECEIVE"),
}
}
}