1use std::fmt::Display;
2use std::io;
3use std::io::BufWriter;
4use std::io::Write;
5use std::net::TcpStream;
6
7#[derive(Debug)]
8pub struct Body {
9 writer: BufWriter<TcpStream>,
10 content: String
11}
12
13impl Body {
14 pub fn new(writer: BufWriter<TcpStream>, content: String) -> Body {
15 Body { writer, content }
16 }
17
18 pub fn add_to_body<B: Display>(&mut self, body: B) -> &mut Body {
19 let content = format!("{}\r\n", body);
20
21 self.content += &content;
22 self
23 }
24
25 pub fn flush(&mut self) -> io::Result<&mut Body> {
26 let bytes = self.content.as_bytes();
27
28 self.writer.write_all(bytes)?;
29 self.writer.write_all(b"\r\n")?;
30 self.writer.flush()?;
31
32 self.content.clear();
33
34 Ok(self)
35 }
36}