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
106
107
108
109
110
111
112
113
use crate::twitch::Capability;
#[cfg(feature = "hashbrown")]
use hashbrown::HashSet;
#[cfg(not(feature = "hashbrown"))]
use std::collections::HashSet;
pub struct UserConfig {
pub token: String,
pub nick: String,
pub caps: Vec<Capability>,
}
impl UserConfig {
pub fn builder() -> self::UserConfigBuilder {
UserConfigBuilder::new()
}
}
pub struct UserConfigBuilder {
nick: Option<String>,
token: Option<String>,
caps: HashSet<Capability>,
}
impl Default for UserConfigBuilder {
fn default() -> Self {
Self {
nick: None,
token: None,
caps: [
Capability::Membership,
Capability::Commands,
Capability::Tags,
]
.iter()
.cloned()
.collect(),
}
}
}
impl UserConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn nick<S: ToString>(mut self, nick: S) -> Self {
self.nick.replace(nick.to_string());
self
}
pub fn token<S: ToString>(mut self, token: S) -> Self {
self.token.replace(token.to_string());
self
}
pub fn membership(mut self) -> Self {
self.toggle_cap(Capability::Membership);
self
}
pub fn commands(mut self) -> Self {
self.toggle_cap(Capability::Commands);
self
}
pub fn tags(mut self) -> Self {
self.toggle_cap(Capability::Tags);
self
}
pub fn build(self) -> Option<UserConfig> {
Some(UserConfig {
nick: self.nick?,
token: self.token?,
caps: self.caps.into_iter().collect(),
})
}
fn toggle_cap(&mut self, cap: Capability) {
if self.caps.contains(&cap) {
self.caps.remove(&cap);
} else {
self.caps.insert(cap);
}
}
}