| 1 | use craterust-analyzer cargo schemars 1.2.2 crate/::_alloc_preluderust-analyzer cargo schemars 1.2.2 _alloc_prelude/::*; |
| 2 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 3 | 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/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write# as _; |
| 4 | |
| 5 | /// Encodes a string for insertion into a JSON Pointer in URI fragment representation. |
| 6 | #[must_use] |
| 7 | pub fn encode_ref_namerust-analyzer cargo schemars 1.2.2 encoding/encode_ref_name().(name: &str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str> { |
| 8 | fn needs_encodingrust-analyzer cargo schemars 1.2.2 encoding/needs_encoding().(byte: u8) -> bool { |
| 9 | match byte { |
| 10 | // `~` and `/` need encoding for JSON Pointer |
| 11 | // See https://datatracker.ietf.org/doc/html/rfc6901#section-3 |
| 12 | b'~' | b'/' => true, |
| 13 | // These chars (and `~`) are valid in URL fragment |
| 14 | // See https://datatracker.ietf.org/doc/html/rfc3986/#section-3.5 |
| 15 | b'!' | b'$' | b'&'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b';' | b'=' | b'?'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'Z' | b'_' | b'a'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'z' => false, |
| 16 | // Everything else needs percent-encoding |
| 17 | _ => true, |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | if name.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][Iterator]any().(needs_encodingrust-analyzer cargo schemars 1.2.2 encoding/needs_encoding().) { |
| 22 | let mut buf = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 23 | |
| 24 | for byte in name.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().() { |
| 25 | if byte rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'~' { |
| 26 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("~0"); |
| 27 | } else if byte rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'/' { |
| 28 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("~1"); |
| 29 | } else if needs_encodingrust-analyzer cargo schemars 1.2.2 encoding/needs_encoding().(byte) { |
| 30 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "%{byte:2X}").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 31 | } else { |
| 32 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(byte as char); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(buf) |
| 37 | } else { |
| 38 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(name) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// Percent-decodes the given string, returning `None` if it results in invalid UTF-8. |
| 43 | /// A `%` that is not followed by two hex digits is treated as a literal `%`. |
| 44 | #[must_use] |
| 45 | pub fn percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().(s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str>> { |
| 46 | if s.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().('%') { |
| 47 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::<u8>::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 48 | |
| 49 | let mut segments = s.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('%'); |
| 50 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(segments.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().().unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().().as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 51 | |
| 52 | for segment in segments { |
| 53 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(decoded_byte) = segment |
| 54 | .getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]get().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#2) |
| 55 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|p| u8::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]from_str_radix().(p, 16).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 56 | { |
| 57 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(decoded_byte); |
| 58 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&segment.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2..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().); |
| 59 | } else { |
| 60 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'%'); |
| 61 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(segment.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(buf).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#) |
| 66 | } else { |
| 67 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(s)) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[cfg(test)] |
| 72 | mod testsrust-analyzer cargo schemars 1.2.2 encoding/tests/ { |
| 73 | use superrust-analyzer cargo schemars 1.2.2 encoding/::*; |
| 74 | |
| 75 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 76 | fn test_encode_ref_namerust-analyzer cargo schemars 1.2.2 encoding/tests/test_encode_ref_name().() { |
| 77 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(encode_ref_namerust-analyzer cargo schemars 1.2.2 encoding/encode_ref_name().("Simple!"), "Simple!"); |
| 78 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 79 | encode_ref_namerust-analyzer cargo schemars 1.2.2 encoding/encode_ref_name().("Needs %-encoding 🚀"), |
| 80 | "Needs%20%25-encoding%20%F0%9F%9A%80" |
| 81 | ); |
| 82 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 83 | encode_ref_namerust-analyzer cargo schemars 1.2.2 encoding/encode_ref_name().("aA0-._!$&'()*+,;=:@?"), |
| 84 | "aA0-._!$&'()*+,;=:@?", |
| 85 | ); |
| 86 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(encode_ref_namerust-analyzer cargo schemars 1.2.2 encoding/encode_ref_name().("\"£%^\\~/"), "%22%C2%A3%25%5E%5C~0~1",); |
| 87 | } |
| 88 | |
| 89 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 90 | fn test_percent_decoderust-analyzer cargo schemars 1.2.2 encoding/tests/test_percent_decode().() { |
| 91 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("Simple!"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("Simple!".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 92 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 93 | percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("Needs %-encoding 🚀"), |
| 94 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("Needs %-encoding 🚀".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 95 | ); |
| 96 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 97 | percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("Needs%20%25-encoding%20%F0%9F%9A%80"), |
| 98 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("Needs %-encoding 🚀".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 99 | ); |
| 100 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 101 | percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("aA0-._!$&'()*+,;=:@?"), |
| 102 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("aA0-._!$&'()*+,;=:@?".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 103 | ); |
| 104 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("\"£%^\\~/"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("\"£%^\\~/".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 105 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 106 | percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("%22%C2%A3%25%5E%5C~0~1"), |
| 107 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("\"£%^\\~0~1".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 108 | ); |
| 109 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("%%%2020%%%"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("%% 20%%%".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 110 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("%f0%9F%9a%80"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("🚀".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 111 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().("%F0%9F%9A"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 112 | } |
| 113 | } |