use crate::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PercentEncodeSet(ByteSet);
impl PercentEncodeSet {
pub const fn new() -> Self {
Self(ByteSet::new().add_non_ascii())
}
pub const fn contains(self, b: u8) -> bool {
self.0.contains(b)
}
pub const fn add(self, b: u8) -> Self {
Self(self.0.add(b))
}
pub const fn add_many(self, bs: &[u8]) -> Self {
Self(self.0.add_many(bs))
}
pub const fn remove(self, b: u8) -> Self {
assert!(b.is_ascii());
Self(self.0.remove(b))
}
pub const fn remove_many(self, bs: &[u8]) -> Self {
assert!(bs.is_ascii());
Self(self.0.remove_many(bs))
}
pub const fn merge(self, other: Self) -> Self {
Self(self.0.merge(other.0))
}
pub const fn into_inner(self) -> ByteSet {
self.0
}
pub const fn new_unchecked(value: ByteSet) -> Self {
Self(value)
}
}
impl Default for PercentEncodeSet {
fn default() -> Self {
Self::new()
}
}
impl From<PercentEncodeSet> for ByteSet {
fn from(value: PercentEncodeSet) -> Self {
value.0
}
}
impl TryFrom<ByteSet> for PercentEncodeSet {
type Error = ByteSet;
fn try_from(value: ByteSet) -> Result<Self, Self::Error> {
if !(128..=255).all(|b| value.contains(b)) {
Err(value)?;
}
Ok(Self(value))
}
}
pub 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");
pub const COMPONENT: PercentEncodeSet = USERINFO.add_many(b"$&+,");
pub const USERINFO : PercentEncodeSet = PATH.add_many(b"/:;=@[\\]|");
pub const OPAQUE_HOST : PercentEncodeSet = C0;
pub const OPAQUE_PATH : PercentEncodeSet = C0;
pub const PATH : PercentEncodeSet = NON_SPECIAL_QUERY.add_many(b"?^`{}");
pub const PATH_SEGMENT : PercentEncodeSet = PATH.add_many(b"/%");
pub const QUERY_PART : PercentEncodeSet = COMPONENT.add_many(b"!'()~");
pub const NON_SPECIAL_QUERY : PercentEncodeSet = C0.add_many(b" \"#<>");
pub const NON_SPECIAL_QUERY_SEGMENT : PercentEncodeSet = NON_SPECIAL_QUERY.add(b'&');
pub const SPECIAL_QUERY : PercentEncodeSet = NON_SPECIAL_QUERY.add_many(b"'");
pub const SPECIAL_QUERY_SEGMENT : PercentEncodeSet = SPECIAL_QUERY.add(b'&');
pub const FRAGMENT : PercentEncodeSet = C0.add_many(b" \"<>`");
pub const FRAGMENT_QUERY_SEGMENT : PercentEncodeSet = FRAGMENT.add(b'&');