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
pub use self::code::{Code, AsReplyCode};
pub use self::commands::*;
pub mod code;
pub mod feat;
mod commands;
use std::io::prelude::*;
use std::{io, fmt};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Reply
{
pub code: Code,
pub text: Text,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Text
{
SingleLine(String),
MultiLine(Vec<String>),
}
impl Reply
{
pub fn new<C,S>(code: C, text: S) -> Self
where C: Into<Code>, S: Into<String> {
let text: String = text.into();
Reply {
code: code.into(),
text: text.into(),
}
}
pub fn single_line<C,S>(code: C, text: S) -> Self
where C: Into<Code>, S: Into<String> {
Reply {
code: code.into(),
text: Text::SingleLine(text.into()),
}
}
pub fn multi_line<C>(code: C, lines: Vec<String>) -> Self
where C: Into<Code> {
Reply {
code: code.into(),
text: Text::MultiLine(lines),
}
}
pub fn write(&self, write: &mut Write) -> Result<(), io::Error> {
match self.text {
Text::SingleLine(ref line) => {
write!(write, "{} {}\r\n", self.code.0, line)
},
Text::MultiLine(..) => unimplemented!(),
}
}
}
impl From<String> for Text
{
fn from(s: String) -> Text {
let lines: Vec<_> = s.lines().collect();
assert_eq!(lines.is_empty(), false);
if lines.len() == 1 {
Text::SingleLine(lines[0].to_owned())
} else {
Text::MultiLine(lines.into_iter().map(|l| l.to_owned()).collect())
}
}
}
impl fmt::Display for Text
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Text::SingleLine(ref line) => write!(fmt, "{}", line),
Text::MultiLine(ref lines) => {
for line in lines { write!(fmt, "{}\n", line)?; }
Ok(())
},
}
}
}