Skip to content

Commit 7a4d461

Browse files
committed
Minor Improvements
1 parent e0f82a2 commit 7a4d461

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

my-http-server-core/src/http_ok_result/http_ok_result.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl HttpOutput {
6868

6969
pub fn get_content_type(&self) -> &str {
7070
match self {
71-
HttpOutput::Empty => "text/plain",
71+
HttpOutput::Empty => "text/plain".into(),
7272
HttpOutput::Content {
7373
headers: _,
7474
content_type,
@@ -77,17 +77,18 @@ impl HttpOutput {
7777
} => content_type
7878
.as_ref()
7979
.map(|ct| ct.as_str())
80-
.unwrap_or("text/plain"),
80+
.unwrap_or("text/plain")
81+
.into(),
8182
HttpOutput::Redirect {
8283
url: _,
8384
headers: _,
8485
permanent: _,
85-
} => "text/plain",
86+
} => "text/plain".into(),
8687
HttpOutput::File {
8788
file_name: _,
8889
content: _,
8990
} => "application/octet-stream",
90-
HttpOutput::Raw(_) => "text/plain",
91+
HttpOutput::Raw(_) => "text/plain".into(),
9192
}
9293
}
9394

@@ -98,7 +99,7 @@ impl HttpOutput {
9899
) -> Result<HttpOkResult, HttpFailResult> {
99100
let result = match self {
100101
HttpOutput::Empty => HttpFailResult {
101-
content_type: WebContentType::Text,
102+
content_type: WebContentType::Text.into(),
102103
status_code: status_code,
103104
content: Vec::new(),
104105
write_telemetry,
@@ -150,13 +151,14 @@ impl HttpOutput {
150151
Err(result)
151152
}
152153

153-
pub fn as_text<'s>(text: impl Into<StrOrString<'s>>) -> Self {
154+
pub fn as_text<'s>(text: impl Into<StrOrString<'s>>) -> HttpOkResultBuilder {
154155
let text = text.into().to_string();
155-
Self::Content {
156+
157+
HttpOkResultBuilder {
156158
headers: None,
157159
content_type: Some(WebContentType::Text),
158-
content: text.into_bytes(),
159-
set_cookies: None,
160+
cookies: Default::default(),
161+
body: text.into_bytes(),
160162
}
161163
}
162164

@@ -171,14 +173,14 @@ impl HttpOutput {
171173
}
172174
}
173175

174-
pub fn as_yaml<T: Serialize>(model: T) -> Self {
176+
pub fn as_yaml<T: Serialize>(model: T) -> HttpOkResultBuilder {
175177
let yaml = serde_yaml::to_string(&model).unwrap();
176178

177-
Self::Content {
179+
HttpOkResultBuilder {
178180
headers: None,
179181
content_type: Some(WebContentType::Yaml),
180-
content: yaml.into_bytes(),
181-
set_cookies: None,
182+
cookies: Default::default(),
183+
body: yaml.into_bytes(),
182184
}
183185
}
184186

my-http-server-core/src/http_ok_result/http_ok_result_builder.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::HttpOkResult;
99
pub struct HttpOkResultBuilder {
1010
pub(crate) headers: Option<HashMap<String, String>>,
1111
pub(crate) content_type: Option<WebContentType>,
12-
pub(crate) cookies: CookieJar,
12+
pub(crate) cookies: Option<CookieJar>,
1313
pub(crate) body: Vec<u8>,
1414
}
1515

@@ -64,15 +64,28 @@ impl HttpOkResultBuilder {
6464
}
6565

6666
pub fn set_cookie(mut self, cookie: impl Into<Cookie>) -> Self {
67-
self.cookies = self.cookies.set_cookie(cookie);
67+
let cookie_jar = match self.cookies.take() {
68+
Some(cookie_jar) => cookie_jar,
69+
None => CookieJar::new(),
70+
};
71+
72+
self.cookies = Some(cookie_jar.set_cookie(cookie));
73+
6874
self
6975
}
7076

7177
pub fn set_cookies(mut self, cookies: impl IntoIterator<Item = impl Into<Cookie>>) -> Self {
78+
let mut cookie_jar = match self.cookies.take() {
79+
Some(cookies) => cookies,
80+
None => CookieJar::new(),
81+
};
82+
7283
for cookie in cookies {
73-
self.cookies = self.cookies.set_cookie(cookie);
84+
cookie_jar = cookie_jar.set_cookie(cookie);
7485
}
7586

87+
self.cookies = Some(cookie_jar);
88+
7689
self
7790
}
7891

@@ -84,11 +97,7 @@ impl HttpOkResultBuilder {
8497
output: HttpOutput::Content {
8598
headers: self.headers,
8699
content_type: self.content_type,
87-
set_cookies: if self.cookies.is_empty() {
88-
None
89-
} else {
90-
Some(self.cookies)
91-
},
100+
set_cookies: self.cookies,
92101
content: self.body,
93102
},
94103
})
@@ -98,11 +107,7 @@ impl HttpOkResultBuilder {
98107
HttpOutput::Content {
99108
headers: self.headers,
100109
content_type: self.content_type,
101-
set_cookies: if self.cookies.is_empty() {
102-
None
103-
} else {
104-
Some(self.cookies)
105-
},
110+
set_cookies: self.cookies,
106111
content,
107112
}
108113
}

0 commit comments

Comments
 (0)