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
use std::fmt;
use std::str::FromStr;
use hyper::header::*;
use hyper;
#[derive(Clone, PartialEq, Debug)]
pub struct DiscogsTokenAuth {
token: String,
}
impl Scheme for DiscogsTokenAuth {
fn scheme() -> Option<&'static str> {
Some("Discogs")
}
fn fmt_scheme(&self, f: &mut fmt::Formatter) -> fmt::Result {
let text: String = format!("token={}", self.token.clone());
f.write_str(text.as_ref())
}
}
impl FromStr for DiscogsTokenAuth {
type Err = hyper::error::Error;
fn from_str(s: &str) -> hyper::Result<DiscogsTokenAuth> {
match String::from_utf8(s.into()) {
Ok(text) => {
let mut parts = &mut text.split('=');
parts.next();
let token = match parts.next() {
Some(token_part) => token_part.to_owned(),
None => return Err(hyper::error::Error::Header)
};
Ok(DiscogsTokenAuth {
token: token,
})
},
Err(e) => {
println!("DiscogsTokenAuth::from_utf8 error={:?}", e);
Err(hyper::error::Error::Header)
}
}
}
}
#[cfg(test)]
mod tests {
use hyper::header::{Authorization, Basic, Bearer};
use hyper::header::{Headers, Header};
use query::DiscogsTokenAuth;
#[test]
fn test_discogs_token_auth() {
let mut headers = Headers::new();
headers.set(Authorization(DiscogsTokenAuth {
token: "fghcvkbaskj,dabsd".to_owned(),
}));
assert_eq!(
headers.to_string(),
"Authorization: Discogs token=fghcvkbaskj,dabsd\r\n".to_owned());
}
#[test]
fn test_discogs_token_auth_parse() {
let auth: Authorization<DiscogsTokenAuth> = Header::parse_header(
&[b"Discogs token=fghcvkbaskj,dabsd".to_vec()])
.unwrap();
assert_eq!(auth.0.token, "fghcvkbaskj,dabsd".to_string());
}
}
#[cfg(all(test, feature = "nightly"))]
mod discogs_ks_bench {
use test::Bencher;
use super::*;
use hyper::header::*;
#[bench]
fn bench_parse(b: &mut Bencher) {
let val = &[b"Discogs token=fghcvkbaskj,dabsd".to_vec()];
b.iter(|| {
let _: Authorization<DiscogsTokenAuth> = Header::parse_header(val).unwrap();
});
}
}