Use UrlPartsBuilder and remove join_with_slash

This commit is contained in:
Noah Lev 2022-01-10 12:18:26 -08:00
parent 6b19cf9f74
commit 53f1bed83a
2 changed files with 16 additions and 36 deletions

View File

@ -503,28 +503,6 @@ crate enum HrefError {
NotInExternalCache, NotInExternalCache,
} }
// This mostly works with sequences of symbols, but sometimes the first item
// comes from a string, and in that case we want to trim any trailing `/`.
// `syms` can be empty.
crate fn join_with_slash(first: Option<&str>, syms: &[Symbol]) -> String {
// 64 bytes covers 99.9%+ of cases.
let mut s = String::with_capacity(64);
if let Some(first) = first {
s.push_str(first.trim_end_matches('/'));
if !syms.is_empty() {
s.push('/');
}
}
if !syms.is_empty() {
s.push_str(&syms[0].as_str());
for sym in &syms[1..] {
s.push('/');
s.push_str(&sym.as_str());
}
}
s
}
// Panics if `syms` is empty. // Panics if `syms` is empty.
crate fn join_with_double_colon(syms: &[Symbol]) -> String { crate fn join_with_double_colon(syms: &[Symbol]) -> String {
// 64 bytes covers 99.9%+ of cases. // 64 bytes covers 99.9%+ of cases.

View File

@ -26,12 +26,13 @@ use crate::formats::item_type::ItemType;
use crate::formats::{AssocItemRender, Impl, RenderMode}; use crate::formats::{AssocItemRender, Impl, RenderMode};
use crate::html::escape::Escape; use crate::html::escape::Escape;
use crate::html::format::{ use crate::html::format::{
join_with_double_colon, join_with_slash, print_abi_with_space, print_constness_with_space, join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
print_where_clause, Buffer, PrintWithSpace, Buffer, PrintWithSpace,
}; };
use crate::html::highlight; use crate::html::highlight;
use crate::html::layout::Page; use crate::html::layout::Page;
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine}; use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
use crate::html::url_parts_builder::UrlPartsBuilder;
use askama::Template; use askama::Template;
@ -854,20 +855,21 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
} }
} }
let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
.take(cx.current.len())
.chain(std::iter::once("implementors"))
.collect();
if it.def_id.is_local() {
js_src_path.extend(cx.current.iter().copied());
} else {
let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
js_src_path.extend(path[..path.len() - 1].iter().copied());
}
js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
write!( write!(
w, w,
"<script type=\"text/javascript\" \ "<script type=\"text/javascript\" src=\"{src}\" async></script>",
src=\"{root_path}/implementors/{path}/{ty}.{name}.js\" async>\ src = js_src_path.finish(),
</script>",
root_path = vec![".."; cx.current.len()].join("/"),
path = if it.def_id.is_local() {
join_with_slash(None, &cx.current)
} else {
let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
join_with_slash(None, &path[..path.len() - 1])
},
ty = it.type_(),
name = it.name.unwrap()
); );
} }