url_cleaner_engine/glue/
percent_encoding.rs

1//! Named common [`percent_encoding::AsciiSet`]s.
2
3use serde::{Serialize, Deserialize};
4use percent_encoding::AsciiSet;
5
6#[expect(unused_imports, reason = "Used in docs.")]
7use crate::types::*;
8use crate::util::*;
9
10/// As defined in [the URL spec](https://url.spec.whatwg.org/#percent-encoded-bytes).
11pub const FRAGMENT_PERCENT_ENCODE_SET             : AsciiSet = percent_encoding::CONTROLS  .add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
12/// As defined in [the URL spec](https://url.spec.whatwg.org/#percent-encoded-bytes).
13pub const QUERY_PERCENT_ENCODE_SET                : AsciiSet = percent_encoding::CONTROLS  .add(b' ').add(b'"').add(b'#').add(b'<').add(b'>');
14/// As defined in [the URL spec](https://url.spec.whatwg.org/#percent-encoded-bytes).
15pub const SPECIAL_QUERY_PERCENT_ENCODE_SET        : AsciiSet = QUERY_PERCENT_ENCODE_SET    .add(b'\'');
16/// As defined in [the URL spec](https://url.spec.whatwg.org/#percent-encoded-bytes).
17pub const PATH_PERCENT_ENCODE_SET                 : AsciiSet = QUERY_PERCENT_ENCODE_SET    .add(b'?').add(b'^').add(b'`').add(b'{').add(b'}');
18/// As defined in [the URL spec](https://url.spec.whatwg.org/#percent-encoded-bytes).
19pub const USERINFO_PERCENT_ENCODE_SET             : AsciiSet = PATH_PERCENT_ENCODE_SET     .add(b'/').add(b':').add(b';').add(b'=').add(b'@').add(b'[').add(b'\\').add(b']').add(b'|');
20/// As defined in [the URL spec](https://url.spec.whatwg.org/#percent-encoded-bytes).
21pub const COMPONENT_PERCENT_ENCODE_SET            : AsciiSet = USERINFO_PERCENT_ENCODE_SET .add(b'$').add(b'%').add(b'&').add(b'+').add(b',');
22/// As defined in [the URL spec](https://url.spec.whatwg.org/#percent-encoded-bytes).
23pub const X_WWW_FORM_URLENCODED_PERCENT_ENCODE_SET: AsciiSet = COMPONENT_PERCENT_ENCODE_SET.add(b'!').add(b'\'').add(b'(').add(b')');
24
25/// The [`AsciiSet`] that emulates [`encodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent).
26pub const JS_ENCODE_URI_COMPONENT_ASCII_SET: AsciiSet = percent_encoding::NON_ALPHANUMERIC
27    .remove(b'-' ).remove(b'_').remove(b'.').remove(b'!' ).remove(b'~').remove(b'*').remove(b'\'').remove(b'(').remove(b')');
28
29/// The [`AsciiSet`] that emulates [`encodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI).
30pub const JS_ENCODE_URI_ASCII_SET: AsciiSet = JS_ENCODE_URI_COMPONENT_ASCII_SET
31    .remove(b';').remove(b'/').remove(b'?').remove(b':').remove(b'@').remove(b'&')
32    .remove(b'=').remove(b'+').remove(b'$').remove(b',').remove(b'#');
33
34/// An enum of named common [`AsciiSet`]s for use in [`StringModification::PercentEncode`].
35///
36/// Defaults to [`Self::XWWWFormUrlEncoded`].
37///
38/// Unfortunately, custom sets aren't possible because [`percent_encoding::utf8_percent_encode`] requires a `'static` [`AsciiSet`].
39#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Suitability)]
40pub enum PercentEncodeAlphabet {
41    /// Use [`JS_ENCODE_URI_COMPONENT_ASCII_SET`].
42    JsEncodeUriComponent,
43    /// Use [`JS_ENCODE_URI_ASCII_SET`].
44    JsEncodeUri,
45    /// Use [`percent_encoding::NON_ALPHANUMERIC`].
46    NonAlphanumeric,
47    /// Use [`FRAGMENT_PERCENT_ENCODE_SET`].
48    Fragment,
49    /// Use [`QUERY_PERCENT_ENCODE_SET`].
50    Query,
51    /// Use [`SPECIAL_QUERY_PERCENT_ENCODE_SET`].
52    Special,
53    /// Use [`PATH_PERCENT_ENCODE_SET`].
54    Path,
55    /// Use [`USERINFO_PERCENT_ENCODE_SET`].
56    Userinfo,
57    /// Use [`COMPONENT_PERCENT_ENCODE_SET`].
58    Component,
59    /// Use [`X_WWW_FORM_URLENCODED_PERCENT_ENCODE_SET`].
60    #[default]
61    XWWWFormUrlEncoded,
62    /// Use [`percent_encoding::CONTROLS`].
63    Controls
64}
65
66impl PercentEncodeAlphabet {
67    /// Get the corresponding [`AsciiSet`].
68    pub fn get(&self) -> &'static AsciiSet {
69        match self {
70            Self::JsEncodeUriComponent => &JS_ENCODE_URI_COMPONENT_ASCII_SET,
71            Self::JsEncodeUri          => &JS_ENCODE_URI_ASCII_SET,
72            Self::NonAlphanumeric      => percent_encoding::NON_ALPHANUMERIC,
73            Self::Fragment             => &FRAGMENT_PERCENT_ENCODE_SET,
74            Self::Query                => &QUERY_PERCENT_ENCODE_SET,
75            Self::Special              => &SPECIAL_QUERY_PERCENT_ENCODE_SET,
76            Self::Path                 => &PATH_PERCENT_ENCODE_SET,
77            Self::Userinfo             => &USERINFO_PERCENT_ENCODE_SET,
78            Self::Component            => &COMPONENT_PERCENT_ENCODE_SET,
79            Self::XWWWFormUrlEncoded   => &X_WWW_FORM_URLENCODED_PERCENT_ENCODE_SET,
80            Self::Controls             => percent_encoding::CONTROLS
81        }
82    }
83}