-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
89 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ authors = ["Oleh Martsokha <[email protected]>"] | |
license = "MIT" | ||
|
||
[workspace.dependencies] | ||
tokio = { version = "1.38", default-features = false } | ||
tokio = { version = "1", default-features = false } | ||
futures-io = { version = "0.3", default-features = false } | ||
futures-util = { version = "0.3", default-features = false } | ||
futures-test = { version = "0.3", default-features = false } | ||
|
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
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
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,7 +1,16 @@ | ||
use url::Url; | ||
|
||
use crate::{Error, Result}; | ||
|
||
/// Returns the expected path to the `robots.txt` file | ||
/// as the [`url::Url`]. | ||
/// as the `url::`[`Url`]. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns the error if the provided [`Url`] cannot be a base, | ||
/// does not have a host or the schema is not `http` or `https`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// use url::Url; | ||
|
@@ -12,38 +21,43 @@ use crate::{Error, Result}; | |
/// let robots = create_url(&path).unwrap().to_string(); | ||
/// assert_eq!(robots, "https://example.com/robots.txt") | ||
/// ``` | ||
pub fn create_url(path: &url::Url) -> Result<url::Url> { | ||
pub fn create_url(path: &Url) -> Result<Url> { | ||
let mut path = path.clone(); | ||
|
||
if path.cannot_be_a_base() { | ||
return Err(Error::CannotBeBase); | ||
} | ||
|
||
if path.host().is_none() { | ||
return Err(Error::NoHost); | ||
} | ||
|
||
if path.scheme() != "http" && path.scheme() != "https" { | ||
return Err(Error::WrongScheme { | ||
scheme: path.scheme().to_string(), | ||
}); | ||
} | ||
|
||
if !path.username().is_empty() { | ||
path.set_username("").unwrap(); | ||
path.set_username("").expect("should pass base/host tests"); | ||
} | ||
|
||
if path.password().is_some() { | ||
path.set_password(None).unwrap(); | ||
path.set_password(None) | ||
.expect("should pass base/host tests"); | ||
} | ||
|
||
path.join("/robots.txt").map_err(Into::into) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::{create_url, url::Url, Result}; | ||
|
||
#[test] | ||
fn from_url() -> Result<()> { | ||
let path = "https://user:[email protected]/foo/sample.txt"; | ||
let path = url::Url::parse(path).unwrap(); | ||
let path = Url::parse(path).unwrap(); | ||
|
||
let robots = create_url(&path)?.to_string(); | ||
assert_eq!(robots, "https://example.com/robots.txt"); | ||
|
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
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