use std::io::{BufWriter, Error, Write};
use std::net::TcpStream;
pub struct TcpWriterImpl {
writer: BufWriter<TcpStream>
}
pub trait TcpWriter {
fn write_line(&mut self, message: &String) -> Result<(), Error>;
fn write(&mut self, message: &String) -> Result<usize, Error>;
fn flush(&mut self) -> Result<(), Error>;
}
impl TcpWriterImpl {
pub fn new(writer: BufWriter<TcpStream>) -> Self {
Self {
writer
}
}
}
impl TcpWriter for TcpWriterImpl {
fn write_line(&mut self, message: &String) -> Result<(), Error> {
self.writer.write_all(format!("{}\n", message).as_bytes())?;
self.writer.write_all("\n".to_string().as_bytes())?;
self.writer.flush()
}
fn write(&mut self, message: &String) -> Result<usize, Error> {
self.writer.write(message.as_bytes())
}
fn flush(&mut self) -> Result<(), Error> {
self.writer.flush()
}
}