use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Ref {
#[serde(rename = "$ref")]
pub location: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub summary: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
impl Ref {
pub fn new(location: impl Into<String>) -> Self {
Self {
location: location.into(),
summary: None,
description: None,
}
}
#[must_use]
pub fn schema(name: &str) -> Self {
Self::new(format!("#/components/schemas/{name}"))
}
#[must_use]
pub fn response(name: &str) -> Self {
Self::new(format!("#/components/responses/{name}"))
}
#[must_use]
pub fn parameter(name: &str) -> Self {
Self::new(format!("#/components/parameters/{name}"))
}
#[must_use]
pub fn request_body(name: &str) -> Self {
Self::new(format!("#/components/requestBodies/{name}"))
}
#[must_use]
pub fn security_scheme(name: &str) -> Self {
Self::new(format!("#/components/securitySchemes/{name}"))
}
#[must_use]
pub fn with_summary(mut self, summary: impl Into<String>) -> Self {
self.summary = Some(summary.into());
self
}
#[must_use]
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RefOr<T> {
Ref(Ref),
Item(T),
}
impl<T> RefOr<T> {
pub fn as_item(&self) -> Option<&T> {
match self {
Self::Item(item) => Some(item),
Self::Ref(_) => None,
}
}
pub fn as_ref_object(&self) -> Option<&Ref> {
match self {
Self::Ref(reference) => Some(reference),
Self::Item(_) => None,
}
}
pub fn is_ref(&self) -> bool {
matches!(self, Self::Ref(_))
}
}
impl<T> From<Ref> for RefOr<T> {
fn from(reference: Ref) -> Self {
Self::Ref(reference)
}
}
#[cfg(test)]
mod tests;