Skip to content

Commit 9079c3e

Browse files
committed
Refactor
1 parent e3accd5 commit 9079c3e

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

src/models/partial_request.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::{
1212
#[derive(Debug, Clone, PartialEq)]
1313
pub struct PartialHttpRequest {
1414
message: String,
15-
uri: Option<Range<usize>>,
1615
method: Option<Range<usize>>,
16+
uri: Option<Range<usize>>,
1717
http_version: Option<Range<usize>>,
1818
headers: Vec<Range<usize>>,
1919
body: Option<Range<usize>>,
@@ -36,72 +36,85 @@ impl PartialHttpRequest {
3636
) -> Self {
3737
Self {
3838
message: message.to_string(),
39-
uri,
4039
method,
40+
uri,
4141
http_version,
4242
headers,
4343
body,
4444
}
4545
}
4646

47+
/// Get the original HTTP request message text
4748
pub fn message(&self) -> &str {
4849
&self.message
4950
}
5051

51-
fn slice_message(&self, span: &Span) -> &str {
52-
&self.message[span.clone()]
53-
}
54-
52+
/// Get the text span of the uri, if defined
5553
pub fn uri_span(&self) -> &Option<Range<usize>> {
5654
&self.uri
5755
}
5856

57+
/// Get the string text of the uri, if defined
5958
pub fn uri_str(&self) -> Option<&str> {
6059
self.uri.as_ref().map(|span| self.slice_message(span))
6160
}
6261

62+
/// Get the text span of the method, if defined
6363
pub fn method_span(&self) -> &Option<Range<usize>> {
6464
&self.method
6565
}
6666

67+
/// Get the string text of the method, if defined
6768
pub fn method_str(&self) -> Option<&str> {
6869
self.method.as_ref().map(|span| self.slice_message(span))
6970
}
7071

72+
/// Get the text span of the http version, if defined
7173
pub fn http_version_span(&self) -> &Option<Range<usize>> {
7274
&self.http_version
7375
}
7476

77+
/// Get the string text of the http version, if defined
7578
pub fn http_version_str(&self) -> Option<&str> {
7679
self.http_version
7780
.as_ref()
7881
.map(|span| self.slice_message(span))
7982
}
8083

84+
/// Get a list of the header line text spans
8185
pub fn header_spans(&self) -> &Vec<Range<usize>> {
8286
&self.headers
8387
}
8488

89+
/// Get a list of the string text header lines
8590
pub fn header_strs(&self) -> Vec<&str> {
8691
self.headers
8792
.iter()
8893
.map(|span| self.slice_message(span))
8994
.collect()
9095
}
9196

97+
/// Get the text span of a header line by key, if defined
9298
pub fn header_span(&self, key: &str) -> Option<&Range<usize>> {
9399
self.headers
94100
.iter()
95101
.find(|span| self.slice_message(span).starts_with(&format!("{key}:")))
96102
}
97103

104+
/// Get the string text of a header by key, if defined
98105
pub fn header_str(&self, key: &str) -> Option<&str> {
99106
self.header_span(key).map(|span| self.slice_message(span))
100107
}
101108

109+
/// Get the string text of the body, if defined
102110
pub fn body_str(&self) -> Option<&str> {
103111
self.body.as_ref().map(|span| &self.message[span.clone()])
104112
}
113+
114+
/// Return a slice of the message string
115+
fn slice_message(&self, span: &Span) -> &str {
116+
&self.message[span.clone()]
117+
}
105118
}
106119

107120
impl Default for PartialHttpRequest {

0 commit comments

Comments
 (0)