diff options
Diffstat (limited to 'src/response/builder.rs')
| -rw-r--r-- | src/response/builder.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/response/builder.rs b/src/response/builder.rs new file mode 100644 index 0000000..b99b824 --- /dev/null +++ b/src/response/builder.rs @@ -0,0 +1,75 @@ +use std::collections::HashMap; + +use crate::response::{self, Response}; + +#[derive(Debug, Clone, Copy)] +pub struct Builder; + +#[derive(Debug, Clone, Copy)] +pub struct Status { + status: response::Status, +} + +#[derive(Debug, Clone)] +pub struct Header { + status: response::Status, + headers: HashMap<String, Vec<u8>>, +} + +impl Builder { + pub fn new() -> Self { + Self + } + + pub fn status(self, status: response::Status) -> Status { + Status { status } + } +} + +impl Status { + pub fn headers<K: Into<String>, V: Into<Vec<u8>>, T: IntoIterator<Item = (K, V)>>( + self, + headers: T, + ) -> Header { + Header { + status: self.status, + headers: HashMap::from_iter( + headers + .into_iter() + .map(|(key, value)| (key.into(), value.into())), + ), + } + } + + pub fn body(self, body: response::Body) -> Response { + Response { + status: self.status, + headers: HashMap::new(), + body, + } + } +} + +impl Header { + pub fn headers<K: Into<String>, V: Into<Vec<u8>>, T: IntoIterator<Item = (K, V)>>( + self, + headers: T, + ) -> Header { + Header { + status: self.status, + headers: HashMap::from_iter( + headers + .into_iter() + .map(|(key, value)| (key.into(), value.into())), + ), + } + } + + pub fn body(self, body: response::Body) -> Response { + Response { + status: self.status, + headers: self.headers, + body, + } + } +} |
