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
use crate::quoted_string; use crate::types::{AttrMacro, Attribute, State}; pub struct CommandBuilder {} impl CommandBuilder { pub fn check() -> Command { let args = b"CHECK".to_vec(); Command { args, next_state: None, } } pub fn close() -> Command { let args = b"CLOSE".to_vec(); Command { args, next_state: Some(State::Authenticated), } } pub fn examine(mailbox: &str) -> Command { let args = format!("EXAMINE \"{}\"", quoted_string(mailbox).unwrap()).into_bytes(); Command { args, next_state: Some(State::Selected), } } pub fn fetch() -> FetchCommandEmpty { let args = b"FETCH ".to_vec(); FetchCommandEmpty { args } } pub fn list(reference: &str, glob: &str) -> Command { let args = format!( "LIST \"{}\" \"{}\"", quoted_string(reference).unwrap(), quoted_string(glob).unwrap() ) .into_bytes(); Command { args, next_state: None, } } pub fn login(user_name: &str, password: &str) -> Command { let args = format!( "LOGIN \"{}\" \"{}\"", quoted_string(user_name).unwrap(), quoted_string(password).unwrap() ) .into_bytes(); Command { args, next_state: Some(State::Authenticated), } } pub fn select(mailbox: &str) -> Command { let args = format!("SELECT \"{}\"", quoted_string(mailbox).unwrap()).into_bytes(); Command { args, next_state: Some(State::Selected), } } pub fn uid_fetch() -> FetchCommandEmpty { let args = b"UID FETCH ".to_vec(); FetchCommandEmpty { args } } } pub struct Command { args: Vec<u8>, next_state: Option<State>, } impl Command { pub fn into_parts(self) -> (Vec<u8>, Option<State>) { let Command { args, next_state } = self; (args, next_state) } } pub struct FetchCommandEmpty { args: Vec<u8>, } impl FetchBuilderMessages for FetchCommandEmpty { fn prepare(self) -> FetchCommandMessages { FetchCommandMessages { args: self.args } } } pub struct FetchCommandMessages { args: Vec<u8>, } impl FetchBuilderMessages for FetchCommandMessages { fn prepare(self) -> FetchCommandMessages { let FetchCommandMessages { mut args } = self; args.push(b','); FetchCommandMessages { args } } } impl FetchCommandMessages { pub fn attr_macro(self, named: AttrMacro) -> FetchCommand { let FetchCommandMessages { mut args } = self; args.push(b' '); args.extend( match named { AttrMacro::All => "ALL", AttrMacro::Fast => "FAST", AttrMacro::Full => "FULL", } .as_bytes(), ); FetchCommand { args } } } pub trait FetchBuilderMessages where Self: Sized, { fn prepare(self) -> FetchCommandMessages; fn num(self, num: u32) -> FetchCommandMessages { let FetchCommandMessages { mut args } = self.prepare(); args.extend(num.to_string().as_bytes()); FetchCommandMessages { args } } fn range(self, start: u32, stop: u32) -> FetchCommandMessages { let FetchCommandMessages { mut args } = self.prepare(); args.extend(start.to_string().as_bytes()); args.push(b':'); args.extend(stop.to_string().as_bytes()); FetchCommandMessages { args } } fn all_after(self, start: u32) -> FetchCommandMessages { let FetchCommandMessages { mut args } = self.prepare(); args.extend(start.to_string().as_bytes()); args.extend(b":*"); FetchCommandMessages { args } } } pub struct FetchCommandAttributes { args: Vec<u8>, } impl FetchBuilderAttributes for FetchCommandMessages { fn prepare(self) -> FetchCommandAttributes { let FetchCommandMessages { mut args } = self; args.extend(b" ("); FetchCommandAttributes { args } } } impl FetchBuilderAttributes for FetchCommandAttributes { fn prepare(self) -> FetchCommandAttributes { let FetchCommandAttributes { mut args } = self; args.push(b' '); FetchCommandAttributes { args } } } pub trait FetchBuilderAttributes where Self: Sized, { fn prepare(self) -> FetchCommandAttributes; fn attr(self, attr: Attribute) -> FetchCommandAttributes { let FetchCommandAttributes { mut args } = self.prepare(); args.extend( match attr { Attribute::Body => "BODY", Attribute::Envelope => "ENVELOPE", Attribute::Flags => "FLAGS", Attribute::InternalDate => "INTERNALDATE", Attribute::ModSeq => "MODSEQ", Attribute::Rfc822 => "RFC822", Attribute::Rfc822Size => "RFC822.SIZE", Attribute::Rfc822Text => "RFC822.TEXT", Attribute::Uid => "UID", } .as_bytes(), ); FetchCommandAttributes { args } } } pub struct FetchCommand { args: Vec<u8>, } pub trait FetchBuilderModifiers where Self: Sized, { fn prepare(self) -> FetchCommand; fn build(self) -> Command { let FetchCommand { args } = self.prepare(); Command { args, next_state: None, } } fn changed_since(self, seq: u64) -> FetchCommand { let FetchCommand { mut args } = self.prepare(); args.extend(b" (CHANGEDSINCE "); args.extend(seq.to_string().as_bytes()); args.push(b')'); FetchCommand { args } } } impl FetchBuilderModifiers for FetchCommandAttributes { fn prepare(self) -> FetchCommand { let FetchCommandAttributes { mut args, .. } = self; args.push(b')'); FetchCommand { args } } } impl FetchBuilderModifiers for FetchCommand { fn prepare(self) -> FetchCommand { self } } #[cfg(test)] mod tests { use super::CommandBuilder; #[test] fn login() { assert_eq!( CommandBuilder::login("djc", "s3cr3t").into_parts().0, b"LOGIN \"djc\" \"s3cr3t\"" ); assert_eq!( CommandBuilder::login("djc", "domain\\password") .into_parts() .0, b"LOGIN \"djc\" \"domain\\\\password\"" ); } }