Debian Codegraph @

src/uri.rs

raw symbols SCIP
1 //! URI handling utilities for JSON Schema references.
2 use fluent_urirust-analyzer cargo fluent-uri 0.4.1 crate/::{
3 pct_encrust-analyzer cargo fluent-uri 0.4.1 pct_enc/::{encoderrust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/::Fragmentrust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/Fragment#, EStrrust-analyzer cargo fluent-uri 0.4.1 pct_enc/EStr#, Encoderrust-analyzer cargo fluent-uri 0.4.1 pct_enc/Encoder#},
4 Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#, UriRefrust-analyzer cargo fluent-uri 0.4.1 imp/UriRef#,
5 };
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::LazyLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/lazy_lock/LazyLock#;
7
8 use craterust-analyzer cargo referencing 0.37.4 crate/::Errorrust-analyzer cargo referencing 0.37.4 error/Error#;
9 pub use fluent_urirust-analyzer cargo fluent-uri 0.4.1 crate/::pct_encrust-analyzer cargo fluent-uri 0.4.1 pct_enc/::encoderrust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/::Pathrust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/Path#;
10
11 /// Resolves the URI reference against the given base URI and returns the target URI.
12 ///
13 /// # Errors
14 ///
15 /// Returns an error if base has not schema or there is a fragment.
16 pub fn resolve_againstrust-analyzer cargo referencing 0.37.4 uri/resolve_against().(base: &Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#<&str>, uri: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, Errorrust-analyzer cargo referencing 0.37.4 error/Error#> {
17 if uri.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('#') && base.as_strrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`Uri<T>`]as_str().().ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(uri) {
18 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(base.to_ownedrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`Uri<&str>`]to_owned().());
19 }
20 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(UriRefrust-analyzer cargo fluent-uri 0.4.1 imp/UriRef#::parserust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]parse().(uri)
21 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|error| Errorrust-analyzer cargo referencing 0.37.4 error/Error#::uri_reference_parsing_errorrust-analyzer cargo referencing 0.37.4 error/impl#[Error]uri_reference_parsing_error().(uri, error))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
22 .resolve_againstrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]resolve_against().(base)
23 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|error| Errorrust-analyzer cargo referencing 0.37.4 error/Error#::uri_resolving_errorrust-analyzer cargo referencing 0.37.4 error/impl#[Error]uri_resolving_error().(uri, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().base, error))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
24 .normalizerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`Uri<T>`]normalize().())
25 }
26
27 /// Parses a URI reference from a string into a [`crate::Uri`].
28 ///
29 /// # Errors
30 ///
31 /// Returns an error if the input string does not conform to URI-reference from RFC 3986.
32 pub fn from_strrust-analyzer cargo referencing 0.37.4 uri/from_str().(uri: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, Errorrust-analyzer cargo referencing 0.37.4 error/Error#> {
33 let uriref = UriRefrust-analyzer cargo fluent-uri 0.4.1 imp/UriRef#::parserust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]parse().(uri)
34 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|error| Errorrust-analyzer cargo referencing 0.37.4 error/Error#::uri_reference_parsing_errorrust-analyzer cargo referencing 0.37.4 error/impl#[Error]uri_reference_parsing_error().(uri, error))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
35 .normalizerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]normalize().();
36 if uriref.has_schemerust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]has_scheme().() {
37 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#::try_fromrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`Uri<&'a str>`][`TryFrom<&'a str>`]try_from().(uriref.as_strrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]as_str().())
38 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|error| Errorrust-analyzer cargo referencing 0.37.4 error/Error#::uri_parsing_errorrust-analyzer cargo referencing 0.37.4 error/impl#[Error]uri_parsing_error().(uriref.as_strrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]as_str().(), error))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
39 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
40 } else {
41 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(uriref
42 .resolve_againstrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`UriRef<T>`]resolve_against().(&DEFAULT_ROOT_URIrust-analyzer cargo referencing 0.37.4 uri/DEFAULT_ROOT_URI..borrowrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`Uri<String>`]borrow().())
43 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|error| Errorrust-analyzer cargo referencing 0.37.4 error/Error#::uri_resolving_errorrust-analyzer cargo referencing 0.37.4 error/impl#[Error]uri_resolving_error().(uri, DEFAULT_ROOT_URIrust-analyzer cargo referencing 0.37.4 uri/DEFAULT_ROOT_URI..borrowrust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`Uri<String>`]borrow().(), error))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().)
44 }
45 }
46
47 pub(craterust-analyzer cargo referencing 0.37.4 crate/) static DEFAULT_ROOT_URIrust-analyzer cargo referencing 0.37.4 uri/DEFAULT_ROOT_URI.: LazyLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/lazy_lock/LazyLock#<Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> =
48 LazyLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/lazy_lock/LazyLock#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/lazy_lock/impl#[`LazyLock<T, F>`]new().(|| Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#::parserust-analyzer cargo fluent-uri 0.4.1 imp/impl#[`Uri<T>`]parse().("json-schema:///".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid URI"));
49
50 pub type EncodedStringrust-analyzer cargo referencing 0.37.4 uri/EncodedString# = EStrrust-analyzer cargo fluent-uri 0.4.1 pct_enc/EStr#<Fragmentrust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/Fragment#>;
51
52 // Adapted from `https://github.com/yescallop/fluent-uri-rs/blob/main/src/encoding/table.rs#L153`
53 pub fn encode_torust-analyzer cargo referencing 0.37.4 uri/encode_to().(input: &str, buffer: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
54 const HEX_TABLErust-analyzer cargo referencing 0.37.4 uri/HEX_TABLE.: [u8; 512] = {
55 const HEX_DIGITSrust-analyzer cargo referencing 0.37.4 uri/HEX_DIGITS.: &[u8; 16] = b"0123456789ABCDEF";
56
57 let mut i = 0;
58 let mut table = [0; 512];
59 while i 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().256 {
60 table[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = HEX_DIGITSrust-analyzer cargo referencing 0.37.4 uri/HEX_DIGITS.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr().4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
61 table[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2 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]`][`IndexMut<I>`]index_mut(). = HEX_DIGITSrust-analyzer cargo referencing 0.37.4 uri/HEX_DIGITS.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().0b1111]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
62 i 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().1;
63 }
64 table
65 };
66
67 for ch in input.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() {
68 if Pathrust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/Path#::TABLErust-analyzer cargo fluent-uri 0.4.1 pct_enc/encoder/impl#[Path][Encoder]TABLE..allowsrust-analyzer cargo fluent-uri 0.4.1 pct_enc/table/impl#[Table]allows().(ch) {
69 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch);
70 } else {
71 for x in ch.encode_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]encode_utf8().(&mut [0; 4]).bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().() {
72 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('%');
73 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(HEX_TABLErust-analyzer cargo referencing 0.37.4 uri/HEX_TABLE.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().x as usize rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). as char);
74 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(HEX_TABLErust-analyzer cargo referencing 0.37.4 uri/HEX_TABLE.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().x as usize rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2 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(). as char);
75 }
76 }
77 }
78 }