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
use std::collections::BTreeMap;
use derive_more::derive::{Display, Error};
use serde::{Deserialize, Serialize};
use url::Url;
use super::spec_extensions;
/// Error raised when contact info contains an email field which is not a valid email.
#[derive(Debug, Display, Error)]
#[display("Email address is not valid")]
#[non_exhaustive]
pub struct InvalidEmail;
/// Contact information for the exposed API.
///
/// See <https://spec.openapis.org/oas/v3.1.1#contact-object>.
#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
pub struct Contact {
/// Identifying name of the contact person/organization.
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// URL pointing to the contact information.
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<Url>,
/// Email address of the contact person/organization.
///
/// Use [`validate_email()`](Self::validate_email) after deserializing to check that email
/// address provided is valid.
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
/// Specification extensions.
///
/// Only "x-" prefixed keys are collected, and the prefix is stripped.
///
/// See <https://spec.openapis.org/oas/v3.1.1#specification-extensions>.
#[serde(flatten, with = "spec_extensions")]
pub extensions: BTreeMap<String, serde_json::Value>,
}
impl Contact {
/// Validates email address field.
pub fn validate_email(&self) -> Result<(), InvalidEmail> {
let Some(email) = &self.email else {
return Ok(());
};
if email.contains("@") {
Ok(())
} else {
Err(InvalidEmail)
}
}
}