| 1 | //! Module for reference resolution. |
| 2 | |
| 3 | use craterust-analyzer cargo fluent-uri 0.4.1 crate/::imprust-analyzer cargo fluent-uri 0.4.1 imp/::{Metarust-analyzer cargo fluent-uri 0.4.1 imp/Meta#, Rirust-analyzer cargo fluent-uri 0.4.1 imp/Ri#, RiMaybeRefrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#, RmrRefrust-analyzer cargo fluent-uri 0.4.1 imp/RmrRef#}; |
| 4 | 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#; |
| 5 | use borrow_or_sharerust-analyzer cargo borrow-or-share 0.2.4 crate/::Bosrust-analyzer cargo borrow-or-share 0.2.4 Bos#; |
| 6 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, 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#}; |
| 7 | |
| 8 | /// An error occurred when resolving a URI/IRI reference. |
| 9 | #[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:)] |
| 10 | pub enum ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError# { |
| 11 | /// The base has a fragment. |
| 12 | BaseWithFragmentrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#BaseWithFragment#, |
| 13 | /// The base has no authority and its path is rootless, but the reference |
| 14 | /// is relative, is not empty and does not start with `'#'`. |
| 15 | InvalidReferenceAgainstOpaqueBaserust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#InvalidReferenceAgainstOpaqueBase#, |
| 16 | /// An underflow occurred in path resolution. |
| 17 | /// |
| 18 | /// Used only when [`Resolver::allow_path_underflow`] is set to `false`. |
| 19 | PathUnderflowrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#PathUnderflow#, |
| 20 | } |
| 21 | |
| 22 | 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 ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError# { |
| 23 | fn fmtrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[ResolveError][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# { |
| 24 | let msg = match self { |
| 25 | Selfrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[ResolveError][Display]::BaseWithFragmentrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#BaseWithFragment# => "base should not have fragment", |
| 26 | Selfrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[ResolveError][Display]::InvalidReferenceAgainstOpaqueBaserust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#InvalidReferenceAgainstOpaqueBase# => { |
| 27 | "when base has a rootless path and no authority, reference should either have scheme, be empty or start with '#'" |
| 28 | } |
| 29 | Selfrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[ResolveError][Display]::PathUnderflowrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#PathUnderflow# => "underflow occurred in path resolution", |
| 30 | }; |
| 31 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(msg) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[cfg(feature = "impl-error")] |
| 36 | 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 ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError# {} |
| 37 | |
| 38 | /// A configurable URI/IRI reference resolver against a fixed base. |
| 39 | /// |
| 40 | /// # Examples |
| 41 | /// |
| 42 | /// ``` |
| 43 | /// use fluent_uri::{resolve::Resolver, Uri, UriRef}; |
| 44 | /// |
| 45 | /// let base = Uri::parse("http://example.com/foo/bar")?; |
| 46 | /// let resolver = Resolver::with_base(base); |
| 47 | /// |
| 48 | /// assert_eq!(resolver.resolve(&UriRef::parse("baz")?).unwrap(), "http://example.com/foo/baz"); |
| 49 | /// assert_eq!(resolver.resolve(&UriRef::parse("../baz")?).unwrap(), "http://example.com/baz"); |
| 50 | /// assert_eq!(resolver.resolve(&UriRef::parse("?baz")?).unwrap(), "http://example.com/foo/bar?baz"); |
| 51 | /// # Ok::<_, fluent_uri::ParseError>(()) |
| 52 | /// ``` |
| 53 | #[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:)] |
| 54 | #[must_use] |
| 55 | pub struct Resolverrust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#<R> { |
| 56 | baserust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#base.: R, |
| 57 | allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#allow_path_underflow.: bool, |
| 58 | } |
| 59 | |
| 60 | impl<R: Rirust-analyzer cargo fluent-uri 0.4.1 imp/Ri#> Resolverrust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#<R> |
| 61 | where |
| 62 | R::Valrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#Val#: Bosrust-analyzer cargo borrow-or-share 0.2.4 Bos#<str>, |
| 63 | { |
| 64 | /// Creates a new `Resolver` with the given base and default configuration. |
| 65 | pub fn with_baserust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[`Resolver<R>`]with_base().(base: R) -> Selfrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[`Resolver<R>`] { |
| 66 | Selfrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[`Resolver<R>`] { |
| 67 | baserust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#base., |
| 68 | allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#allow_path_underflow.: true, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Sets whether to allow underflow in path resolution. |
| 73 | /// |
| 74 | /// This defaults to `true`. A value of `false` is a deviation from the |
| 75 | /// reference resolution algorithm defined in |
| 76 | /// [Section 5 of RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986/#section-5). |
| 77 | /// |
| 78 | /// # Examples |
| 79 | /// |
| 80 | /// ``` |
| 81 | /// use fluent_uri::{resolve::{Resolver, ResolveError}, Uri, UriRef}; |
| 82 | /// |
| 83 | /// let base = Uri::parse("http://example.com/foo/bar")?; |
| 84 | /// let resolver = Resolver::with_base(base).allow_path_underflow(false); |
| 85 | /// |
| 86 | /// assert_eq!(resolver.resolve(&UriRef::parse("../../baz")?).unwrap_err(), ResolveError::PathUnderflow); |
| 87 | /// assert_eq!(resolver.resolve(&UriRef::parse("../../../baz")?).unwrap_err(), ResolveError::PathUnderflow); |
| 88 | /// assert_eq!(resolver.resolve(&UriRef::parse("/../baz")?).unwrap_err(), ResolveError::PathUnderflow); |
| 89 | /// # Ok::<_, fluent_uri::ParseError>(()) |
| 90 | /// ``` |
| 91 | pub fn allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[`Resolver<R>`]allow_path_underflow().(mut self, value: bool) -> Selfrust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[`Resolver<R>`] { |
| 92 | self.allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#allow_path_underflow. = value; |
| 93 | self |
| 94 | } |
| 95 | |
| 96 | /// Resolves the given reference against the configured base. |
| 97 | /// |
| 98 | /// See [`resolve_against`] for the exact behavior of this method. |
| 99 | /// |
| 100 | /// # Errors |
| 101 | /// |
| 102 | /// Returns `Err` on the same conditions as [`resolve_against`] or if an underflow |
| 103 | /// occurred in path resolution when [`allow_path_underflow`] is set to `false`. |
| 104 | /// |
| 105 | /// [`resolve_against`]: crate::UriRef::resolve_against |
| 106 | /// [`allow_path_underflow`]: Self::allow_path_underflow |
| 107 | pub fn resolverust-analyzer cargo fluent-uri 0.4.1 resolve/impl#[`Resolver<R>`]resolve().<T: Bosrust-analyzer cargo borrow-or-share 0.2.4 Bos#<str>>( |
| 108 | &self, |
| 109 | reference: &R::Refrust-analyzer cargo fluent-uri 0.4.1 imp/Ri#Ref#<T>, |
| 110 | ) -> 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#>, ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#> { |
| 111 | resolverust-analyzer cargo fluent-uri 0.4.1 resolve/resolve().( |
| 112 | self.baserust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#base..make_refrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#make_ref().(), |
| 113 | reference.make_refrust-analyzer cargo fluent-uri 0.4.1 imp/RiMaybeRef#make_ref().(), |
| 114 | self.allow_path_underflowrust-analyzer cargo fluent-uri 0.4.1 resolve/Resolver#allow_path_underflow., |
| 115 | ) |
| 116 | .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().) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | pub(craterust-analyzer cargo fluent-uri 0.4.1 crate/) fn resolverust-analyzer cargo fluent-uri 0.4.1 resolve/resolve().( |
| 121 | base: RmrRefrust-analyzer cargo fluent-uri 0.4.1 imp/RmrRef#<'_, '_>, |
| 122 | /* reference */ r: RmrRefrust-analyzer cargo fluent-uri 0.4.1 imp/RmrRef#<'_, '_>, |
| 123 | allow_path_underflow: bool, |
| 124 | ) -> 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#), ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#> { |
| 125 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(base.has_schemerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]has_scheme().()); |
| 126 | |
| 127 | if base.has_fragmentrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]has_fragment().() { |
| 128 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#::BaseWithFragmentrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#BaseWithFragment#); |
| 129 | } |
| 130 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().base.has_authorityrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]has_authority().() |
| 131 | && base.pathrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]path().().is_rootlessrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]is_rootless().() |
| 132 | && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().r.has_schemerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]has_scheme().() |
| 133 | && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(r.as_strrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]as_str().().bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][Iterator]next().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'#')) |
| 134 | { |
| 135 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#::InvalidReferenceAgainstOpaqueBaserust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#InvalidReferenceAgainstOpaqueBase#); |
| 136 | } |
| 137 | |
| 138 | let (t_scheme, t_authority, t_path, t_query, t_fragment); |
| 139 | |
| 140 | let r_scheme = r.scheme_optrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]scheme_opt().(); |
| 141 | let r_authority = r.authorityrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]authority().(); |
| 142 | let r_path = r.pathrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]path().(); |
| 143 | let r_query = r.queryrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]query().(); |
| 144 | let r_fragment = r.fragmentrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]fragment().(); |
| 145 | |
| 146 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(r_scheme) = r_scheme { |
| 147 | t_scheme = r_scheme; |
| 148 | t_authority = r_authority; |
| 149 | t_path = (r_path.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 150 | t_query = r_query; |
| 151 | } else { |
| 152 | if r_authority.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 153 | t_authority = r_authority; |
| 154 | t_path = (r_path.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 155 | t_query = r_query; |
| 156 | } else { |
| 157 | if r_path.is_emptyrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]is_empty().() { |
| 158 | t_path = (base.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().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 159 | if r_query.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 160 | t_query = r_query; |
| 161 | } else { |
| 162 | t_query = base.queryrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]query().(); |
| 163 | } |
| 164 | } else { |
| 165 | if r_path.is_absoluterust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]is_absolute().() { |
| 166 | t_path = (r_path.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 167 | } else { |
| 168 | let base_path = base.pathrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]path().(); |
| 169 | let base_path = if base_path.is_emptyrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]is_empty().() { |
| 170 | "/" |
| 171 | } else { |
| 172 | base_path.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().() |
| 173 | }; |
| 174 | |
| 175 | // Make sure that swapping the order of resolution and normalization |
| 176 | // does not change the result. |
| 177 | let last_slash_i = base_path.rfindrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rfind().('/').unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 178 | let last_seg = &base_path[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().last_slash_i 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().1..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().; |
| 179 | let base_path_stripped = match classify_segmentrust-analyzer cargo fluent-uri 0.4.1 resolve/classify_segment().(last_seg) { |
| 180 | SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::DoubleDotrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#DoubleDot# => base_path, |
| 181 | _ => &base_path[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeToInclusive#last_slash_i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., |
| 182 | }; |
| 183 | |
| 184 | // Instead of merging the paths, remove dot segments incrementally. |
| 185 | t_path = (base_path_stripped, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(r_path.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().())); |
| 186 | } |
| 187 | t_query = r_query; |
| 188 | } |
| 189 | t_authority = base.authorityrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]authority().(); |
| 190 | } |
| 191 | t_scheme = base.schemerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`RmrRef<'v, 'm>`]scheme().(); |
| 192 | } |
| 193 | t_fragment = r_fragment; |
| 194 | |
| 195 | // Calculate the output length. |
| 196 | let mut len = t_scheme.as_strrust-analyzer cargo fluent-uri 0.4.1 component/impl#[Scheme]as_str().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() 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().1; |
| 197 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(authority) = t_authority { |
| 198 | len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().authority.as_strrust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]as_str().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() 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().2; |
| 199 | } |
| 200 | len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().t_path.0.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() 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().t_path.1.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(0, |s| s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()); |
| 201 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(query) = t_query { |
| 202 | len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().query.lenrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]len().() 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().1; |
| 203 | } |
| 204 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(fragment) = t_fragment { |
| 205 | len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().fragment.lenrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]len().() 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().1; |
| 206 | } |
| 207 | |
| 208 | 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().(len); |
| 209 | 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().(); |
| 210 | |
| 211 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(t_scheme.as_strrust-analyzer cargo fluent-uri 0.4.1 component/impl#[Scheme]as_str().()); |
| 212 | 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().()); |
| 213 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(':'); |
| 214 | |
| 215 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(authority) = t_authority { |
| 216 | let mut auth_meta = authority.metarust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]meta().(); |
| 217 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("//"); |
| 218 | |
| 219 | auth_meta.host_boundsrust-analyzer cargo fluent-uri 0.4.1 imp/AuthMeta#host_bounds..0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 220 | auth_meta.host_boundsrust-analyzer cargo fluent-uri 0.4.1 imp/AuthMeta#host_bounds..1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 221 | |
| 222 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(authority.as_strrust-analyzer cargo fluent-uri 0.4.1 component/impl#[`Authority<'a, UserinfoE, RegNameE>`]as_str().()); |
| 223 | 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); |
| 224 | } |
| 225 | |
| 226 | let path_start = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(); |
| 227 | meta.path_boundsrust-analyzer cargo fluent-uri 0.4.1 imp/Meta#path_bounds..0 = path_start; |
| 228 | |
| 229 | if t_path.0.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('/') { |
| 230 | let path = [t_path.0, t_path.1.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("")]; |
| 231 | let path = &path[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#t_path.1.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() as usize 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().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 232 | |
| 233 | let underflow_occurred = remove_dot_segmentsrust-analyzer cargo fluent-uri 0.4.1 resolve/remove_dot_segments().(&mut buf, path_start, path); |
| 234 | if underflow_occurred && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().allow_path_underflow { |
| 235 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ResolveErrorrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#::PathUnderflowrust-analyzer cargo fluent-uri 0.4.1 resolve/ResolveError#PathUnderflow#); |
| 236 | } |
| 237 | } else { |
| 238 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(t_path.0); |
| 239 | } |
| 240 | |
| 241 | // Close the loophole in the original algorithm. |
| 242 | if t_authority.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() && buf[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().path_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][`Index<I>`]index()..starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("//") { |
| 243 | buf.insert_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]insert_str().(path_start, "/."); |
| 244 | } |
| 245 | |
| 246 | 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().(); |
| 247 | |
| 248 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(query) = t_query { |
| 249 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('?'); |
| 250 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(query.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().()); |
| 251 | 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().()); |
| 252 | } |
| 253 | |
| 254 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(fragment) = t_fragment { |
| 255 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('#'); |
| 256 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(fragment.as_strrust-analyzer cargo fluent-uri 0.4.1 pct_enc/impl#[`EStr<E>`]as_str().()); |
| 257 | } |
| 258 | |
| 259 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() <= len); |
| 260 | |
| 261 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((buf, meta)) |
| 262 | } |
| 263 | |
| 264 | pub(craterust-analyzer cargo fluent-uri 0.4.1 crate/) fn remove_dot_segmentsrust-analyzer cargo fluent-uri 0.4.1 resolve/remove_dot_segments().(buf: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, start: usize, path: &[&str]) -> bool { |
| 265 | let mut underflow_occurred = false; |
| 266 | for seg in path.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().flat_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flat_map().(|s| s.split_inclusiverust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_inclusive().('/')) { |
| 267 | let seg_stripped = seg.strip_suffixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_suffix().('/').unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(seg); |
| 268 | match classify_segmentrust-analyzer cargo fluent-uri 0.4.1 resolve/classify_segment().(seg_stripped) { |
| 269 | SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::Dotrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#Dot# => {} |
| 270 | SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::DoubleDotrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#DoubleDot# => { |
| 271 | 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>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().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().1 { |
| 272 | buf.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]truncate().(buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#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 ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..rfindrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rfind().('/').unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() 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().1); |
| 273 | } else { |
| 274 | underflow_occurred = true; |
| 275 | } |
| 276 | } |
| 277 | SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::Normalrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#Normal# => buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(seg), |
| 278 | } |
| 279 | } |
| 280 | underflow_occurred |
| 281 | } |
| 282 | |
| 283 | enum SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind# { |
| 284 | Dotrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#Dot#, |
| 285 | DoubleDotrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#DoubleDot#, |
| 286 | Normalrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#Normal#, |
| 287 | } |
| 288 | |
| 289 | fn classify_segmentrust-analyzer cargo fluent-uri 0.4.1 resolve/classify_segment().(mut seg: &str) -> SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind# { |
| 290 | if seg.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 291 | return SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::Normalrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#Normal#; |
| 292 | } |
| 293 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(rem) = seg.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('.') { |
| 294 | seg = rem; |
| 295 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(rem) = seg.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().("%2E") { |
| 296 | seg = rem; |
| 297 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(rem) = seg.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().("%2e") { |
| 298 | seg = rem; |
| 299 | } |
| 300 | if seg.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 301 | SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::Dotrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#Dot# |
| 302 | } else if seg rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."." || seg rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."%2E" || seg rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."%2e" { |
| 303 | SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::DoubleDotrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#DoubleDot# |
| 304 | } else { |
| 305 | SegKindrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#::Normalrust-analyzer cargo fluent-uri 0.4.1 resolve/SegKind#Normal# |
| 306 | } |
| 307 | } |