use simd_json::value::tape::Node;
use crate::models::decoders::json::simd::TapeOps;
#[derive(Debug, Clone)]
enum PathStep {
Key(String),
Index(usize),
}
#[derive(Debug, Clone)]
pub struct JsonPath(Vec<PathStep>);
impl JsonPath {
pub fn parse(path: &str) -> Self {
JsonPath(
path.split('.')
.filter(|s| !s.is_empty())
.map(|s| match s.parse::<usize>() {
Ok(i) => PathStep::Index(i),
Err(_) => PathStep::Key(s.to_string()),
})
.collect(),
)
}
pub(crate) fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub(crate) fn resolve(&self, nodes: &[Node<'_>], start: usize) -> Option<usize> {
let mut cur = start;
for step in &self.0 {
cur = match step {
PathStep::Key(k) => nodes.object_value_index(cur, k)?,
PathStep::Index(i) => nodes.array_element_index(cur, *i)?,
};
}
Some(cur)
}
}