Skip to content

Commit

Permalink
Add array support for find
Browse files Browse the repository at this point in the history
  • Loading branch information
thorio committed Feb 6, 2024
1 parent 5180f6d commit cf267ec
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/value/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,23 @@ impl Value {
pub fn find(self, path: &str) -> Option<Value> {
fn find(mut keys: Split<char>, value: Value) -> Option<Value> {
match keys.next() {
Some(k) if !k.is_empty() => find(keys, value.into_dict()?.remove(k)?),
Some(_) | None => Some(value)
Some(k) if !k.is_empty() => find(keys, get_value(k, value)?),
Some(_) | None => Some(value),
}
}

fn get_value(key: &str, value: Value) -> Option<Value> {
match value {
Value::Dict(_, mut dict) => dict.remove(key),
Value::Array(_, mut array) => {
let index = key.parse().ok()?;
if index >= array.len() {
return None;
}

Some(array.swap_remove(index))
}
_ => None,
}
}

Expand Down

0 comments on commit cf267ec

Please sign in to comment.