use crate::{AttributeBag, Headers, Method};
#[derive(Debug)]
pub struct Request {
method: Method,
path: String,
query: Option<String>,
headers: Headers,
body: Vec<u8>,
attributes: AttributeBag,
}
impl Request {
#[must_use]
pub fn new(method: Method, path: impl Into<String>) -> Self {
Self {
method,
path: path.into(),
query: None,
headers: Headers::new(),
body: Vec::new(),
attributes: AttributeBag::new(),
}
}
#[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 fn with_query(mut self, query: impl Into<String>) -> Self {
let query = query.into();
self.query = if query.is_empty() { None } else { Some(query) };
self
}
#[must_use]
pub const fn method(&self) -> Method {
self.method
}
#[must_use]
pub fn path(&self) -> &str {
&self.path
}
#[must_use]
pub fn query(&self) -> Option<&str> {
self.query.as_deref()
}
#[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 const fn attributes(&self) -> &AttributeBag {
&self.attributes
}
pub const fn attributes_mut(&mut self) -> &mut AttributeBag {
&mut self.attributes
}
}