use hyper::{http::uri::Builder, http::uri::Scheme, Uri};
use strum::{Display, IntoStaticStr};
use crate::errors::Error;
#[non_exhaustive]
#[derive(Debug, Display, IntoStaticStr)]
pub enum Path {
#[strum(to_string = "/ext/admin")]
Admin,
#[strum(to_string = "/ext/info")]
Info,
#[strum(to_string = "/ext/health")]
Health,
#[strum(to_string = "/ext/health/liveness")]
Liveness,
#[strum(to_string = "/ext/P")]
P,
#[strum(to_string = "/ext/bc/X")]
X,
#[strum(to_string = "/ext/bc/C/rpc")]
C,
#[strum(to_string = "{0}")]
Custom(String),
}
pub fn try_create_url(
path: Path,
scheme: Option<&str>,
host: &str,
port: Option<u16>,
) -> Result<Uri, Error> {
Builder::new()
.authority(if let Some(port) = port {
format!("{host}:{port}")
} else {
host.to_string()
})
.scheme(scheme.unwrap_or(Scheme::HTTP.as_str()))
.path_and_query(path.to_string())
.build()
.map_err(|e| Error::Other {
message: format!("http://{host}{path} failed url::try_create_url '{}'", e),
retryable: false,
})
}
#[cfg(test)]
mod tests {
use super::Path;
#[test]
fn test_try_create_url() {
let test_table: Vec<(Option<&str>, &str, Option<u16>)> =
vec![(None, "127.0.0.1", Some(9650))];
assert_eq!(
super::try_create_url(
Path::Admin,
test_table[0].0,
test_table[0].1,
test_table[0].2
)
.unwrap()
.to_string(),
"http://127.0.0.1:9650/ext/admin".to_string()
);
assert_eq!(
super::try_create_url(
Path::Liveness,
test_table[0].0,
test_table[0].1,
test_table[0].2
)
.unwrap()
.to_string(),
"http://127.0.0.1:9650/ext/health/liveness".to_string()
);
assert_eq!(
super::try_create_url(Path::C, test_table[0].0, test_table[0].1, test_table[0].2)
.unwrap()
.to_string(),
"http://127.0.0.1:9650/ext/bc/C/rpc".to_string()
);
}
}