use crate::Headers;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Response {
status: u16,
headers: Headers,
body: Vec<u8>,
}
impl Response {
#[must_use]
pub fn new(status: u16) -> Self {
Self {
status,
headers: Headers::new(),
body: Vec::new(),
}
}
#[must_use]
pub fn text(status: u16, body: impl Into<String>) -> Self {
Self::new(status)
.with_header("content-type", "text/plain; charset=utf-8")
.with_body(body.into().into_bytes())
}
#[must_use]
pub fn with_header(mut self, name: impl AsRef<str>, value: impl Into<String>) -> Self {
self.headers.insert(name, value);
self
}
#[must_use]
pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
self.body = body.into();
self
}
#[must_use]
pub const fn status(&self) -> u16 {
self.status
}
#[must_use]
pub const fn headers(&self) -> &Headers {
&self.headers
}
pub const fn headers_mut(&mut self) -> &mut Headers {
&mut self.headers
}
#[must_use]
pub fn body(&self) -> &[u8] {
&self.body
}
#[must_use]
pub fn body_str(&self) -> Option<&str> {
std::str::from_utf8(&self.body).ok()
}
}