use bytes::Bytes;
use crate::error::HttpError;
#[derive(Debug)]
pub struct Response {
status: u16,
headers: Vec<(String, String)>,
body: Bytes,
}
impl Response {
pub(crate) fn new(status: u16, headers: Vec<(String, String)>, body: Bytes) -> Self {
Self {
status,
headers,
body,
}
}
pub fn status(&self) -> u16 {
self.status
}
pub fn headers(&self) -> &[(String, String)] {
&self.headers
}
pub fn header(&self, name: &str) -> Option<&str> {
let lower = name.to_ascii_lowercase();
self.headers
.iter()
.find(|(k, _)| k.to_ascii_lowercase() == lower)
.map(|(_, v)| v.as_str())
}
pub fn bytes(self) -> Bytes {
self.body
}
pub fn text(self) -> Result<String, HttpError> {
String::from_utf8(self.body.to_vec()).map_err(|_| HttpError::Parse)
}
pub fn body(&self) -> &Bytes {
&self.body
}
}