use std::collections::BTreeMap;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ClientResponse {
status: u16,
headers: BTreeMap<String, String>,
body: Vec<u8>,
}
impl ClientResponse {
#[must_use]
pub const fn new(status: u16) -> Self {
Self {
status,
headers: BTreeMap::new(),
body: Vec::new(),
}
}
#[must_use]
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers
.insert(name.into().to_ascii_lowercase(), value.into());
self
}
#[must_use]
pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
self.body = body.into();
self
}
#[must_use]
pub fn body_text(self, text: impl Into<String>) -> Self {
self.body(text.into().into_bytes())
}
#[must_use]
pub const fn status(&self) -> u16 {
self.status
}
#[must_use]
pub const fn is_success(&self) -> bool {
self.status >= 200 && self.status < 300
}
#[must_use]
pub const fn headers(&self) -> &BTreeMap<String, String> {
&self.headers
}
#[must_use]
pub fn header_value(&self, name: &str) -> Option<&str> {
self.headers
.get(&name.to_ascii_lowercase())
.map(String::as_str)
}
#[must_use]
pub fn body_bytes(&self) -> &[u8] {
&self.body
}
#[must_use]
pub fn body_text_lossy(&self) -> String {
String::from_utf8_lossy(&self.body).into_owned()
}
}