use std::borrow::Cow;
use std::io::{self, Write};
use nom::IResult;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorResponse<'a> {
message: Cow<'a, str>,
}
impl<'a> ErrorResponse<'a> {
pub fn new(message: &'a str) -> ErrorResponse<'a> {
ErrorResponse { message: Cow::Borrowed(message) }
}
pub fn from_bytes(bytes: &'a [u8]) -> IResult<&'a [u8], ErrorResponse<'a>> {
map!(bytes, take_str!(bytes.len()), |m| ErrorResponse::new(m))
}
pub fn write_bytes<W>(&self, mut writer: W) -> io::Result<()>
where W: Write
{
try!(writer.write_all(self.message.as_bytes()));
Ok(())
}
pub fn message(&self) -> &str {
&*self.message
}
pub fn to_owned(&self) -> ErrorResponse<'static> {
ErrorResponse { message: Cow::Owned((*self.message).to_owned()) }
}
}