better_url/util/percent/
encode_set.rs1use crate::prelude::*;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct PercentEncodeSet(ByteSet);
10
11impl PercentEncodeSet {
12 pub const fn new() -> Self {
14 Self(ByteSet::new().add_non_ascii())
15 }
16
17 pub const fn contains(self, b: u8) -> bool {
19 self.0.contains(b)
20 }
21
22
23
24 pub const fn add(self, b: u8) -> Self {
26 Self(self.0.add(b))
27 }
28
29 pub const fn add_many(self, bs: &[u8]) -> Self {
31 Self(self.0.add_many(bs))
32 }
33
34
35
36 pub const fn remove(self, b: u8) -> Self {
40 assert!(b.is_ascii());
41
42 Self(self.0.remove(b))
43 }
44
45 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 pub const fn merge(self, other: Self) -> Self {
58 Self(self.0.merge(other.0))
59 }
60
61 pub const fn into_inner(self) -> ByteSet {
63 self.0
64 }
65
66 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
98pub 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
101pub const COMPONENT: PercentEncodeSet = USERINFO.add_many(b"$&+,");
103
104pub const USERINFO : PercentEncodeSet = PATH.add_many(b"/:;=@[\\]|");
106pub const OPAQUE_HOST : PercentEncodeSet = C0;
108
109pub const OPAQUE_PATH : PercentEncodeSet = C0;
111pub const PATH : PercentEncodeSet = NON_SPECIAL_QUERY.add_many(b"?^`{}");
113pub const PATH_SEGMENT : PercentEncodeSet = PATH.add_many(b"/%");
115
116pub const QUERY_PART : PercentEncodeSet = COMPONENT.add_many(b"!'()~");
118
119pub const NON_SPECIAL_QUERY : PercentEncodeSet = C0.add_many(b" \"#<>");
121pub const NON_SPECIAL_QUERY_SEGMENT : PercentEncodeSet = NON_SPECIAL_QUERY.add(b'&');
123
124pub const SPECIAL_QUERY : PercentEncodeSet = NON_SPECIAL_QUERY.add_many(b"'");
126pub const SPECIAL_QUERY_SEGMENT : PercentEncodeSet = SPECIAL_QUERY.add(b'&');
128
129pub const FRAGMENT : PercentEncodeSet = C0.add_many(b" \"<>`");
131pub const FRAGMENT_QUERY_SEGMENT : PercentEncodeSet = FRAGMENT.add(b'&');