1
0
Fork 0
website/src/helpers.rs

34 lines
1.1 KiB
Rust
Raw Normal View History

use axum::http::{uri, Uri};
pub fn uri_with_path(uri: &Uri, path: &str) -> Uri {
if path.starts_with('/') {
// 'path' is an root path, so let's just override the uri's path
return uri::Builder::new()
.scheme(uri.scheme_str().unwrap())
.authority(uri.authority().unwrap().as_str())
.path_and_query(path)
.build()
.unwrap();
}
// 'path' is a relative/local path, so let's combine it with the uri's path
let base_path = uri.path_and_query().map_or("/", |p| p.path());
if base_path.ends_with('/') {
return uri::Builder::new()
.scheme(uri.scheme_str().unwrap())
.authority(uri.authority().unwrap().as_str())
.path_and_query(format!("{base_path}{path}"))
.build()
.unwrap();
}
let (base, _) = base_path.rsplit_once('/').unwrap();
return uri::Builder::new()
.scheme(uri.scheme_str().unwrap())
.authority(uri.authority().unwrap().as_str())
.path_and_query(format!("{base}/{path}"))
.build()
.unwrap();
}