topcoat_openapi/
parameter.rs1use std::borrow::Cow;
2
3use schemars::{JsonSchema, SchemaGenerator};
4use serde::Serialize;
5
6use crate::content::SchemaFn;
7
8#[derive(Debug, Clone)]
29pub struct Parameter {
30 pub(crate) location: ParameterLocation,
31 pub(crate) name: Cow<'static, str>,
32 pub(crate) description: Option<Cow<'static, str>>,
33 pub(crate) required: bool,
34 pub(crate) schema: Option<SchemaFn>,
35}
36
37impl Parameter {
38 fn new(location: ParameterLocation, name: impl Into<Cow<'static, str>>) -> Self {
40 Self {
41 location,
42 name: name.into(),
43 description: None,
44 required: false,
45 schema: None,
46 }
47 }
48
49 #[must_use]
52 pub fn path(name: impl Into<Cow<'static, str>>) -> Self {
53 Self::new(ParameterLocation::Path, name)
54 }
55
56 #[must_use]
58 pub fn query(name: impl Into<Cow<'static, str>>) -> Self {
59 Self::new(ParameterLocation::Query, name)
60 }
61
62 #[must_use]
64 pub fn header(name: impl Into<Cow<'static, str>>) -> Self {
65 Self::new(ParameterLocation::Header, name)
66 }
67
68 #[must_use]
70 pub fn cookie(name: impl Into<Cow<'static, str>>) -> Self {
71 Self::new(ParameterLocation::Cookie, name)
72 }
73
74 #[must_use]
76 pub fn description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
77 self.description = Some(description.into());
78 self
79 }
80
81 #[must_use]
85 pub fn required(mut self, required: bool) -> Self {
86 self.required = required;
87 self
88 }
89
90 #[must_use]
93 pub fn schema<T: JsonSchema>(mut self) -> Self {
94 let schema: SchemaFn = SchemaGenerator::subschema_for::<T>;
95 self.schema = Some(schema);
96 self
97 }
98
99 pub(crate) fn same_as(&self, other: &Parameter) -> bool {
101 self.location == other.location && self.name == other.name
102 }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
107#[serde(rename_all = "lowercase")]
108pub(crate) enum ParameterLocation {
109 Path,
110 Query,
111 Header,
112 Cookie,
113}