Skip to content

Commit 4a0dfff

Browse files
committed
uri - improve docs and refactor code
1 parent c82e964 commit 4a0dfff

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

src/uri.rs

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! uri operations
2+
23
use crate::error::{Error, ParseErr};
34
use std::{
45
convert::TryFrom,
@@ -21,11 +22,14 @@ pub struct RangeC {
2122
impl RangeC {
2223
/// Creates new `RangeC` with `start` and `end`.
2324
///
24-
/// # Exmaples
25+
/// # Examples
2526
/// ```
2627
/// use http_req::uri::RangeC;
2728
///
28-
/// const range: RangeC = RangeC::new(0, 20);
29+
/// let range = RangeC::new(0, 20);
30+
///
31+
/// assert_eq!(range.start, 0);
32+
/// assert_eq!(range.end, 20);
2933
/// ```
3034
pub const fn new(start: usize, end: usize) -> RangeC {
3135
RangeC { start, end }
@@ -61,12 +65,12 @@ impl Index<RangeC> for String {
6165

6266
/// Representation of Uniform Resource Identifier
6367
///
64-
/// # Example
68+
/// # Examples
6569
/// ```
6670
/// use http_req::uri::Uri;
6771
/// use std::convert::TryFrom;
6872
///
69-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
73+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
7074
/// assert_eq!(uri.host(), Some("foo.com"));
7175
/// ```
7276
#[derive(Clone, Debug, PartialEq)]
@@ -87,12 +91,12 @@ impl<'a> Uri<'a> {
8791

8892
/// Returns scheme of this `Uri`.
8993
///
90-
/// # Example
94+
/// # Examples
9195
/// ```
9296
/// use http_req::uri::Uri;
9397
/// use std::convert::TryFrom;
9498
///
95-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
99+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
96100
/// assert_eq!(uri.scheme(), "https");
97101
/// ```
98102
pub fn scheme(&self) -> &str {
@@ -101,12 +105,12 @@ impl<'a> Uri<'a> {
101105

102106
/// Returns information about the user included in this `Uri`.
103107
///
104-
/// # Example
108+
/// # Examples
105109
/// ```
106110
/// use http_req::uri::Uri;
107111
/// use std::convert::TryFrom;
108112
///
109-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
113+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
110114
/// assert_eq!(uri.user_info(), Some("user:info"));
111115
/// ```
112116
pub fn user_info(&self) -> Option<&str> {
@@ -115,12 +119,12 @@ impl<'a> Uri<'a> {
115119

116120
/// Returns host of this `Uri`.
117121
///
118-
/// # Example
122+
/// # Examples
119123
/// ```
120124
/// use http_req::uri::Uri;
121125
/// use std::convert::TryFrom;
122126
///
123-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
127+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
124128
/// assert_eq!(uri.host(), Some("foo.com"));
125129
/// ```
126130
pub fn host(&self) -> Option<&str> {
@@ -129,12 +133,12 @@ impl<'a> Uri<'a> {
129133

130134
/// Returns host of this `Uri` to use in a header.
131135
///
132-
/// # Example
136+
/// # Examples
133137
/// ```
134138
/// use http_req::uri::Uri;
135139
/// use std::convert::TryFrom;
136140
///
137-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
141+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
138142
/// assert_eq!(uri.host_header(), Some("foo.com:12".to_string()));
139143
/// ```
140144
pub fn host_header(&self) -> Option<String> {
@@ -146,12 +150,12 @@ impl<'a> Uri<'a> {
146150

147151
/// Returns port of this `Uri`
148152
///
149-
/// # Example
153+
/// # Examples
150154
/// ```
151155
/// use http_req::uri::Uri;
152156
/// use std::convert::TryFrom;
153157
///
154-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
158+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
155159
/// assert_eq!(uri.port(), Some(12));
156160
/// ```
157161
pub fn port(&self) -> Option<u16> {
@@ -161,12 +165,12 @@ impl<'a> Uri<'a> {
161165
/// Returns port corresponding to this `Uri`.
162166
/// Returns default port if it hasn't been set in the uri.
163167
///
164-
/// # Example
168+
/// # Examples
165169
/// ```
166170
/// use http_req::uri::Uri;
167171
/// use std::convert::TryFrom;
168172
///
169-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
173+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
170174
/// assert_eq!(uri.corr_port(), 12);
171175
/// ```
172176
pub fn corr_port(&self) -> u16 {
@@ -183,12 +187,12 @@ impl<'a> Uri<'a> {
183187

184188
/// Returns path of this `Uri`.
185189
///
186-
/// # Example
190+
/// # Examples
187191
/// ```
188192
/// use http_req::uri::Uri;
189193
/// use std::convert::TryFrom;
190194
///
191-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
195+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
192196
/// assert_eq!(uri.path(), Some("/bar/baz"));
193197
/// ```
194198
pub fn path(&self) -> Option<&str> {
@@ -197,12 +201,12 @@ impl<'a> Uri<'a> {
197201

198202
/// Returns query of this `Uri`.
199203
///
200-
/// # Example
204+
/// # Examples
201205
/// ```
202206
/// use http_req::uri::Uri;
203207
/// use std::convert::TryFrom;
204208
///
205-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
209+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
206210
/// assert_eq!(uri.query(), Some("query"));
207211
/// ```
208212
pub fn query(&self) -> Option<&str> {
@@ -211,12 +215,12 @@ impl<'a> Uri<'a> {
211215

212216
/// Returns fragment of this `Uri`.
213217
///
214-
/// # Example
218+
/// # Examples
215219
/// ```
216220
/// use http_req::uri::Uri;
217221
/// use std::convert::TryFrom;
218222
///
219-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
223+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
220224
/// assert_eq!(uri.fragment(), Some("fragment"));
221225
/// ```
222226
pub fn fragment(&self) -> Option<&str> {
@@ -225,12 +229,12 @@ impl<'a> Uri<'a> {
225229

226230
/// Returns resource `Uri` points to.
227231
///
228-
/// # Example
232+
/// # Examples
229233
/// ```
230234
/// use http_req::uri::Uri;
231235
/// use std::convert::TryFrom;
232236
///
233-
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();;
237+
/// let uri: Uri = Uri::try_from("https://user:[email protected]:12/bar/baz?query#fragment").unwrap();
234238
/// assert_eq!(uri.resource(), "/bar/baz?query#fragment");
235239
/// ```
236240
pub fn resource(&self) -> &str {
@@ -241,6 +245,14 @@ impl<'a> Uri<'a> {
241245
}
242246

243247
/// Checks if &str is a relative uri.
248+
///
249+
/// # Examples
250+
/// ```
251+
/// use http_req::uri::Uri;
252+
///
253+
/// assert!(Uri::is_relative("/relative/path"));
254+
/// assert!(!Uri::is_relative("http://absolute.com"));
255+
/// ```
244256
pub fn is_relative(raw_uri: &str) -> bool {
245257
raw_uri.starts_with("/")
246258
|| raw_uri.starts_with("?")
@@ -250,6 +262,18 @@ impl<'a> Uri<'a> {
250262

251263
/// Creates a new `Uri` from current uri and relative uri.
252264
/// Writes the new uri (raw string) into `relative_uri`.
265+
///
266+
/// # Examples
267+
/// ```
268+
/// use http_req::uri::Uri;
269+
/// use std::convert::TryFrom;
270+
///
271+
/// let base_uri: Uri = Uri::try_from("https://example.com/base").unwrap();
272+
/// let mut relative_path = String::from("/relative/path");
273+
/// let new_uri = base_uri.from_relative(&mut relative_path).unwrap();
274+
///
275+
/// assert_eq!(new_uri.to_string(), "https://example.com/relative/path");
276+
/// ```
253277
pub fn from_relative(&'a self, relative_uri: &'a mut String) -> Result<Uri<'a>, Error> {
254278
let inner_uri = self.inner;
255279
let mut resource = self.resource().to_string();
@@ -258,7 +282,7 @@ impl<'a> Uri<'a> {
258282
Some("#") => Uri::add_part_start(&resource, relative_uri, "#"),
259283
Some("?") => Uri::add_part_start(&self.path().unwrap_or("/"), relative_uri, "?"),
260284
Some("/") => Uri::add_part_start(&resource, relative_uri, "/"),
261-
Some(_) | None => Uri::add_part_end(&resource, relative_uri, "/"),
285+
_ => Uri::add_part_end(&resource, relative_uri, "/"),
262286
};
263287

264288
*relative_uri = if let Some(p) = self.path {
@@ -270,7 +294,7 @@ impl<'a> Uri<'a> {
270294
Uri::try_from(relative_uri.as_str())
271295
}
272296

273-
/// Adds a part at the beggining of the base.
297+
/// Adds a part at the beginning of the base.
274298
/// Finds the first occurance of a separator in a base and the first occurance of a separator in a part.
275299
/// Joins all chars before the separator from the base, separator and all chars after the separator from the part.
276300
fn add_part_start(base: &str, part: &str, separator: &str) -> String {
@@ -377,7 +401,7 @@ impl<'a> TryFrom<&'a str> for Uri<'a> {
377401

378402
/// Authority of Uri
379403
///
380-
/// # Example
404+
/// # Examples
381405
/// ```
382406
/// use http_req::uri::Authority;
383407
/// use std::convert::TryFrom;
@@ -397,7 +421,7 @@ pub struct Authority<'a> {
397421
impl<'a> Authority<'a> {
398422
/// Returns username of this `Authority`
399423
///
400-
/// # Example
424+
/// # Examples
401425
/// ```
402426
/// use http_req::uri::Authority;
403427
/// use std::convert::TryFrom;
@@ -411,7 +435,7 @@ impl<'a> Authority<'a> {
411435

412436
/// Returns password of this `Authority`
413437
///
414-
/// # Example
438+
/// # Examples
415439
/// ```
416440
/// use http_req::uri::Authority;
417441
/// use std::convert::TryFrom;
@@ -425,7 +449,7 @@ impl<'a> Authority<'a> {
425449

426450
/// Returns information about the user
427451
///
428-
/// # Example
452+
/// # Examples
429453
/// ```
430454
/// use http_req::uri::Authority;
431455
/// use std::convert::TryFrom;
@@ -443,7 +467,7 @@ impl<'a> Authority<'a> {
443467

444468
/// Returns host of this `Authority`
445469
///
446-
/// # Example
470+
/// # Examples
447471
/// ```
448472
/// use http_req::uri::Authority;
449473
/// use std::convert::TryFrom;
@@ -457,7 +481,7 @@ impl<'a> Authority<'a> {
457481

458482
/// Returns port of this `Authority`
459483
///
460-
/// # Example
484+
/// # Examples
461485
/// ```
462486
/// use http_req::uri::Authority;
463487
/// use std::convert::TryFrom;
@@ -524,11 +548,6 @@ impl<'a> fmt::Display for Authority<'a> {
524548
}
525549
}
526550

527-
/// Removes whitespace from `text`
528-
pub fn remove_spaces(text: &mut String) {
529-
text.retain(|c| !c.is_whitespace());
530-
}
531-
532551
/// Splits `s` by `separator`. If `separator` is found inside `s`, it will return two `Some` values
533552
/// consisting `RangeC` of each `&str`. If `separator` is at the end of `s` or it's not found,
534553
/// it will return tuple consisting `Some` with `RangeC` of entire `s` inside and None.
@@ -588,15 +607,6 @@ mod tests {
588607
"?users#1551",
589608
];
590609

591-
#[test]
592-
fn remove_space() {
593-
let mut text = String::from("Hello World !");
594-
let expect = String::from("HelloWorld!");
595-
596-
remove_spaces(&mut text);
597-
assert_eq!(text, expect);
598-
}
599-
600610
#[test]
601611
fn uri_full_parse() {
602612
let uri = Uri::try_from(

0 commit comments

Comments
 (0)