-
Notifications
You must be signed in to change notification settings - Fork 4
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
27 changed files
with
1,211 additions
and
984 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,9 @@ | ||
pub mod host_and_port; | ||
pub mod keepalive; | ||
pub mod metered; | ||
pub mod path_or_uri; | ||
|
||
pub use host_and_port::HostAndPort; | ||
pub use keepalive::TcpKeepAlive; | ||
pub use metered::Metered; | ||
pub use path_or_uri::PathOrUri; |
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,70 @@ | ||
use std::{fmt::Display, path::PathBuf, str::FromStr}; | ||
|
||
use http::Uri; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum PathOrUri { | ||
Path(PathBuf), | ||
Uri(Uri), | ||
} | ||
|
||
impl Display for PathOrUri { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Path(p) => p.display().fmt(f), | ||
Self::Uri(u) => u.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for PathOrUri { | ||
type Err = http::uri::InvalidUri; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if s.starts_with("http://") || s.starts_with("https://") { | ||
Ok(Self::from(s.parse::<Uri>()?)) | ||
} else { | ||
Ok(Self::from(PathBuf::from(s))) | ||
} | ||
} | ||
} | ||
|
||
impl From<PathBuf> for PathOrUri { | ||
fn from(path: PathBuf) -> Self { | ||
Self::Path(path) | ||
} | ||
} | ||
|
||
impl From<Uri> for PathOrUri { | ||
fn from(uri: Uri) -> Self { | ||
Self::Uri(uri) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::PathOrUri; | ||
use http::Uri; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn path_or_uri_test() -> Result<(), Box<dyn std::error::Error>> { | ||
assert_eq!( | ||
PathOrUri::Path(PathBuf::from("README.md")), | ||
"README.md".parse::<PathOrUri>()? | ||
); | ||
assert_eq!( | ||
PathOrUri::Path(PathBuf::from("/etc/motd")), | ||
"/etc/motd".parse::<PathOrUri>()? | ||
); | ||
assert_eq!( | ||
PathOrUri::Uri("http://example.org/index.html".parse::<Uri>()?), | ||
"http://example.org/index.html".parse::<PathOrUri>()? | ||
); | ||
assert_eq!( | ||
PathOrUri::Uri("https://example.org/index.html".parse::<Uri>()?), | ||
"https://example.org/index.html".parse::<PathOrUri>()? | ||
); | ||
Ok(()) | ||
} | ||
} |
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
Oops, something went wrong.