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
use std::io::{Read, Write}; use std::fmt; use std::error::Error; use std::ops::Deref; use std::mem; use std::convert::Into; use regex::Regex; use {Encodable, Decodable}; use encodable::StringEncodeError; const VALIDATE_TOPIC_FILTER_REGEX: &'static str = r"^(#|((\+|\$?[^/\$\+#]+)?(/(\+|[^/\$\+#]+))*?(/(\+|#|[^/\$\+#]+))?))$"; #[derive(Debug, Eq, PartialEq, Clone)] pub struct TopicFilter(String); impl TopicFilter { pub fn new_checked<S: Into<String>>(topic: S) -> Result<TopicFilter, TopicFilterError> { let topic = topic.into(); let re = Regex::new(VALIDATE_TOPIC_FILTER_REGEX).unwrap(); if topic.is_empty() || topic.as_bytes().len() > 65535 || !re.is_match(&topic[..]) { Err(TopicFilterError::InvalidTopicFilter(topic)) } else { Ok(TopicFilter(topic)) } } pub fn new<S: Into<String>>(topic: S) -> TopicFilter { TopicFilter(topic.into()) } } impl<'a> Encodable<'a> for TopicFilter { type Err = TopicFilterError; fn encode<W: Write>(&self, writer: &mut W) -> Result<(), TopicFilterError> { (&self.0[..]).encode(writer).map_err(TopicFilterError::StringEncodeError) } fn encoded_length(&self) -> u32 { (&self.0[..]).encoded_length() } } impl<'a> Decodable<'a> for TopicFilter { type Err = TopicFilterError; type Cond = (); fn decode_with<R: Read>(reader: &mut R, _rest: Option<()>) -> Result<TopicFilter, TopicFilterError> { let topic_filter: String = try!(Decodable::decode(reader).map_err(TopicFilterError::StringEncodeError)); TopicFilter::new_checked(topic_filter) } } impl Deref for TopicFilter { type Target = TopicFilterRef; fn deref(&self) -> &TopicFilterRef { TopicFilterRef::new(&self.0) } } #[derive(Debug, Eq, PartialEq)] pub struct TopicFilterRef(str); impl TopicFilterRef { pub fn new_checked<S: AsRef<str> + ?Sized>(topic: &S) -> Result<&TopicFilterRef, TopicFilterError> { let re = Regex::new(VALIDATE_TOPIC_FILTER_REGEX).unwrap(); let topic = topic.as_ref(); if topic.is_empty() || topic.as_bytes().len() > 65535 || !re.is_match(&topic[..]) { Err(TopicFilterError::InvalidTopicFilter(topic.to_owned())) } else { Ok(unsafe { mem::transmute(topic) }) } } pub fn new<S: AsRef<str> + ?Sized>(topic: &S) -> &TopicFilterRef { unsafe { mem::transmute(topic.as_ref()) } } } impl Deref for TopicFilterRef { type Target = str; fn deref(&self) -> &str { &self.0 } } #[derive(Debug)] pub enum TopicFilterError { StringEncodeError(StringEncodeError), InvalidTopicFilter(String), } impl fmt::Display for TopicFilterError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { &TopicFilterError::StringEncodeError(ref err) => err.fmt(f), &TopicFilterError::InvalidTopicFilter(ref topic) => write!(f, "Invalid topic filter ({})", topic), } } } impl Error for TopicFilterError { fn description(&self) -> &str { match self { &TopicFilterError::StringEncodeError(ref err) => err.description(), &TopicFilterError::InvalidTopicFilter(..) => "Invalid topic filter", } } fn cause(&self) -> Option<&Error> { match self { &TopicFilterError::StringEncodeError(ref err) => Some(err), &TopicFilterError::InvalidTopicFilter(..) => None, } } } #[cfg(test)] mod test { use super::*; #[test] fn test_topic_filter_validate() { let topic = "#".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "sport/tennis/player1".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "sport/tennis/player1/ranking".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "sport/tennis/player1/#".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "#".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "sport/tennis/#".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "sport/tennis#".to_owned(); assert!(TopicFilter::new_checked(topic).is_err()); let topic = "sport/tennis/#/ranking".to_owned(); assert!(TopicFilter::new_checked(topic).is_err()); let topic = "+".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "+/tennis/#".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "sport+".to_owned(); assert!(TopicFilter::new_checked(topic).is_err()); let topic = "sport/+/player1".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "+/+".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "$SYS/#".to_owned(); TopicFilter::new_checked(topic).unwrap(); let topic = "$SYS".to_owned(); TopicFilter::new_checked(topic).unwrap(); } }