-
Notifications
You must be signed in to change notification settings - Fork 778
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
1 parent
e014bfb
commit 0c76816
Showing
16 changed files
with
145 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A trusted host, which could be a host or a host-port pair. | ||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub enum TrustedHost { | ||
Host(String), | ||
HostPort(String, u16), | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum TrustedHostError { | ||
#[error("missing host for `--trusted-host`: `{0}`")] | ||
MissingHost(String), | ||
#[error("invalid port for `--trusted-host`: `{0}`")] | ||
InvalidPort(String), | ||
} | ||
|
||
impl std::str::FromStr for TrustedHost { | ||
type Err = TrustedHostError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut parts = s.splitn(2, ':'); | ||
let host = parts | ||
.next() | ||
.ok_or_else(|| TrustedHostError::MissingHost(s.to_string()))?; | ||
let port = parts | ||
.next() | ||
.map(str::parse) | ||
.transpose() | ||
.map_err(|_| TrustedHostError::InvalidPort(s.to_string()))?; | ||
|
||
match port { | ||
Some(port) => Ok(TrustedHost::HostPort(host.to_string(), port)), | ||
None => Ok(TrustedHost::Host(host.to_string())), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "schemars")] | ||
impl schemars::JsonSchema for TrustedHost { | ||
fn schema_name() -> String { | ||
"TrustedHost".to_string() | ||
} | ||
|
||
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { | ||
schemars::schema::SchemaObject { | ||
instance_type: Some(schemars::schema::InstanceType::String.into()), | ||
metadata: Some(Box::new(schemars::schema::Metadata { | ||
description: Some("A host or host-port pair.".to_string()), | ||
..schemars::schema::Metadata::default() | ||
})), | ||
..schemars::schema::SchemaObject::default() | ||
} | ||
.into() | ||
} | ||
} |
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.