From 4f9d09b58ae81e4e6144b85ce36ba424e319b779 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 3 Jun 2024 20:40:54 +0300 Subject: codecrafters submit [skip ci] --- src/response.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/response.rs (limited to 'src/response.rs') diff --git a/src/response.rs b/src/response.rs new file mode 100644 index 0000000..4219afd --- /dev/null +++ b/src/response.rs @@ -0,0 +1,40 @@ +use crate::http_types::*; +use std::collections::HashMap; + +pub struct Response { + version: String, + status: StatusCode, + headers: Option, + body: Option, +} + +impl Response { + pub fn new( + version: String, + status: StatusCode, + headers: Option, + body: Option, + ) -> Self { + Response { + version, + headers, + status, + body, + } + } +} + +impl Into for Response { + fn into(self) -> String { + let status_line = format!("HTTP/{} {}", self.version, String::from(self.status)); + let headers = self + .headers + .unwrap_or(Headers(HashMap::new())) + .0 + .iter() + .map(|(key, value)| format!("{key}: {value}\r\n")) + .collect::(); + let body = self.body.unwrap_or("".to_string()); + format!("{status_line}\r\n{headers}\r\n{body}") + } +} -- cgit v1.2.3