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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use http::Method;
use mime::Mime;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::types::{ClientId, Scope, ScopeFromStrError, ScopeParameter};
pub const METHOD: Method = Method::POST;
pub const CONTENT_TYPE: Mime = mime::APPLICATION_WWW_FORM_URLENCODED;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Body<SCOPE>
where
SCOPE: Scope,
{
#[serde(skip_serializing_if = "Option::is_none")]
pub client_id: Option<ClientId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub scope: Option<ScopeParameter<SCOPE>>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
_extra: Option<Map<String, Value>>,
}
impl<SCOPE> Body<SCOPE>
where
SCOPE: Scope,
{
pub fn new(client_id: Option<ClientId>, scope: Option<ScopeParameter<SCOPE>>) -> Self {
Self {
client_id,
scope,
_extra: None,
}
}
pub fn set_extra(&mut self, extra: Map<String, Value>) {
self._extra = Some(extra);
}
pub fn extra(&self) -> Option<&Map<String, Value>> {
self._extra.as_ref()
}
pub fn try_from_t_with_string(body: &Body<String>) -> Result<Self, ScopeFromStrError> {
let scope = if let Some(x) = &body.scope {
Some(ScopeParameter::<SCOPE>::try_from_t_with_string(x)?)
} else {
None
};
let mut this = Self::new(body.client_id.to_owned(), scope);
if let Some(extra) = body.extra() {
this.set_extra(extra.to_owned());
}
Ok(this)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ser() {
let body = Body::new(
Some("your_client_id".to_owned()),
Some(vec!["email".to_owned(), "profile".to_owned()].into()),
);
match serde_urlencoded::to_string(&body) {
Ok(body_str) => {
assert_eq!(body_str, "client_id=your_client_id&scope=email+profile");
}
Err(err) => panic!("{}", err),
}
}
#[test]
fn de() {
let body_str = r"client_id=1406020730&scope=example_scope";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(body) => {
assert_eq!(body.client_id, Some("1406020730".to_owned()));
assert_eq!(
body.scope,
Some(ScopeParameter(vec!["example_scope".to_owned()]))
);
}
Err(err) => panic!("{}", err),
}
let body_str = r"client_id=1406020730";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(body) => {
assert_eq!(body.client_id, Some("1406020730".to_owned()));
assert_eq!(body.scope, None);
}
Err(err) => panic!("{}", err),
}
}
}