use crate::encode::ProbeError;
use std::string::String;
use std::vec::Vec;
use zerodds_float128::Float128;
pub trait Source: Sized {
fn kind(&self) -> &'static str;
fn is_null(&self) -> bool;
fn is_object(&self) -> bool;
fn as_bool(&self) -> Option<bool>;
fn as_text(&self) -> Option<&str>;
fn as_float(&self) -> Option<f64>;
fn as_items(&self) -> Option<&[Self]>;
fn field(&self, name: &str) -> Option<&Self>;
fn entries(&self) -> Vec<(&str, &Self)>;
fn as_integer(&self, path: &str) -> Result<i128, ProbeError>;
fn as_float128(&self, path: &str) -> Result<Float128, ProbeError>;
fn render(&self) -> String;
}
pub struct Fields<'a, V>(&'a V);
impl<V> Clone for Fields<'_, V> {
fn clone(&self) -> Self {
*self
}
}
impl<V> Copy for Fields<'_, V> {}
impl<'a, V: Source> Fields<'a, V> {
pub(crate) const fn of(value: &'a V) -> Self {
Self(value)
}
#[must_use]
pub const fn value(self) -> &'a V {
self.0
}
#[must_use]
pub fn get(self, name: &str) -> Option<&'a V> {
self.0.field(name)
}
#[must_use]
pub fn names(self) -> Vec<&'a str> {
self.0.entries().into_iter().map(|(name, _)| name).collect()
}
}
impl<'a, V: Source> IntoIterator for Fields<'a, V> {
type Item = (&'a str, &'a V);
type IntoIter = std::vec::IntoIter<(&'a str, &'a V)>;
fn into_iter(self) -> Self::IntoIter {
self.0.entries().into_iter()
}
}