use std::fmt;
use std::sync::Arc;
#[cfg(feature = "dynamic")]
#[derive(Debug, Clone, PartialEq)]
pub enum Element {
Float(f64),
Int(i64),
Text(Arc<str>),
Bool(bool),
None,
}
#[cfg(feature = "dynamic")]
impl Element {
pub fn text(s: impl AsRef<str>) -> Self {
Element::Text(Arc::from(s.as_ref()))
}
pub fn is_none(&self) -> bool {
matches!(self, Element::None)
}
pub fn is_numeric(&self) -> bool {
matches!(self, Element::Float(_) | Element::Int(_))
}
pub fn try_as_f64(&self) -> Option<f64> {
match self {
Element::Float(v) => Some(*v),
Element::Int(v) => Some(*v as f64),
_ => None,
}
}
pub fn as_text(&self) -> Option<&str> {
match self {
Element::Text(s) => Some(s),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Element::Bool(b) => Some(*b),
_ => None,
}
}
}
#[cfg(feature = "dynamic")]
impl fmt::Display for Element {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Element::Float(v) => write!(f, "{v}"),
Element::Int(v) => write!(f, "{v}"),
Element::Text(s) => write!(f, "{s}"),
Element::Bool(b) => write!(f, "{b}"),
Element::None => f.write_str("None"),
}
}
}
#[cfg(feature = "dynamic")]
impl From<f64> for Element {
fn from(v: f64) -> Self {
Element::Float(v)
}
}
#[cfg(feature = "dynamic")]
impl From<i64> for Element {
fn from(v: i64) -> Self {
Element::Int(v)
}
}
#[cfg(feature = "dynamic")]
impl From<i32> for Element {
fn from(v: i32) -> Self {
Element::Int(v as i64)
}
}
#[cfg(feature = "dynamic")]
impl From<bool> for Element {
fn from(v: bool) -> Self {
Element::Bool(v)
}
}
#[cfg(feature = "dynamic")]
impl From<String> for Element {
fn from(s: String) -> Self {
Element::Text(Arc::from(s.as_str()))
}
}
#[cfg(feature = "dynamic")]
impl From<&str> for Element {
fn from(s: &str) -> Self {
Element::Text(Arc::from(s))
}
}
#[cfg(feature = "dynamic")]
impl From<Arc<str>> for Element {
fn from(s: Arc<str>) -> Self {
Element::Text(s)
}
}