1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Implements [OpenAPI External Docs Object][external_docs] types.
//!
//! [external_docs]: https://spec.openapis.org/oas/latest.html#xml-object
use serde::{Deserialize, Serialize};
use crate::PropMap;
/// Reference of external resource allowing extended documentation.
#[non_exhaustive]
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ExternalDocs {
/// Target url for external documentation location.
pub url: String,
/// Additional description supporting markdown syntax of the external documentation.
pub description: Option<String>,
/// Optional extensions "x-something"
#[serde(skip_serializing_if = "PropMap::is_empty", flatten)]
pub extensions: PropMap<String, serde_json::Value>,
}
impl ExternalDocs {
/// Construct a new [`ExternalDocs`].
///
/// Function takes target url argument for the external documentation location.
///
/// # Examples
///
/// ```
/// # use salvo_oapi::ExternalDocs;
/// let external_docs = ExternalDocs::new("https://pet-api.external.docs");
/// ```
#[must_use]
pub fn new<S: AsRef<str>>(url: S) -> Self {
Self {
url: url.as_ref().to_owned(),
..Default::default()
}
}
/// Add target url for external documentation location.
#[must_use]
pub fn url<I: Into<String>>(mut self, url: I) -> Self {
self.url = url.into();
self
}
/// Add additional description of external documentation.
#[must_use]
pub fn description<S: Into<String>>(mut self, description: S) -> Self {
self.description = Some(description.into());
self
}
/// Add openapi extensions (`x-something`) for [`ExternalDocs`].
#[must_use]
pub fn extensions(mut self, extensions: PropMap<String, serde_json::Value>) -> Self {
self.extensions = extensions;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_external_docs() {
let external_docs = ExternalDocs::default();
assert_eq!(external_docs.url, "");
assert_eq!(external_docs.description, None);
}
#[test]
fn test_build_external_docs() {
let external_docs = ExternalDocs::default();
let external_docs_with_url = external_docs
.url("https://pet-api.external.docs")
.description("description");
assert_eq!(external_docs_with_url.url, "https://pet-api.external.docs");
assert_eq!(
external_docs_with_url.description,
Some("description".to_owned())
);
}
}