use alloc::{collections::BTreeMap, string::String, sync::Arc, vec::Vec};
#[derive(Clone, Debug)]
pub enum EntryMetaValue {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(Arc<str>),
Bytes(Arc<[u8]>),
Array(Arc<[Self]>),
Object(Arc<BTreeMap<Arc<str>, Self>>),
}
impl PartialEq for EntryMetaValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Null, Self::Null) => true,
(Self::Bool(a), Self::Bool(b)) => a == b,
(Self::Int(a), Self::Int(b)) => a == b,
(Self::Float(a), Self::Float(b)) => a.to_bits() == b.to_bits(),
(Self::String(a), Self::String(b)) => a == b,
(Self::Bytes(a), Self::Bytes(b)) => a == b,
(Self::Array(a), Self::Array(b)) => a == b,
(Self::Object(a), Self::Object(b)) => a == b,
_ => false,
}
}
}
impl Eq for EntryMetaValue {}
impl EntryMetaValue {
#[must_use]
pub fn string(s: &str) -> Self {
Self::String(Arc::from(s))
}
#[must_use]
pub fn bytes(b: &[u8]) -> Self {
Self::Bytes(Arc::from(b))
}
#[must_use]
pub fn array(items: Vec<Self>) -> Self {
Self::Array(Arc::from(items.into_boxed_slice()))
}
#[must_use]
pub fn object(map: BTreeMap<Arc<str>, Self>) -> Self {
Self::Object(Arc::new(map))
}
#[must_use]
pub const fn is_null(&self) -> bool {
matches!(self, Self::Null)
}
#[must_use]
pub const fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
}
#[must_use]
pub const fn as_int(&self) -> Option<i64> {
match self {
Self::Int(i) => Some(*i),
_ => None,
}
}
#[must_use]
pub const fn as_float(&self) -> Option<f64> {
match self {
Self::Float(f) => Some(*f),
_ => None,
}
}
#[must_use]
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s.as_ref()),
_ => None,
}
}
#[must_use]
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Self::Bytes(b) => Some(b.as_ref()),
_ => None,
}
}
#[must_use]
pub fn as_array(&self) -> Option<&[Self]> {
match self {
Self::Array(a) => Some(a.as_ref()),
_ => None,
}
}
#[must_use]
pub fn as_object(&self) -> Option<&BTreeMap<Arc<str>, Self>> {
match self {
Self::Object(o) => Some(o.as_ref()),
_ => None,
}
}
}
impl From<bool> for EntryMetaValue {
fn from(b: bool) -> Self {
Self::Bool(b)
}
}
impl From<i64> for EntryMetaValue {
fn from(i: i64) -> Self {
Self::Int(i)
}
}
impl From<i32> for EntryMetaValue {
fn from(i: i32) -> Self {
Self::Int(i64::from(i))
}
}
impl From<f64> for EntryMetaValue {
fn from(f: f64) -> Self {
Self::Float(f)
}
}
impl From<f32> for EntryMetaValue {
fn from(f: f32) -> Self {
Self::Float(f64::from(f))
}
}
impl From<String> for EntryMetaValue {
fn from(s: String) -> Self {
Self::String(Arc::from(s.as_str()))
}
}
impl From<&str> for EntryMetaValue {
fn from(s: &str) -> Self {
Self::String(Arc::from(s))
}
}