|
| 1 | +/// A builder that allows efficiently and easily constructing the part of a URL |
| 2 | +/// after the domain: `nightly/core/str/struct.Bytes.html`. |
| 3 | +/// |
| 4 | +/// This type is a wrapper around the final `String` buffer, |
| 5 | +/// but its API is like that of a `Vec` of URL components. |
| 6 | +#[derive(Debug)] |
| 7 | +crate struct UrlPartsBuilder { |
| 8 | + buf: String, |
| 9 | +} |
| 10 | + |
| 11 | +impl UrlPartsBuilder { |
| 12 | + /// Create an empty buffer. |
| 13 | + crate fn new() -> Self { |
| 14 | + Self { buf: String::new() } |
| 15 | + } |
| 16 | + |
| 17 | + /// Create an empty buffer with capacity for the specified number of bytes. |
| 18 | + fn with_capacity_bytes(count: usize) -> Self { |
| 19 | + Self { buf: String::with_capacity(count) } |
| 20 | + } |
| 21 | + |
| 22 | + /// Create a buffer with one URL component. |
| 23 | + /// |
| 24 | + /// # Examples |
| 25 | + /// |
| 26 | + /// Basic usage: |
| 27 | + /// |
| 28 | + /// ```ignore (private-type) |
| 29 | + /// let builder = UrlPartsBuilder::singleton("core"); |
| 30 | + /// assert_eq!(builder.finish(), "core"); |
| 31 | + /// ``` |
| 32 | + /// |
| 33 | + /// Adding more components afterward. |
| 34 | + /// |
| 35 | + /// ```ignore (private-type) |
| 36 | + /// let mut builder = UrlPartsBuilder::singleton("core"); |
| 37 | + /// builder.push("str"); |
| 38 | + /// builder.push_front("nightly"); |
| 39 | + /// assert_eq!(builder.finish(), "nightly/core/str"); |
| 40 | + /// ``` |
| 41 | + crate fn singleton(part: &str) -> Self { |
| 42 | + Self { buf: part.to_owned() } |
| 43 | + } |
| 44 | + |
| 45 | + /// Push a component onto the buffer. |
| 46 | + /// |
| 47 | + /// # Examples |
| 48 | + /// |
| 49 | + /// Basic usage: |
| 50 | + /// |
| 51 | + /// ```ignore (private-type) |
| 52 | + /// let mut builder = UrlPartsBuilder::new(); |
| 53 | + /// builder.push("core"); |
| 54 | + /// builder.push("str"); |
| 55 | + /// builder.push("struct.Bytes.html"); |
| 56 | + /// assert_eq!(builder.finish(), "core/str/struct.Bytes.html"); |
| 57 | + /// ``` |
| 58 | + crate fn push(&mut self, part: &str) { |
| 59 | + if !self.buf.is_empty() { |
| 60 | + self.buf.push('/'); |
| 61 | + } |
| 62 | + self.buf.push_str(part); |
| 63 | + } |
| 64 | + |
| 65 | + /// Push a component onto the front of the buffer. |
| 66 | + /// |
| 67 | + /// # Examples |
| 68 | + /// |
| 69 | + /// Basic usage: |
| 70 | + /// |
| 71 | + /// ```ignore (private-type) |
| 72 | + /// let mut builder = UrlPartsBuilder::new(); |
| 73 | + /// builder.push("core"); |
| 74 | + /// builder.push("str"); |
| 75 | + /// builder.push_front("nightly"); |
| 76 | + /// builder.push("struct.Bytes.html"); |
| 77 | + /// assert_eq!(builder.finish(), "nightly/core/str/struct.Bytes.html"); |
| 78 | + /// ``` |
| 79 | + crate fn push_front(&mut self, part: &str) { |
| 80 | + let is_empty = self.buf.is_empty(); |
| 81 | + self.buf.reserve(part.len() + if !is_empty { 1 } else { 0 }); |
| 82 | + self.buf.insert_str(0, part); |
| 83 | + if !is_empty { |
| 84 | + self.buf.insert(part.len(), '/'); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Get the final `String` buffer. |
| 89 | + crate fn finish(self) -> String { |
| 90 | + self.buf |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// This is just a guess at the average length of a URL part, |
| 95 | +/// used for [`String::with_capacity`] calls in the [`FromIterator`] |
| 96 | +/// and [`Extend`] impls. |
| 97 | +/// |
| 98 | +/// This is intentionally on the lower end to avoid overallocating. |
| 99 | +const AVG_PART_LENGTH: usize = 5; |
| 100 | + |
| 101 | +impl<'a> FromIterator<&'a str> for UrlPartsBuilder { |
| 102 | + fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self { |
| 103 | + let iter = iter.into_iter(); |
| 104 | + let mut builder = Self::with_capacity_bytes(AVG_PART_LENGTH * iter.size_hint().0); |
| 105 | + iter.for_each(|part| builder.push(part)); |
| 106 | + builder |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<'a> Extend<&'a str> for UrlPartsBuilder { |
| 111 | + fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) { |
| 112 | + let iter = iter.into_iter(); |
| 113 | + self.buf.reserve(AVG_PART_LENGTH * iter.size_hint().0); |
| 114 | + iter.for_each(|part| self.push(part)); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests; |
0 commit comments