use std::borrow::Cow;
#[cfg(feature = "parse")]
mod fields;
mod json;
mod line;
#[cfg(feature = "parse")]
pub use fields::FieldsAt;
pub use json::Json;
pub use line::Line;
#[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 text(self, at: Self::At) -> Result<Cow<'a, str>, Miss>;
}
#[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> {
let float = || {
raw.parse::<f64>()
.map(Typed::Double)
.map_err(|_| Miss::Malformed)
};
if raw.bytes().any(|b| matches!(b, b'.' | b'e' | b'E')) {
return float();
}
if raw.starts_with('-') {
return raw.parse::<i64>().map(Typed::Int).or_else(|_| float());
}
raw.parse::<u64>().map_or_else(
|_| float(),
|u| Ok(i64::try_from(u).map_or(Typed::Uint(u), Typed::Int)),
)
}
#[cfg(test)]
mod tests;