Skip to main content

better_url/util/percent/
encode_set.rs

1//! [`PercentEncodeSet`].
2
3use crate::prelude::*;
4
5/// A [`ByteSet`] with all non-ASCII bytes always set.
6///
7/// Mainly for use in [`percent_encode`].
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct PercentEncodeSet(ByteSet);
10
11impl PercentEncodeSet {
12    /// Make a new [`Self`].
13    pub const fn new() -> Self {
14        Self(ByteSet::new().add_non_ascii())
15    }
16
17    /// If it contains `b`.
18    pub const fn contains(self, b: u8) -> bool {
19        self.0.contains(b)
20    }
21
22
23
24    /// Add `b` to the set.
25    pub const fn add(self, b: u8) -> Self {
26        Self(self.0.add(b))
27    }
28
29    /// [`Self::add`] each byte.
30    pub const fn add_many(self, bs: &[u8]) -> Self {
31        Self(self.0.add_many(bs))
32    }
33
34
35
36    /// Remove `b` from the set.
37    /// # Panics
38    /// If `b` is not ASCII, panics.
39    pub const fn remove(self, b: u8) -> Self {
40        assert!(b.is_ascii());
41
42        Self(self.0.remove(b))
43    }
44
45    /// [`Self::remove`] each byte.
46    /// # Panics
47    /// If `bs` is not ASCII, panics.
48    pub const fn remove_many(self, bs: &[u8]) -> Self {
49        assert!(bs.is_ascii());
50
51        Self(self.0.remove_many(bs))
52    }
53
54
55
56    /// Merge `self` and `other`.
57    pub const fn merge(self, other: Self) -> Self {
58        Self(self.0.merge(other.0))
59    }
60
61    /// Get the inner [`ByteSet`].
62    pub const fn into_inner(self) -> ByteSet {
63        self.0
64    }
65
66    /// Make a new [`Self`] from a [`ByteSet`] without validity checks.
67    /// # Safety
68    /// All bytes in the range `128..=255` must be in the [`ByteSet`].
69    pub const fn new_unchecked(value: ByteSet) -> Self {
70        Self(value)
71    }
72}
73
74impl Default for PercentEncodeSet {
75    fn default() -> Self {
76        Self::new()
77    }
78}
79
80impl From<PercentEncodeSet> for ByteSet {
81    fn from(value: PercentEncodeSet) -> Self {
82        value.0
83    }
84}
85
86impl TryFrom<ByteSet> for PercentEncodeSet {
87    type Error = ByteSet;
88
89    fn try_from(value: ByteSet) -> Result<Self, Self::Error> {
90        if !(128..=255).all(|b| value.contains(b)) {
91            Err(value)?;
92        }
93
94        Ok(Self(value))
95    }
96}
97
98/// [The C0 control percent-encode set](https://url.spec.whatwg.org/#c0-control-percent-encode-set).
99pub const C0: PercentEncodeSet = PercentEncodeSet::new().add_many(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F");
100
101/// [The component percent-encode set](https://url.spec.whatwg.org/#component-percent-encode-set).
102pub const COMPONENT: PercentEncodeSet = USERINFO.add_many(b"$&+,");
103
104/// [The userinfo percent-encode set](https://url.spec.whatwg.org/#userinfo-percent-encode-set).
105pub const USERINFO                          : PercentEncodeSet = PATH.add_many(b"/:;=@[\\]|");
106/// The opaque host percent-encode set. Equal to [`C0`].
107pub const OPAQUE_HOST                       : PercentEncodeSet = C0;
108
109/// [`C0`].
110pub const OPAQUE_PATH                       : PercentEncodeSet = C0;
111/// [The path percent-encode set](https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set).
112pub const PATH                              : PercentEncodeSet = NON_SPECIAL_QUERY.add_many(b"?^`{}");
113/// [`PATH`] + `/` and `%`.
114pub const PATH_SEGMENT                      : PercentEncodeSet = PATH.add_many(b"/%");
115
116/// [The application/x-www-form-urlencoded percent-encode set](https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set).
117pub const QUERY_PART                        : PercentEncodeSet = COMPONENT.add_many(b"!'()~");
118
119/// [The query percent-encode set](https://url.spec.whatwg.org/#query-percent-encode-set).
120pub const NON_SPECIAL_QUERY                 : PercentEncodeSet = C0.add_many(b" \"#<>");
121/// [`NON_SPECIAL_QUERY`] + `&`.
122pub const NON_SPECIAL_QUERY_SEGMENT         : PercentEncodeSet = NON_SPECIAL_QUERY.add(b'&');
123
124/// [The special query percent-encode set](https://url.spec.whatwg.org/#special-query-percent-encode-set).
125pub const SPECIAL_QUERY                     : PercentEncodeSet = NON_SPECIAL_QUERY.add_many(b"'");
126/// [`SPECIAL_QUERY`] + `&`.
127pub const SPECIAL_QUERY_SEGMENT             : PercentEncodeSet = SPECIAL_QUERY.add(b'&');
128
129/// [The fragment percent-encode set](https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set)
130pub const FRAGMENT                          : PercentEncodeSet = C0.add_many(b" \"<>`");
131/// [`FRAGMENT`] + `&`.
132pub const FRAGMENT_QUERY_SEGMENT            : PercentEncodeSet = FRAGMENT.add(b'&');