-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from uklotzde/parse_lazy_url
Parse homepage URLs of entries lazily
- Loading branch information
Showing
5 changed files
with
64 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod filter; | ||
pub mod geo; | ||
pub mod parse; | ||
pub mod sort; | ||
pub mod validate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use core::error::ParameterError; | ||
use url::{ParseError, Url}; | ||
|
||
/// Completes incomplete URLs before parsing | ||
pub fn parse_lazy_url(url: &str) -> Result<Url, ParseError> { | ||
let url = url.trim(); | ||
if url.is_empty() || url.contains("://") { | ||
Url::parse(url) | ||
} else { | ||
// Add the missing protocol by assuming https | ||
if url.starts_with("www.") { | ||
Url::parse(&format!("https://{}", url)) | ||
} else { | ||
Url::parse(&format!("https://www.{}", url)) | ||
} | ||
} | ||
} | ||
|
||
pub fn parse_url_param(url: &str) -> Result<String, ParameterError> { | ||
parse_lazy_url(url) | ||
.map(|url| url.into_string()) | ||
.map_err(|_| ParameterError::Url) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn parse_url_param() { | ||
assert!(super::parse_url_param("").is_err()); | ||
assert!(super::parse_url_param("\t \n").is_err()); | ||
assert_eq!( | ||
super::parse_url_param("example.com/index.html").unwrap(), | ||
"https://www.example.com/index.html" | ||
); | ||
assert_eq!( | ||
super::parse_url_param("www.example.com").unwrap(), | ||
"https://www.example.com/" | ||
); | ||
assert_eq!( | ||
super::parse_url_param("http://www.example.com/index.html").unwrap(), | ||
"http://www.example.com/index.html" | ||
); | ||
assert_eq!( | ||
super::parse_url_param("https://example.com").unwrap(), | ||
"https://example.com/" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters