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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
use mumble_protocol::control::msgs;
use serde::export::Formatter;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Server {
pub channels: Channel,
pub welcome_text: Option<String>,
pub username: String,
pub host: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Channel {
pub description: Option<String>,
pub links: Vec<Vec<usize>>, //to represent several walks through the tree to find channels its linked to
pub max_users: u32,
pub name: String,
pub children: Vec<Channel>,
pub users: Vec<User>,
}
impl Channel {
pub fn iter(&self) -> Iter<'_> {
Iter {
me: Some(&self),
channel: if self.children.len() > 0 {
Some(0)
} else {
None
},
channels: self.children.iter().map(|e| e.iter()).collect(),
}
}
pub fn users_iter(&self) -> UsersIter<'_> {
UsersIter {
channels: self.children.iter().map(|e| e.users_iter()).collect(),
channel: if self.children.len() > 0 {
Some(0)
} else {
None
},
user: if self.users.len() > 0 { Some(0) } else { None },
users: &self.users,
}
}
}
pub struct Iter<'a> {
me: Option<&'a Channel>,
channel: Option<usize>,
channels: Vec<Iter<'a>>,
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a Channel;
fn next(&mut self) -> Option<Self::Item> {
if self.me.is_some() {
self.me.take()
} else if let Some(mut c) = self.channel {
let mut n = self.channels[c].next();
while n.is_none() {
c += 1;
if c >= self.channels.len() {
self.channel = None;
return None;
}
n = self.channels[c].next();
}
self.channel = Some(c);
n
} else {
None
}
}
}
pub struct UsersIter<'a> {
channel: Option<usize>,
channels: Vec<UsersIter<'a>>,
user: Option<usize>,
users: &'a [User],
}
impl<'a> Iterator for UsersIter<'a> {
type Item = &'a User;
fn next(&mut self) -> Option<Self::Item> {
if let Some(u) = self.user {
let ret = Some(&self.users[u]);
if u + 1 < self.users.len() {
self.user = Some(u + 1);
} else {
self.user = None;
}
ret
} else if let Some(mut c) = self.channel {
let mut n = self.channels[c].next();
while n.is_none() {
c += 1;
if c >= self.channels.len() {
self.channel = None;
return None;
}
n = self.channels[c].next();
}
self.channel = Some(c);
n
} else {
None
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct User {
pub comment: Option<String>, //TODO not option, empty string instead
pub hash: Option<String>,
pub name: String,
pub priority_speaker: bool,
pub recording: bool,
pub suppress: bool, // by me
pub self_mute: bool, // by self
pub self_deaf: bool, // by self
pub mute: bool, // by admin
pub deaf: bool, // by admin
}
macro_rules! true_to_str {
($condition:expr, $res:expr) => {if $condition { $res } else { "" }};
}
impl Display for User {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}{}{}{}{}",
self.name,
true_to_str!(self.suppress, "s"),
true_to_str!(self.self_mute, "M"),
true_to_str!(self.self_deaf, "D"),
true_to_str!(self.mute, "m"),
true_to_str!(self.deaf, "d"))
}
}
#[derive(Debug, Default)]
pub struct UserDiff {
pub comment: Option<String>,
pub hash: Option<String>,
pub name: Option<String>,
pub priority_speaker: Option<bool>,
pub recording: Option<bool>,
pub suppress: Option<bool>, // by me
pub self_mute: Option<bool>, // by self
pub self_deaf: Option<bool>, // by self
pub mute: Option<bool>, // by admin
pub deaf: Option<bool>, // by admin
pub channel_id: Option<u32>,
}
impl UserDiff {
pub fn new() -> Self {
UserDiff::default()
}
}
impl From<msgs::UserState> for UserDiff {
fn from(mut msg: msgs::UserState) -> Self {
let mut ud = UserDiff::new();
if msg.has_comment() {
ud.comment = Some(msg.take_comment());
}
if msg.has_hash() {
ud.hash = Some(msg.take_hash());
}
if msg.has_name() {
ud.name = Some(msg.take_name());
}
if msg.has_priority_speaker() {
ud.priority_speaker = Some(msg.get_priority_speaker());
}
if msg.has_recording() {
ud.recording = Some(msg.get_recording());
}
if msg.has_suppress() {
ud.suppress = Some(msg.get_suppress());
}
if msg.has_self_mute() {
ud.self_mute = Some(msg.get_self_mute());
}
if msg.has_self_deaf() {
ud.self_deaf = Some(msg.get_self_deaf());
}
if msg.has_mute() {
ud.mute = Some(msg.get_mute());
}
if msg.has_deaf() {
ud.deaf = Some(msg.get_deaf());
}
if msg.has_channel_id() {
ud.channel_id = Some(msg.get_channel_id());
}
ud
}
}
|