-
Notifications
You must be signed in to change notification settings - Fork 13.4k
rustdoc: Add UrlPartsBuilder
#91871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
rustdoc: Add UrlPartsBuilder
#91871
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
use crate::html::format::href_relative_parts; | ||
|
||
fn assert_relative_path(expected: &[&str], relative_to_fqp: &[&str], fqp: &[&str]) { | ||
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)); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
assert_relative_path("std", relative_to_fqp, fqp); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/// A builder that allows efficiently and easily constructing the part of a URL | ||
/// after the domain: `nightly/core/str/struct.Bytes.html`. | ||
/// | ||
/// This type is a wrapper around the final `String` buffer, | ||
/// but its API is like that of a `Vec` of URL components. | ||
#[derive(Debug)] | ||
crate struct UrlPartsBuilder { | ||
buf: String, | ||
} | ||
|
||
impl UrlPartsBuilder { | ||
/// Create an empty buffer. | ||
crate fn new() -> Self { | ||
Self { buf: String::new() } | ||
} | ||
|
||
/// Create an empty buffer with capacity for the specified number of bytes. | ||
fn with_capacity_bytes(count: usize) -> Self { | ||
Self { buf: String::with_capacity(count) } | ||
} | ||
|
||
/// Create a buffer with one URL component. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ```ignore (private-type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any way to run doctests for private code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that I know of... |
||
/// let builder = UrlPartsBuilder::singleton("core"); | ||
/// assert_eq!(builder.finish(), "core"); | ||
/// ``` | ||
/// | ||
/// Adding more components afterward. | ||
/// | ||
/// ```ignore (private-type) | ||
/// let mut builder = UrlPartsBuilder::singleton("core"); | ||
/// builder.push("str"); | ||
/// builder.push_front("nightly"); | ||
/// assert_eq!(builder.finish(), "nightly/core/str"); | ||
/// ``` | ||
crate fn singleton(part: &str) -> Self { | ||
Self { buf: part.to_owned() } | ||
} | ||
|
||
/// Push a component onto the buffer. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ```ignore (private-type) | ||
/// let mut builder = UrlPartsBuilder::new(); | ||
/// builder.push("core"); | ||
/// builder.push("str"); | ||
/// builder.push("struct.Bytes.html"); | ||
/// assert_eq!(builder.finish(), "core/str/struct.Bytes.html"); | ||
/// ``` | ||
crate fn push(&mut self, part: &str) { | ||
if !self.buf.is_empty() { | ||
self.buf.push('/'); | ||
} | ||
self.buf.push_str(part); | ||
} | ||
|
||
/// Push a component onto the front of the buffer. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ```ignore (private-type) | ||
/// let mut builder = UrlPartsBuilder::new(); | ||
/// builder.push("core"); | ||
/// builder.push("str"); | ||
/// builder.push_front("nightly"); | ||
/// builder.push("struct.Bytes.html"); | ||
/// assert_eq!(builder.finish(), "nightly/core/str/struct.Bytes.html"); | ||
/// ``` | ||
crate fn push_front(&mut self, part: &str) { | ||
let is_empty = self.buf.is_empty(); | ||
self.buf.reserve(part.len() + if !is_empty { 1 } else { 0 }); | ||
self.buf.insert_str(0, part); | ||
if !is_empty { | ||
self.buf.insert(part.len(), '/'); | ||
} | ||
} | ||
|
||
/// Get the final `String` buffer. | ||
crate fn finish(self) -> String { | ||
self.buf | ||
} | ||
} | ||
|
||
/// This is just a guess at the average length of a URL part, | ||
/// used for [`String::with_capacity`] calls in the [`FromIterator`] | ||
/// and [`Extend`] impls. | ||
/// | ||
/// This is intentionally on the lower end to avoid overallocating. | ||
const AVG_PART_LENGTH: usize = 5; | ||
|
||
impl<'a> FromIterator<&'a str> for UrlPartsBuilder { | ||
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self { | ||
let iter = iter.into_iter(); | ||
let mut builder = Self::with_capacity_bytes(AVG_PART_LENGTH * iter.size_hint().0); | ||
iter.for_each(|part| builder.push(part)); | ||
builder | ||
} | ||
} | ||
|
||
impl<'a> Extend<&'a str> for UrlPartsBuilder { | ||
fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) { | ||
let iter = iter.into_iter(); | ||
self.buf.reserve(AVG_PART_LENGTH * iter.size_hint().0); | ||
iter.for_each(|part| self.push(part)); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use super::*; | ||
|
||
fn t(builder: UrlPartsBuilder, expect: &str) { | ||
assert_eq!(builder.finish(), expect); | ||
} | ||
|
||
#[test] | ||
fn empty() { | ||
t(UrlPartsBuilder::new(), ""); | ||
} | ||
|
||
#[test] | ||
fn singleton() { | ||
t(UrlPartsBuilder::singleton("index.html"), "index.html"); | ||
} | ||
|
||
#[test] | ||
fn push_several() { | ||
let mut builder = UrlPartsBuilder::new(); | ||
builder.push("core"); | ||
builder.push("str"); | ||
builder.push("struct.Bytes.html"); | ||
t(builder, "core/str/struct.Bytes.html"); | ||
} | ||
|
||
#[test] | ||
fn push_front_empty() { | ||
let mut builder = UrlPartsBuilder::new(); | ||
builder.push_front("page.html"); | ||
t(builder, "page.html"); | ||
} | ||
|
||
#[test] | ||
fn push_front_non_empty() { | ||
let mut builder = UrlPartsBuilder::new(); | ||
builder.push("core"); | ||
builder.push("str"); | ||
builder.push("struct.Bytes.html"); | ||
builder.push_front("nightly"); | ||
t(builder, "nightly/core/str/struct.Bytes.html"); | ||
} | ||
|
||
#[test] | ||
fn collect() { | ||
t(["core", "str"].into_iter().collect(), "core/str"); | ||
t(["core", "str", "struct.Bytes.html"].into_iter().collect(), "core/str/struct.Bytes.html"); | ||
} | ||
|
||
#[test] | ||
fn extend() { | ||
let mut builder = UrlPartsBuilder::singleton("core"); | ||
builder.extend(["str", "struct.Bytes.html"]); | ||
t(builder, "core/str/struct.Bytes.html"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.