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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use client::{Client, ClientList};
use error;
use escaping::*;
use map::*;
use rustc_serialize::json;
use std::cmp;
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
pub struct Channel {
pub cid: i64,
pub channel_name: String,
pub clients: Vec<Client>,
}
impl Default for Channel {
fn default() -> Channel {
Channel {
cid: 0,
channel_name: String::new(),
clients: Vec::new(),
}
}
}
impl Channel {
pub fn new(channel_id: i64, name: String) -> Channel {
let mut channel = Channel::default();
channel.cid = channel_id;
channel.channel_name = name;
channel.unescape();
channel
}
fn unescape(&mut self) {
self.channel_name = unescape(&self.channel_name);
}
pub fn from_map(map: &StringMap) -> Channel {
let mut channel = Channel::default();
channel.mut_from_map(map);
channel
}
pub fn update_from_map(channel: &Channel, map: &StringMap) -> Channel {
let mut channel = channel.clone();
channel.mut_from_map(map);
channel
}
pub fn mut_from_map(&mut self, map: &StringMap) {
update_from_map(map, "cid", &mut self.cid);
update_from_map(map, "channel_name", &mut self.channel_name);
self.unescape();
}
pub fn is_empty(&self) -> bool {
self.clients.is_empty()
}
pub fn clients_len(&self) -> usize {
self.clients.len()
}
pub fn add_client(&mut self, client: Client) {
self.clients.push(client);
}
pub fn sort_clients(&mut self) {
self.clients.sort()
}
pub fn remove_sq_clients(&mut self) {
let new_clients = self
.clients
.iter()
.filter(|c| c.is_client())
.cloned()
.collect();
self.clients = new_clients;
}
pub fn as_json(&self) -> String {
json::encode(self).unwrap_or_default()
}
}
impl FromStr for Channel {
type Err = error::Error;
fn from_str(s: &str) -> error::Result<Self> {
let map = to_map(s);
Ok(Channel::from_map(&map))
}
}
impl fmt::Display for Channel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.channel_name)?;
if !self.is_empty() {
for client in &self.clients {
write!(f, "\n {}", client)?;
}
};
Ok(())
}
}
impl PartialEq for Channel {
fn eq(&self, other: &Self) -> bool {
self.cid.eq(&other.cid)
}
}
impl Eq for Channel {}
impl PartialOrd for Channel {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.cid.partial_cmp(&other.cid)
}
}
impl Ord for Channel {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.cid.cmp(&other.cid)
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, RustcDecodable, RustcEncodable)]
pub struct ChannelList(Vec<Channel>);
impl Default for ChannelList {
fn default() -> ChannelList {
ChannelList(Vec::new())
}
}
impl ChannelList {
pub fn into_inner(self) -> Vec<Channel> {
let ChannelList(channel) = self;
channel
}
pub fn from_maps(maps: &[StringMap]) -> ChannelList {
let mut vec = Vec::new();
for map in maps.iter() {
let channel = Channel::from_map(map);
vec.push(channel);
}
ChannelList(vec)
}
pub fn insert_client(&mut self, client: &Client) {
let cid = client.cid;
for channel in self.as_mut().iter_mut() {
if channel.cid == cid {
channel.add_client(client.clone());
}
}
}
pub fn merge_clients(&mut self, clients: &ClientList) {
for client in clients.iter() {
self.insert_client(client);
}
}
pub fn as_json(&self) -> String {
json::encode(self.as_ref()).unwrap_or_default()
}
}
impl Deref for ChannelList {
type Target = Vec<Channel>;
fn deref(&self) -> &Vec<Channel> {
self.as_ref()
}
}
impl AsRef<Vec<Channel>> for ChannelList {
fn as_ref(&self) -> &Vec<Channel> {
let &ChannelList(ref channel) = self;
channel
}
}
impl AsMut<Vec<Channel>> for ChannelList {
fn as_mut(&mut self) -> &mut Vec<Channel> {
let &mut ChannelList(ref mut channel) = self;
channel
}
}
impl FromStr for ChannelList {
type Err = error::Error;
fn from_str(s: &str) -> error::Result<Self> {
let maps: Vec<_> = s.split('|').map(to_map).collect();
Ok(ChannelList::from_maps(&maps))
}
}
impl From<Vec<Channel>> for ChannelList {
fn from(channels: Vec<Channel>) -> Self {
ChannelList(channels)
}
}
impl From<ChannelList> for Vec<Channel> {
fn from(channels: ChannelList) -> Self {
channels.into_inner()
}
}