| 1 | //! Module for normalization. |
| 2 | |
| 3 | use craterust-analyzer cargo fluent-uri 0.4.1 crate/::{ |
| 4 | componentrust-analyzer cargo fluent-uri 0.4.1 component/::Schemerust-analyzer cargo fluent-uri 0.4.1 component/Scheme#, |
| 5 | imprust-analyzer cargo fluent-uri 0.4.1 imp/::{HostMetarust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#, Metarust-analyzer cargo fluent-uri 0.4.1 imp/Meta#, RiMaybeRefrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#, RmrRefrust-analyzer cargo fluent-uri 0.4.1 imp/RmrRef#}, |
| 6 | parserust-analyzer cargo fluent-uri 0.4.1 parse/, |
| 7 | pct_encrust-analyzer cargo fluent-uri 0.4.1 pct_enc/::{ |
| 8 | selfrust-analyzer cargo fluent-uri 0.4.1 pct_enc/, |
| 9 | encoderrust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/::{Datarust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/Data#, IDatarust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/IData#}, |
| 10 | Decoderust-analyzer cargo fluent-uri 0.4.1 pct_enc/Decode#, DecodedChunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedChunk#, DecodedUtf8Chunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedUtf8Chunk#, Encoderust-analyzer cargo fluent-uri 0.4.1 pct_enc/Encode#, EncodedChunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/EncodedChunk#, Encoderrust-analyzer cargo fluent-uri 0.4.1 pct_enc/Encoder#, Tablerust-analyzer cargo fluent-uri 0.4.1 pct_enc/table/Table#, |
| 11 | }, |
| 12 | resolverust-analyzer cargo fluent-uri 0.4.1 resolve/, |
| 13 | }; |
| 14 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 15 | use borrow_or_sharerust-analyzer cargo borrow-or-share 0.2.4 crate/::Bosrust-analyzer cargo borrow-or-share 0.2.4 Bos#; |
| 16 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{ |
| 17 | fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#}, |
| 18 | numrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/::NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize#, |
| 19 | }; |
| 20 | |
| 21 | /// An error occurred when normalizing a URI/IRI (reference). |
| 22 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 23 | pub enum NormalizeErrorrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError# { |
| 24 | /// An underflow occurred in path normalization. |
| 25 | /// |
| 26 | /// Used only when [`Normalizer::allow_path_underflow`] is set to `false`. |
| 27 | PathUnderflowrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError#PathUnderflow#, |
| 28 | } |
| 29 | |
| 30 | impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for NormalizeErrorrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError# { |
| 31 | fn fmtrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[NormalizeError][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 32 | let msg = match self { |
| 33 | Selfrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[NormalizeError][Display]::PathUnderflowrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError#PathUnderflow# => "underflow occurred in path resolution", |
| 34 | }; |
| 35 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(msg) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[cfg(feature = "impl-error")] |
| 40 | impl craterust-analyzer cargo fluent-uri 0.4.1 crate/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for NormalizeErrorrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError# {} |
| 41 | |
| 42 | /// A configurable URI/IRI (reference) normalizer. |
| 43 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)] |
| 44 | #[allow(missing_debug_implementations)] |
| 45 | #[must_use] |
| 46 | pub struct Normalizerrust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer# { |
| 47 | allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#allow_path_underflow.: bool, |
| 48 | default_port_frust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#default_port_f.: fn(&Schemerust-analyzer cargo fluent-uri 0.4.1 component/Scheme#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16>, |
| 49 | } |
| 50 | |
| 51 | impl Normalizerrust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer# { |
| 52 | /// Creates a new `Normalizer` with default configuration. |
| 53 | pub fn newrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer]new().() -> Selfrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer] { |
| 54 | Selfrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer] { |
| 55 | allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#allow_path_underflow.: true, |
| 56 | default_port_frust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#default_port_f.: Schemerust-analyzer cargo fluent-uri 0.4.1 component/Scheme#::default_portrust-analyzer cargo fluent-uri 0.4.1 component/impl#[Scheme]default_port()., |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Sets whether to allow underflow in path normalization. |
| 61 | /// |
| 62 | /// This defaults to `true`. A value of `false` is a deviation from the |
| 63 | /// normalization methods described in |
| 64 | /// [Section 6 of RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986/#section-6). |
| 65 | /// |
| 66 | /// # Examples |
| 67 | /// |
| 68 | /// ``` |
| 69 | /// use fluent_uri::{normalize::{Normalizer, NormalizeError}, Uri}; |
| 70 | /// |
| 71 | /// let normalizer = Normalizer::new().allow_path_underflow(false); |
| 72 | /// let uri = Uri::parse("http://example.com/..")?; |
| 73 | /// |
| 74 | /// assert_eq!(normalizer.normalize(&uri).unwrap_err(), NormalizeError::PathUnderflow); |
| 75 | /// # Ok::<_, fluent_uri::ParseError>(()) |
| 76 | /// ``` |
| 77 | pub fn allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer]allow_path_underflow().(mut self, value: bool) -> Selfrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer] { |
| 78 | self.allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#allow_path_underflow. = value; |
| 79 | self |
| 80 | } |
| 81 | |
| 82 | /// Sets the function with which to get the default port of a scheme. |
| 83 | /// |
| 84 | /// This defaults to [`Scheme::default_port`]. |
| 85 | /// |
| 86 | /// # Examples |
| 87 | /// |
| 88 | /// ``` |
| 89 | /// use fluent_uri::{component::Scheme, normalize::Normalizer, Uri}; |
| 90 | /// |
| 91 | /// const SCHEME_FOO: &Scheme = Scheme::new_or_panic("foo"); |
| 92 | /// |
| 93 | /// let normalizer = Normalizer::new().default_port_with(|scheme| { |
| 94 | /// if scheme == SCHEME_FOO { |
| 95 | /// Some(4673) |
| 96 | /// } else { |
| 97 | /// scheme.default_port() |
| 98 | /// } |
| 99 | /// }); |
| 100 | /// let uri = Uri::parse("foo://localhost:4673")?; |
| 101 | /// |
| 102 | /// assert_eq!(normalizer.normalize(&uri).unwrap(), "foo://localhost"); |
| 103 | /// # Ok::<_, fluent_uri::ParseError>(()) |
| 104 | /// ``` |
| 105 | pub fn default_port_withrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer]default_port_with().(mut self, f: fn(&Schemerust-analyzer cargo fluent-uri 0.4.1 component/Scheme#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16>) -> Selfrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer] { |
| 106 | self.default_port_frust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#default_port_f. = f; |
| 107 | self |
| 108 | } |
| 109 | |
| 110 | /// Normalizes the given URI/IRI (reference). |
| 111 | /// |
| 112 | /// See [`Uri::normalize`][crate::Uri::normalize] for the exact behavior of this method. |
| 113 | /// |
| 114 | /// # Errors |
| 115 | /// |
| 116 | /// Returns `Err` if an underflow occurred in path normalization |
| 117 | /// when [`allow_path_underflow`] is set to `false`. |
| 118 | /// |
| 119 | /// [`allow_path_underflow`]: Self::allow_path_underflow |
| 120 | pub fn normalizerust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer]normalize().<R: RiMaybeRefrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#>(&self, r: &R) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<R::WithValrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#WithVal#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, NormalizeErrorrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError#> |
| 121 | where |
| 122 | R::Valrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#Val#: Bosrust-analyzer cargo borrow-or-share 0.2.4 Bos#<str>, |
| 123 | { |
| 124 | normalizerust-analyzer cargo fluent-uri 0.4.1 normalize/normalize().( |
| 125 | r.make_refrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#make_ref().(), |
| 126 | R::CONSTRAINTSrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#CONSTRAINTS..ascii_onlyrust-analyzer cargo fluent-uri 0.4.1 imp/Constraints#ascii_only., |
| 127 | self.allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#allow_path_underflow., |
| 128 | self.default_port_frust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer#default_port_f., |
| 129 | ) |
| 130 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(RiMaybeRefrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#::from_pairrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#from_pair().) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Normalizerrust-analyzer cargo fluent-uri 0.4.1 normalize/Normalizer# { |
| 135 | fn defaultrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer][Default]default().() -> Selfrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer][Default] { |
| 136 | Selfrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer][Default]::newrust-analyzer cargo fluent-uri 0.4.1 normalize/impl#[Normalizer]new().() |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | pub(craterust-analyzer cargo fluent-uri 0.4.1 crate/) fn normalizerust-analyzer cargo fluent-uri 0.4.1 normalize/normalize().( |
| 141 | r: RmrRefrust-analyzer cargo fluent-uri 0.4.1 imp/RmrRef#<'_, '_>, |
| 142 | ascii_only: bool, |
| 143 | allow_path_underflow: bool, |
| 144 | default_port_f: fn(&Schemerust-analyzer cargo fluent-uri 0.4.1 component/Scheme#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16>, |
| 145 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Metarust-analyzer cargo fluent-uri 0.4.1 imp/Meta#), NormalizeErrorrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError#> { |
| 146 | // For "a://[::ffff:5:9]/" the capacity is not enough, |
| 147 | // but it's fine since this rarely happens. |
| 148 | let mut buf = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(r.as_strrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]as_str().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()); |
| 149 | |
| 150 | let path = r.pathrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]path().().as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().(); |
| 151 | let mut path_buf = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(path.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()); |
| 152 | |
| 153 | let data_table = if ascii_only { |
| 154 | Datarust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/Data#::TABLErust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/impl#[Data][Encoder]TABLE. |
| 155 | } else { |
| 156 | IDatarust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/IData#::TABLErust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/impl#[IData][Encoder]TABLE. |
| 157 | }; |
| 158 | |
| 159 | if r.has_schemerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]has_scheme().() && path.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('/') { |
| 160 | normalize_estrrust-analyzer cargo fluent-uri 0.4.1 normalize/normalize_estr().(&mut buf, path, false, data_table); |
| 161 | |
| 162 | let underflow_occurred = resolverust-analyzer cargo fluent-uri 0.4.1 resolve/::remove_dot_segmentsrust-analyzer cargo fluent-uri 0.4.1 resolve/remove_dot_segments().(&mut path_buf, 0, &[&buf]); |
| 163 | if underflow_occurred && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().allow_path_underflow { |
| 164 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(NormalizeErrorrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError#::PathUnderflowrust-analyzer cargo fluent-uri 0.4.1 normalize/NormalizeError#PathUnderflow#); |
| 165 | } |
| 166 | |
| 167 | buf.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 168 | } else { |
| 169 | // Don't remove dot segments from relative reference or rootless path. |
| 170 | normalize_estrrust-analyzer cargo fluent-uri 0.4.1 normalize/normalize_estr().(&mut path_buf, path, false, data_table); |
| 171 | } |
| 172 | |
| 173 | let mut meta = Metarust-analyzer cargo fluent-uri 0.4.1 imp/Meta#::defaultrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[Meta][Default]default().(); |
| 174 | |
| 175 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(scheme) = r.scheme_optrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]scheme_opt().() { |
| 176 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(scheme.as_strrust-analyzer cargo fluent-uri 0.4.1 component/impl#[Scheme]as_str().()); |
| 177 | buf.make_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]make_ascii_lowercase().(); |
| 178 | meta.scheme_endrust-analyzer cargo fluent-uri 0.4.1 imp/Meta#scheme_end. = NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[`NonZero<T>`]new().(buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().()); |
| 179 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(':'); |
| 180 | } |
| 181 | |
| 182 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(auth) = r.authorityrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]authority().() { |
| 183 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("//"); |
| 184 | |
| 185 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(userinfo) = auth.userinforust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]userinfo().() { |
| 186 | normalize_estrrust-analyzer cargo fluent-uri 0.4.1 normalize/normalize_estr().(&mut buf, userinfo.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().(), false, data_table); |
| 187 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('@'); |
| 188 | } |
| 189 | |
| 190 | let mut auth_meta = auth.metarust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]meta().(); |
| 191 | auth_meta.host_boundsrust-analyzer cargo fluent-uri 0.4.1 imp/AuthMeta#host_bounds..0 = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 192 | match auth_meta.host_metarust-analyzer cargo fluent-uri 0.4.1 imp/AuthMeta#host_meta. { |
| 193 | // An IPv4 address is always canonical. |
| 194 | HostMetarust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#::Ipv4rust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#Ipv4#(..) => buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(auth.hostrust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]host().()), |
| 195 | #[cfg(feature = "net")] |
| 196 | HostMetarust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#::Ipv6rust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#Ipv6#(addr) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "[{addr}]").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 197 | #[cfg(not(feature = "net"))] |
| 198 | HostMetarust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#::Ipv6rust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#Ipv6#() => { |
| 199 | buf.push('['); |
| 200 | write_v6(&mut buf, parserust-analyzer cargo fluent-uri 0.4.1 parse/::parse_v6(&auth.host().as_bytes()[1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#])); |
| 201 | buf.push(']'); |
| 202 | } |
| 203 | HostMetarust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#::IpvFuturerust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#IpvFuture# => { |
| 204 | let start = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 205 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(auth.hostrust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]host().()); |
| 206 | |
| 207 | buf[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`IndexMut<I>`]index_mut().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`IndexMut<I>`]index_mut()..make_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]make_ascii_lowercase().(); |
| 208 | } |
| 209 | HostMetarust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#::RegNamerust-analyzer cargo fluent-uri 0.4.1 imp/HostMeta#RegName# => { |
| 210 | let start = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 211 | let host = auth.hostrust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]host().(); |
| 212 | normalize_estrrust-analyzer cargo fluent-uri 0.4.1 normalize/normalize_estr().(&mut buf, host, true, data_table); |
| 213 | |
| 214 | if buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().host.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() { |
| 215 | // Only reparse when the length is less than before. |
| 216 | auth_meta.host_metarust-analyzer cargo fluent-uri 0.4.1 imp/AuthMeta#host_meta. = parserust-analyzer cargo fluent-uri 0.4.1 parse/::parse_v4_or_reg_namerust-analyzer cargo fluent-uri 0.4.1 parse/parse_v4_or_reg_name().(&buf.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | auth_meta.host_boundsrust-analyzer cargo fluent-uri 0.4.1 imp/AuthMeta#host_bounds..1 = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 221 | meta.auth_metarust-analyzer cargo fluent-uri 0.4.1 imp/Meta#auth_meta. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(auth_meta); |
| 222 | |
| 223 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(port) = auth.portrust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]port().() { |
| 224 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().port.is_emptyrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]is_empty().() { |
| 225 | let mut eq_default = false; |
| 226 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(scheme) = r.scheme_optrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]scheme_opt().() { |
| 227 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(default) = default_port_f(scheme) { |
| 228 | eq_default = port.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(default); |
| 229 | } |
| 230 | } |
| 231 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().eq_default { |
| 232 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(':'); |
| 233 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(port.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().()); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | meta.path_boundsrust-analyzer cargo fluent-uri 0.4.1 imp/Meta#path_bounds..0 = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 240 | // Make sure that the output is a valid URI/IRI reference. |
| 241 | if r.has_schemerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]has_scheme().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().r.has_authorityrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]has_authority().() && path_buf.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("//") { |
| 242 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("/."); |
| 243 | } |
| 244 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&path_buf); |
| 245 | meta.path_boundsrust-analyzer cargo fluent-uri 0.4.1 imp/Meta#path_bounds..1 = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 246 | |
| 247 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(query) = r.queryrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]query().() { |
| 248 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('?'); |
| 249 | |
| 250 | const IQUERY_DATArust-analyzer cargo fluent-uri 0.4.1 normalize/IQUERY_DATA.: &Tablerust-analyzer cargo fluent-uri 0.4.1 pct_enc/table/Table# = &IDatarust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/IData#::TABLErust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/impl#[IData][Encoder]TABLE..or_iprivaterust-analyzer cargo fluent-uri 0.4.1 pct_enc/table/impl#[Table]or_iprivate().(); |
| 251 | let query_data_table = if ascii_only { Datarust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/Data#::TABLErust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/impl#[Data][Encoder]TABLE. } else { IQUERY_DATArust-analyzer cargo fluent-uri 0.4.1 normalize/IQUERY_DATA. }; |
| 252 | |
| 253 | normalize_estrrust-analyzer cargo fluent-uri 0.4.1 normalize/normalize_estr().(&mut buf, query.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().(), false, query_data_table); |
| 254 | meta.query_endrust-analyzer cargo fluent-uri 0.4.1 imp/Meta#query_end. = NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[`NonZero<T>`]new().(buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().()); |
| 255 | } |
| 256 | |
| 257 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(fragment) = r.fragmentrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]fragment().() { |
| 258 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('#'); |
| 259 | normalize_estrrust-analyzer cargo fluent-uri 0.4.1 normalize/normalize_estr().(&mut buf, fragment.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().(), false, data_table); |
| 260 | } |
| 261 | |
| 262 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((buf, meta)) |
| 263 | } |
| 264 | |
| 265 | fn normalize_estrrust-analyzer cargo fluent-uri 0.4.1 normalize/normalize_estr().(buf: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, s: &str, to_ascii_lowercase: bool, table: &Tablerust-analyzer cargo fluent-uri 0.4.1 pct_enc/table/Table#) { |
| 266 | if table.allows_non_asciirust-analyzer cargo fluent-uri 0.4.1 pct_enc/table/impl#[Table]allows_non_ascii().() { |
| 267 | Decoderust-analyzer cargo fluent-uri 0.4.1 pct_enc/Decode#::newrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`Decode<'a>`]new().(s).decode_utf8rust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`Decode<'a>`]decode_utf8().(|chunk| match chunk { |
| 268 | DecodedUtf8Chunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedUtf8Chunk#::Unencodedrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedUtf8Chunk#Unencoded#(s) => { |
| 269 | let i = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 270 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(s); |
| 271 | if to_ascii_lowercase { |
| 272 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..make_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]make_ascii_lowercase().(); |
| 273 | } |
| 274 | } |
| 275 | DecodedUtf8Chunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedUtf8Chunk#::Decodedrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedUtf8Chunk#Decoded# { validrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedUtf8Chunk#Decoded#valid., invalidrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedUtf8Chunk#Decoded#invalid. } => { |
| 276 | for chunk in Encoderust-analyzer cargo fluent-uri 0.4.1 pct_enc/Encode#::newrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`Encode<'t, 's>`]new().(table, valid) { |
| 277 | match chunk { |
| 278 | EncodedChunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/EncodedChunk#::Unencodedrust-analyzer cargo fluent-uri 0.4.1 pct_enc/EncodedChunk#Unencoded#(s) => { |
| 279 | let i = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 280 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(s); |
| 281 | if to_ascii_lowercase { |
| 282 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..make_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]make_ascii_lowercase().(); |
| 283 | } |
| 284 | } |
| 285 | EncodedChunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/EncodedChunk#::PctEncodedrust-analyzer cargo fluent-uri 0.4.1 pct_enc/EncodedChunk#PctEncoded#(s) => buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(s), |
| 286 | } |
| 287 | } |
| 288 | for &x in invalid { |
| 289 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(pct_encrust-analyzer cargo fluent-uri 0.4.1 pct_enc/::encode_byterust-analyzer cargo fluent-uri 0.4.1 pct_enc/encode_byte().(x)); |
| 290 | } |
| 291 | } |
| 292 | }); |
| 293 | } else { |
| 294 | for chunk in Decoderust-analyzer cargo fluent-uri 0.4.1 pct_enc/Decode#::newrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`Decode<'a>`]new().(s) { |
| 295 | match chunk { |
| 296 | DecodedChunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedChunk#::Unencodedrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedChunk#Unencoded#(s) => { |
| 297 | let i = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 298 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(s); |
| 299 | if to_ascii_lowercase { |
| 300 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..make_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]make_ascii_lowercase().(); |
| 301 | } |
| 302 | } |
| 303 | DecodedChunkrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedChunk#::PctDecodedrust-analyzer cargo fluent-uri 0.4.1 pct_enc/DecodedChunk#PctDecoded#(mut x) => { |
| 304 | if table.allows_asciirust-analyzer cargo fluent-uri 0.4.1 pct_enc/table/impl#[Table]allows_ascii().(x) { |
| 305 | if to_ascii_lowercase { |
| 306 | x.make_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]make_ascii_lowercase().(); |
| 307 | } |
| 308 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(x as char); |
| 309 | } else { |
| 310 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(pct_encrust-analyzer cargo fluent-uri 0.4.1 pct_enc/::encode_byterust-analyzer cargo fluent-uri 0.4.1 pct_enc/encode_byte().(x)); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Taken from `impl Display for Ipv6Addr`. |
| 319 | #[cfg(not(feature = "net"))] |
| 320 | fn write_v6(buf: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, segments: [u16; 8]) { |
| 321 | if let [0, 0, 0, 0, 0, 0xffff, ab, cd] = segments { |
| 322 | let [a, b] = ab.to_be_bytes(); |
| 323 | let [c, d] = cd.to_be_bytes(); |
| 324 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "::ffff:{a}.{b}.{c}.{d}").unwrap(); |
| 325 | } else { |
| 326 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copy, Clone, Default)] |
| 327 | struct Span { |
| 328 | start: usize, |
| 329 | len: usize, |
| 330 | } |
| 331 | |
| 332 | // Find the inner 0 span |
| 333 | let zeroes = { |
| 334 | let mut longest = Span::default(); |
| 335 | let mut current = Span::default(); |
| 336 | |
| 337 | for (i, &segment) in segments.iter().enumerate() { |
| 338 | if segment == 0 { |
| 339 | if current.len == 0 { |
| 340 | current.start = i; |
| 341 | } |
| 342 | |
| 343 | current.len += 1; |
| 344 | |
| 345 | if current.len > longest.len { |
| 346 | longest = current; |
| 347 | } |
| 348 | } else { |
| 349 | current = Span::default(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | longest |
| 354 | }; |
| 355 | |
| 356 | /// Write a colon-separated part of the address |
| 357 | #[inline] |
| 358 | fn write_subslice(buf: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, chunk: &[u16]) { |
| 359 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((first, tail)) = chunk.split_first() { |
| 360 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "{first:x}").unwrap(); |
| 361 | for segment in tail { |
| 362 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, ":{segment:x}").unwrap(); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if zeroes.len > 1 { |
| 368 | write_subslice(buf, &segments[..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#zeroes.start]); |
| 369 | buf.push_str("::"); |
| 370 | write_subslice(buf, &segments[zeroes.start + zeroes.len..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]); |
| 371 | } else { |
| 372 | write_subslice(buf, &segments); |
| 373 | } |
| 374 | } |
| 375 | } |