use std::ops::{BitOr, BitOrAssign};
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
#[non_exhaustive]
pub enum JsonLdProfile {
Expanded,
Compacted,
Context,
Flattened,
Frame,
Framed,
Streaming,
}
impl JsonLdProfile {
#[inline]
pub const fn iri(self) -> &'static str {
match self {
Self::Expanded => "http://www.w3.org/ns/json-ld#expanded",
Self::Compacted => "http://www.w3.org/ns/json-ld#compacted",
Self::Context => "http://www.w3.org/ns/json-ld#context",
Self::Flattened => "http://www.w3.org/ns/json-ld#flattened",
Self::Frame => "http://www.w3.org/ns/json-ld#frame",
Self::Framed => "http://www.w3.org/ns/json-ld#framed",
Self::Streaming => "http://www.w3.org/ns/json-ld#streaming",
}
}
#[inline]
pub fn from_iri(iri: &str) -> Option<Self> {
match iri {
"http://www.w3.org/ns/json-ld#expanded" => Some(Self::Expanded),
"http://www.w3.org/ns/json-ld#compacted" => Some(Self::Compacted),
"http://www.w3.org/ns/json-ld#context" => Some(Self::Context),
"http://www.w3.org/ns/json-ld#flattened" => Some(Self::Flattened),
"http://www.w3.org/ns/json-ld#frame" => Some(Self::Frame),
"http://www.w3.org/ns/json-ld#framed" => Some(Self::Framed),
"http://www.w3.org/ns/json-ld#streaming" => Some(Self::Streaming),
_ => None,
}
}
#[inline]
const fn to_bit(self) -> u64 {
match self {
JsonLdProfile::Expanded => 1,
JsonLdProfile::Compacted => 2,
JsonLdProfile::Context => 4,
JsonLdProfile::Flattened => 8,
JsonLdProfile::Frame => 16,
JsonLdProfile::Framed => 32,
JsonLdProfile::Streaming => 64,
}
}
}
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Default)]
pub struct JsonLdProfileSet {
value: u64,
}
impl JsonLdProfileSet {
#[inline]
pub const fn empty() -> Self {
Self { value: 0 }
}
#[inline]
pub const fn from_profile(profile: JsonLdProfile) -> Self {
Self {
value: profile.to_bit(),
}
}
#[inline]
pub const fn contains(self, profile: JsonLdProfile) -> bool {
self.value & profile.to_bit() != 0
}
}
impl From<JsonLdProfile> for JsonLdProfileSet {
#[inline]
fn from(profile: JsonLdProfile) -> Self {
Self {
value: profile.to_bit(),
}
}
}
impl IntoIterator for JsonLdProfileSet {
type Item = JsonLdProfile;
type IntoIter = JsonLdProfileBagIter;
#[inline]
fn into_iter(self) -> JsonLdProfileBagIter {
JsonLdProfileBagIter {
set: self,
possible_values: [
JsonLdProfile::Expanded,
JsonLdProfile::Compacted,
JsonLdProfile::Context,
JsonLdProfile::Flattened,
JsonLdProfile::Frame,
JsonLdProfile::Framed,
JsonLdProfile::Streaming,
]
.into_iter(),
}
}
}
pub struct JsonLdProfileBagIter {
set: JsonLdProfileSet,
possible_values: std::array::IntoIter<JsonLdProfile, 7>,
}
impl Iterator for JsonLdProfileBagIter {
type Item = JsonLdProfile;
#[inline]
fn next(&mut self) -> Option<JsonLdProfile> {
loop {
let possible_value = self.possible_values.next()?;
if self.set.contains(possible_value) {
return Some(possible_value);
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self
.set
.value
.count_ones()
.try_into()
.expect("count_ones fits in usize");
(size, Some(size))
}
}
impl BitOr for JsonLdProfileSet {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self {
value: self.value | rhs.value,
}
}
}
impl BitOr<JsonLdProfile> for JsonLdProfileSet {
type Output = JsonLdProfileSet;
#[inline]
fn bitor(self, rhs: JsonLdProfile) -> JsonLdProfileSet {
self | JsonLdProfileSet::from(rhs)
}
}
impl BitOr<JsonLdProfileSet> for JsonLdProfile {
type Output = JsonLdProfileSet;
#[inline]
fn bitor(self, rhs: JsonLdProfileSet) -> JsonLdProfileSet {
JsonLdProfileSet::from(self) | rhs
}
}
impl BitOr for JsonLdProfile {
type Output = JsonLdProfileSet;
#[inline]
fn bitor(self, rhs: Self) -> JsonLdProfileSet {
JsonLdProfileSet::from(self) | JsonLdProfileSet::from(rhs)
}
}
impl BitOrAssign for JsonLdProfileSet {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.value |= rhs.value;
}
}
impl BitOrAssign<JsonLdProfile> for JsonLdProfileSet {
#[inline]
fn bitor_assign(&mut self, rhs: JsonLdProfile) {
*self |= JsonLdProfileSet::from(rhs);
}
}
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Default)]
pub enum JsonLdProcessingMode {
#[default]
JsonLd1_0,
JsonLd1_1, }
impl JsonLdProcessingMode {
#[inline]
pub const fn as_str(self) -> &'static str {
match self {
JsonLdProcessingMode::JsonLd1_0 => "json-ld-1.0",
JsonLdProcessingMode::JsonLd1_1 => "json-ld-1.1",
}
}
#[inline]
pub fn from_id(id: &str) -> Option<Self> {
match id {
"json-ld-1.0" => Some(JsonLdProcessingMode::JsonLd1_0),
"json-ld-1.1" => Some(JsonLdProcessingMode::JsonLd1_1),
_ => None,
}
}
}