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
use std::borrow::Cow;
pub trait SecretsProvider {
fn get_consumer_key_pair<'a>(&'a self) -> (&'a str, &'a str);
fn get_token_pair_option<'a>(&'a self) -> Option<(&'a str, &'a str)>;
fn get_token_option_pair<'a>(&'a self) -> (Option<&'a str>, Option<&'a str>) {
self.get_token_pair_option()
.map(|s| (Some(s.0), Some(s.1)))
.unwrap_or_else(|| (None, None))
}
}
#[derive(Debug, Clone)]
pub struct Secrets<'a> {
consumer_key_secret: (Cow<'a, str>, Cow<'a, str>),
token_key_secret: Option<(Cow<'a, str>, Cow<'a, str>)>,
}
impl<'a> Secrets<'a> {
pub fn new<TKey, TSecret>(consumer_key: TKey, consumer_secret: TSecret) -> Self
where
TKey: Into<Cow<'a, str>>,
TSecret: Into<Cow<'a, str>>,
{
Secrets {
consumer_key_secret: (consumer_key.into(), consumer_secret.into()),
token_key_secret: None,
}
}
pub fn new_with_token<TKey, TSecret, TToken, TTokenSecret>(
consumer_key: TKey,
consumer_secret: TSecret,
token: TToken,
token_secret: TTokenSecret,
) -> Self
where
TKey: Into<Cow<'a, str>>,
TSecret: Into<Cow<'a, str>>,
TToken: Into<Cow<'a, str>>,
TTokenSecret: Into<Cow<'a, str>>,
{
Secrets {
consumer_key_secret: (consumer_key.into(), consumer_secret.into()),
token_key_secret: Some((token.into(), token_secret.into())),
}
}
pub fn token<TKey, TSecret>(self, token: TKey, token_secret: TSecret) -> Secrets<'a>
where
TKey: Into<Cow<'a, str>>,
TSecret: Into<Cow<'a, str>>,
{
Secrets {
token_key_secret: Some((token.into(), token_secret.into())),
..self
}
}
}
impl SecretsProvider for Secrets<'_> {
fn get_consumer_key_pair<'a>(&'a self) -> (&'a str, &'a str) {
(&self.consumer_key_secret.0, &self.consumer_key_secret.1)
}
fn get_token_pair_option<'a>(&'a self) -> Option<(&'a str, &'a str)> {
match &self.token_key_secret {
Some((k, s)) => Some((&k, &s)),
None => None,
}
}
}