use std::borrow::Cow;
use crate::number::{Number, NumberReading};
#[cfg(feature = "parse")]
mod fields;
mod json;
mod line;
#[cfg(feature = "parse")]
mod nested;
#[cfg(feature = "xml")]
mod xml;
#[cfg(feature = "parse")]
pub use fields::FieldsAt;
#[cfg(all(feature = "parse", feature = "scrub"))]
pub(crate) use fields::{Spot, spot, write_strings};
pub use json::Json;
#[cfg(feature = "expr")]
pub(crate) use json::ReadPlan;
#[cfg(feature = "scrub")]
pub(crate) use json::{WalkBufs, Walked};
pub use line::Line;
#[cfg(all(feature = "parse", feature = "scrub"))]
pub(crate) use nested::FirstRead;
#[cfg(feature = "parse")]
pub use nested::{
LayerCache, LayerError, LayerPlan, Layered, LayeredAt, MAX_LAYERS, Source, SourceAt,
};
#[cfg(feature = "xml")]
pub use xml::{Xml, XmlAt};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Miss {
MissingRoot,
MissingField,
WrongParentType,
Malformed,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Typed<'a> {
Str(Cow<'a, str>),
Int(i64),
Uint(u64),
Double(f64),
Bool(bool),
Null,
List,
Map,
}
pub trait Record<'a>: Copy {
type At: Copy;
fn find<K: AsRef<str>>(self, path: &[K]) -> Result<Self::At, Miss>;
fn find_below<K: AsRef<str>>(self, at: Self::At, path: &[K]) -> Result<Self::At, Miss>;
fn typed(self, at: Self::At) -> Result<Typed<'a>, Miss>;
fn typed_exact(self, at: Self::At) -> Result<Typed<'a>, Miss>;
fn text(self, at: Self::At) -> Result<Cow<'a, str>, Miss>;
#[doc(hidden)]
#[allow(
clippy::type_complexity,
reason = "a byte slice and a converter, named once here"
)]
fn raw_json(self) -> Option<(&'a [u8], fn(&'a str) -> Self::At)> {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Kind {
Str,
Number,
Bool,
Null,
List,
Map,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Node<'a, At> {
pub depth: usize,
pub key: Option<Cow<'a, str>>,
pub kind: Kind,
pub at: At,
}
pub trait Walk<'a>: Record<'a> {
fn walk<F: FnMut(Node<'a, Self::At>)>(self, visit: F) -> Result<(), Miss>;
fn read_whole(self) -> bool {
true
}
fn bytes(self, at: Self::At) -> Result<Cow<'a, [u8]>, Miss> {
self.text(at).map(text_bytes)
}
}
fn text_bytes(text: Cow<'_, str>) -> Cow<'_, [u8]> {
match text {
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
}
}
#[cfg(feature = "expr")]
pub(crate) fn object_keys(obj: &str) -> Result<Vec<String>, Miss> {
json::object_keys(obj)
}
fn number(raw: &str) -> Result<Typed<'_>, Miss> {
NumberReading::Cel
.read(raw)
.map(typed_number)
.ok_or(Miss::Malformed)
}
fn number_exact(raw: &str) -> Result<Typed<'_>, Miss> {
match NumberReading::Exact.read(raw) {
Some(number) => Ok(typed_number(number)),
None if crate::number::decimal(raw.as_bytes()).is_some() => {
Ok(Typed::Str(Cow::Borrowed(raw)))
}
None => Err(Miss::Malformed),
}
}
const fn typed_number<'a>(number: Number) -> Typed<'a> {
match number {
Number::Int(n) => Typed::Int(n),
Number::UInt(n) => Typed::Uint(n),
Number::Float(n) => Typed::Double(n),
}
}
#[cfg(test)]
mod tests;