2022-01-17 20:02:18 +00:00
|
|
|
use iri_string::types::IriStr;
|
|
|
|
|
2022-01-17 20:09:00 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-01-17 20:02:18 +00:00
|
|
|
pub struct CheckError;
|
|
|
|
|
2022-01-17 20:09:00 +00:00
|
|
|
impl std::fmt::Display for CheckError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "IRI failed host and port check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for CheckError {}
|
|
|
|
|
2022-01-17 20:13:14 +00:00
|
|
|
pub(crate) fn check<T: AsRef<IriStr>>(
|
2022-01-17 20:02:18 +00:00
|
|
|
iri: T,
|
|
|
|
host: &str,
|
|
|
|
port: Option<&str>,
|
|
|
|
) -> Result<T, CheckError> {
|
|
|
|
let authority = iri.as_ref().authority_components().ok_or(CheckError)?;
|
|
|
|
|
|
|
|
if authority.host() != host || authority.port() != port {
|
|
|
|
return Err(CheckError);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(iri)
|
|
|
|
}
|