This is a type for efficiently and easily constructing the part of a URL after the domain: `nightly/core/str/struct.Bytes.html`. It allows simplifying some code and avoiding some allocations in the `href_*` functions. It will also allow making `Cache.paths` et al. use `Symbol` without having to allocate `String`s in the `href_*` functions. `String`s would be necessary otherwise because `Symbol::as_str()` returns `SymbolStr`, whose `Deref<Target = str>` impl requires the `str` to not outlive it. This is the primary motivation for the addition of `UrlPartsBuilder`.
44 lines
1.4 KiB
Rust
44 lines
1.4 KiB
Rust
use crate::html::format::href_relative_parts;
|
|
|
|
fn assert_relative_path(expected: &str, relative_to_fqp: &[&str], fqp: &[&str]) {
|
|
let relative_to_fqp: Vec<String> = relative_to_fqp.iter().copied().map(String::from).collect();
|
|
let fqp: Vec<String> = fqp.iter().copied().map(String::from).collect();
|
|
assert_eq!(expected, href_relative_parts(&fqp, &relative_to_fqp).finish());
|
|
}
|
|
|
|
#[test]
|
|
fn href_relative_parts_basic() {
|
|
let relative_to_fqp = &["std", "vec"];
|
|
let fqp = &["std", "iter"];
|
|
assert_relative_path("../iter", relative_to_fqp, fqp);
|
|
}
|
|
#[test]
|
|
fn href_relative_parts_parent_module() {
|
|
let relative_to_fqp = &["std", "vec"];
|
|
let fqp = &["std"];
|
|
assert_relative_path("..", relative_to_fqp, fqp);
|
|
}
|
|
#[test]
|
|
fn href_relative_parts_different_crate() {
|
|
let relative_to_fqp = &["std", "vec"];
|
|
let fqp = &["core", "iter"];
|
|
assert_relative_path("../../core/iter", relative_to_fqp, fqp);
|
|
}
|
|
#[test]
|
|
fn href_relative_parts_same_module() {
|
|
let relative_to_fqp = &["std", "vec"];
|
|
let fqp = &["std", "vec"];
|
|
assert_relative_path("", relative_to_fqp, fqp);
|
|
}
|
|
#[test]
|
|
fn href_relative_parts_child_module() {
|
|
let relative_to_fqp = &["std"];
|
|
let fqp = &["std", "vec"];
|
|
assert_relative_path("vec", relative_to_fqp, fqp);
|
|
}
|
|
#[test]
|
|
fn href_relative_parts_root() {
|
|
let relative_to_fqp = &[];
|
|
let fqp = &["std"];
|
|
assert_relative_path("std", relative_to_fqp, fqp);
|
|
}
|