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(())
},
}
}
}