use serde::{Deserialize, Serialize};
use crate::model::parameter::ParameterIn;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Style {
Matrix,
Label,
Simple,
Form,
SpaceDelimited,
PipeDelimited,
DeepObject,
#[cfg(feature = "openapi32")]
Cookie,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum HeaderStyle {
Simple,
}
impl From<HeaderStyle> for Style {
fn from(_: HeaderStyle) -> Self {
Self::Simple
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EncodingStyle {
Form,
SpaceDelimited,
PipeDelimited,
DeepObject,
}
impl From<EncodingStyle> for Style {
fn from(style: EncodingStyle) -> Self {
match style {
EncodingStyle::Form => Self::Form,
EncodingStyle::SpaceDelimited => Self::SpaceDelimited,
EncodingStyle::PipeDelimited => Self::PipeDelimited,
EncodingStyle::DeepObject => Self::DeepObject,
}
}
}
impl Style {
#[must_use]
pub fn default_for(location: ParameterIn) -> Self {
match location {
ParameterIn::Query | ParameterIn::Cookie => Self::Form,
ParameterIn::Path | ParameterIn::Header => Self::Simple,
#[cfg(feature = "openapi32")]
ParameterIn::Querystring => Self::Form,
}
}
#[must_use]
pub fn is_valid_for(self, location: ParameterIn) -> bool {
match self {
Self::Matrix | Self::Label => location == ParameterIn::Path,
Self::Simple => matches!(location, ParameterIn::Path | ParameterIn::Header),
Self::Form => matches!(location, ParameterIn::Query | ParameterIn::Cookie),
Self::SpaceDelimited | Self::PipeDelimited | Self::DeepObject => {
location == ParameterIn::Query
}
#[cfg(feature = "openapi32")]
Self::Cookie => location == ParameterIn::Cookie,
}
}
#[must_use]
pub fn default_explode(self) -> bool {
match self {
Self::Form => true,
#[cfg(feature = "openapi32")]
Self::Cookie => true,
_ => false,
}
}
}