use std::borrow::Cow;
use schemars::{JsonSchema, SchemaGenerator};
use serde::Serialize;
use crate::content::SchemaFn;
#[derive(Debug, Clone)]
pub struct Parameter {
pub(crate) location: ParameterLocation,
pub(crate) name: Cow<'static, str>,
pub(crate) description: Option<Cow<'static, str>>,
pub(crate) required: bool,
pub(crate) schema: Option<SchemaFn>,
}
impl Parameter {
fn new(location: ParameterLocation, name: impl Into<Cow<'static, str>>) -> Self {
Self {
location,
name: name.into(),
description: None,
required: false,
schema: None,
}
}
#[must_use]
pub fn path(name: impl Into<Cow<'static, str>>) -> Self {
Self::new(ParameterLocation::Path, name)
}
#[must_use]
pub fn query(name: impl Into<Cow<'static, str>>) -> Self {
Self::new(ParameterLocation::Query, name)
}
#[must_use]
pub fn header(name: impl Into<Cow<'static, str>>) -> Self {
Self::new(ParameterLocation::Header, name)
}
#[must_use]
pub fn cookie(name: impl Into<Cow<'static, str>>) -> Self {
Self::new(ParameterLocation::Cookie, name)
}
#[must_use]
pub fn description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
self.description = Some(description.into());
self
}
#[must_use]
pub fn required(mut self, required: bool) -> Self {
self.required = required;
self
}
#[must_use]
pub fn schema<T: JsonSchema>(mut self) -> Self {
let schema: SchemaFn = SchemaGenerator::subschema_for::<T>;
self.schema = Some(schema);
self
}
pub(crate) fn same_as(&self, other: &Parameter) -> bool {
self.location == other.location && self.name == other.name
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum ParameterLocation {
Path,
Query,
Header,
Cookie,
}