use core::fmt;
use core::ops::BitOr;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Allow(u16);
impl Allow {
pub const NOTHING: Allow = Allow(0);
pub const STR: Allow = Allow(1 << 0);
pub const NUM: Allow = Allow(1 << 1);
pub const ARR: Allow = Allow(1 << 2);
pub const OBJ: Allow = Allow(1 << 3);
pub const KEY: Allow = Allow(1 << 4);
pub const BOOL: Allow = Allow(1 << 5);
pub const NULL: Allow = Allow(1 << 6);
pub const ATOM: Allow = Allow(Allow::BOOL.0 | Allow::NULL.0);
pub const COLLECTION: Allow = Allow(Allow::ARR.0 | Allow::OBJ.0);
pub const ALL: Allow = Allow(0b0111_1111);
pub const NONE: Allow = Allow::NOTHING;
#[must_use]
pub const fn contains(self, other: Allow) -> bool {
(self.0 & other.0) == other.0
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[must_use]
pub const fn bits(self) -> u16 {
self.0
}
#[must_use]
pub const fn union(self, other: Allow) -> Allow {
Allow(self.0 | other.0)
}
#[must_use]
pub const fn without(self, other: Allow) -> Allow {
Allow(self.0 & !other.0)
}
}
impl BitOr for Allow {
type Output = Allow;
fn bitor(self, rhs: Allow) -> Allow {
self.union(rhs)
}
}
impl fmt::Debug for Allow {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Allow(0b{:07b})", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Repairs(u32);
impl Repairs {
pub const NONE: Repairs = Repairs(0);
pub const FENCES: Repairs = Repairs(1 << 0);
pub const COMMENTS: Repairs = Repairs(1 << 1);
pub const UNQUOTED: Repairs = Repairs(1 << 2);
pub const KEYWORDS: Repairs = Repairs(1 << 3);
pub const NUMBERS: Repairs = Repairs(1 << 4);
pub const CONCATENATION: Repairs = Repairs(1 << 5);
pub const CALLS: Repairs = Repairs(1 << 6);
pub const ENTITIES: Repairs = Repairs(1 << 7);
pub const QUOTES: Repairs = Repairs(1 << 8);
pub const WHITESPACE: Repairs = Repairs(1 << 9);
pub const TRUNCATION: Repairs = Repairs(1 << 10);
pub const NDJSON: Repairs = Repairs(1 << 11);
pub const ALL: Repairs = Repairs(0b1111_1111_1111);
#[must_use]
pub const fn contains(self, other: Repairs) -> bool {
(self.0 & other.0) == other.0
}
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn union(self, other: Repairs) -> Repairs {
Repairs(self.0 | other.0)
}
#[must_use]
pub const fn without(self, other: Repairs) -> Repairs {
Repairs(self.0 & !other.0)
}
}
impl BitOr for Repairs {
type Output = Repairs;
fn bitor(self, rhs: Repairs) -> Repairs {
self.union(rhs)
}
}
impl fmt::Debug for Repairs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Repairs(0b{:011b})", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Options {
pub allow: Allow,
pub repairs: Repairs,
}
impl Options {
pub const ALL: Options = Options {
allow: Allow::ALL,
repairs: Repairs::ALL,
};
#[must_use]
pub const fn all() -> Self {
Self::ALL
}
#[must_use]
pub const fn strict() -> Self {
Self {
allow: Allow::NOTHING,
repairs: Repairs::NONE,
}
}
#[must_use]
pub const fn partial(allow: Allow) -> Self {
Self {
allow,
repairs: Repairs::ALL,
}
}
#[must_use]
pub const fn with_repairs(mut self, repairs: Repairs) -> Self {
self.repairs = repairs;
self
}
#[must_use]
pub const fn with_allow(mut self, allow: Allow) -> Self {
self.allow = allow;
self
}
#[must_use]
pub const fn repairs(self, flag: Repairs) -> bool {
self.repairs.contains(flag)
}
#[must_use]
pub const fn allows(self, flag: Allow) -> bool {
self.allow.contains(flag)
}
}
impl Default for Options {
fn default() -> Self {
Self::ALL
}
}