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
use std::fmt;
use std::collections::BTreeMap;
use error::{Error, ProtoError};
use convert::FromMap;
#[derive(Debug, PartialEq, Clone, RustcEncodable)]
pub struct Message {
pub channel: Channel,
pub message: String,
}
impl FromMap for Message {
fn from_map(map: BTreeMap<String, String>) -> Result<Message, Error> {
Ok(Message {
channel: Channel(try!(map.get("channel")
.map(|v| v.to_owned())
.ok_or(Error::Proto(ProtoError::NoField("channel"))))),
message: try!(map.get("message")
.map(|v| v.to_owned())
.ok_or(Error::Proto(ProtoError::NoField("message")))),
})
}
}
#[derive(Debug, PartialEq, PartialOrd, Clone, RustcEncodable)]
pub struct Channel(String);
impl fmt::Display for Channel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl Channel {
pub fn new(name: &str) -> Option<Channel> {
if Channel::is_valid_name(name) { Some(Channel(name.to_owned())) } else { None }
}
pub unsafe fn new_unchecked(name: String) -> Channel {
Channel(name)
}
pub fn is_valid_name(name: &str) -> bool {
name.bytes().all(|b| {
(0x61 <= b && b <= 0x7a) || (0x41 <= b && b <= 0x5a) || (0x30 <= b && b <= 0x39) ||
(b == 0x5f || b == 0x2f || b == 0x2e || b == 0x3a)
})
}
}