1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Clone, Debug, Deserialize, Serialize)]
6pub struct Server {
7 pub channels: Channel,
9 pub welcome_text: Option<String>,
11 pub username: String,
13 pub host: String,
15}
16
17#[derive(Clone, Debug, Deserialize, Serialize)]
19pub struct Channel {
20 pub description: Option<String>,
22 pub max_users: u32,
24 pub name: String,
26 pub children: Vec<Channel>,
28 pub users: Vec<User>,
30
31 links: Vec<Vec<usize>>, }
33
34impl Channel {
35 pub fn new(name: String, description: Option<String>, max_users: u32) -> Self {
37 Self {
38 description,
39 max_users,
40 name,
41 children: Vec::new(),
42 users: Vec::new(),
43
44 links: Vec::new(),
45 }
46 }
47
48 pub fn iter(&self) -> Iter<'_> {
53 Iter {
54 me: Some(&self),
55 channel: if self.children.is_empty() {
56 None
57 } else {
58 Some(0)
59 },
60 channels: self.children.iter().map(|e| e.iter()).collect(),
61 }
62 }
63
64 pub fn users_iter(&self) -> UsersIter<'_> {
67 UsersIter {
68 channels: self.children.iter().map(|e| e.users_iter()).collect(),
69 channel: if self.children.is_empty() {
70 None
71 } else {
72 Some(0)
73 },
74 user: if self.users.is_empty() { None } else { Some(0) },
75 users: &self.users,
76 }
77 }
78}
79
80#[derive(Debug)]
82pub struct Iter<'a> {
83 me: Option<&'a Channel>,
84 channel: Option<usize>,
85 channels: Vec<Iter<'a>>,
86}
87
88impl<'a> Iterator for Iter<'a> {
89 type Item = &'a Channel;
90
91 fn next(&mut self) -> Option<Self::Item> {
92 if self.me.is_some() {
93 self.me.take()
94 } else if let Some(mut c) = self.channel {
95 let mut n = self.channels[c].next();
96 while n.is_none() {
97 c += 1;
98 if c >= self.channels.len() {
99 self.channel = None;
100 return None;
101 }
102 n = self.channels[c].next();
103 }
104 self.channel = Some(c);
105 n
106 } else {
107 None
108 }
109 }
110}
111
112#[derive(Debug)]
114pub struct UsersIter<'a> {
115 channel: Option<usize>,
116 channels: Vec<UsersIter<'a>>,
117 user: Option<usize>,
118 users: &'a [User],
119}
120
121impl<'a> Iterator for UsersIter<'a> {
122 type Item = &'a User;
123
124 fn next(&mut self) -> Option<Self::Item> {
125 if let Some(u) = self.user {
126 let ret = Some(&self.users[u]);
127 if u + 1 < self.users.len() {
128 self.user = Some(u + 1);
129 } else {
130 self.user = None;
131 }
132 ret
133 } else if let Some(mut c) = self.channel {
134 let mut n = self.channels[c].next();
135 while n.is_none() {
136 c += 1;
137 if c >= self.channels.len() {
138 self.channel = None;
139 return None;
140 }
141 n = self.channels[c].next();
142 }
143 self.channel = Some(c);
144 n
145 } else {
146 None
147 }
148 }
149}
150
151#[derive(Clone, Debug, Deserialize, Serialize)]
152pub struct User {
153 pub comment: Option<String>, pub hash: Option<String>,
155 pub name: String,
156 pub priority_speaker: bool,
157 pub recording: bool,
158
159 pub suppress: bool, pub self_mute: bool, pub self_deaf: bool, pub mute: bool, pub deaf: bool, }
165
166macro_rules! true_to_str {
167 ($condition:expr, $res:expr) => {
168 if $condition {
169 $res
170 } else {
171 ""
172 }
173 };
174}
175
176impl fmt::Display for User {
177 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178 write!(
179 f,
180 "{} {}{}{}{}{}",
181 self.name,
182 true_to_str!(self.suppress, "s"),
183 true_to_str!(self.self_mute, "M"),
184 true_to_str!(self.self_deaf, "D"),
185 true_to_str!(self.mute, "m"),
186 true_to_str!(self.deaf, "d")
187 )
188 }
189}