Skip to content

Commit

Permalink
WIP: zipmerge
Browse files Browse the repository at this point in the history
  • Loading branch information
thorio committed Jul 27, 2024
1 parent bea1620 commit 567a344
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/coalesce.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Profile;
use crate::value::{Empty, Map, Value};
use crate::value::{Map, Value};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Order {
Expand Down Expand Up @@ -58,14 +58,14 @@ impl Coalescible for Vec<Value> {
let mut zipped = Vec::new();
let mut other = other.into_iter();

// Coalesces self.0 with other.0, self.1 with other.1 and so on.
// Coalesces self[0] with other[0], self[1] with other[1] and so on.
for a_val in self.into_iter() {
match other.next() {
// Special cases: either a or b has an empty value, in which
// case we always choose the non-empty one regardless of order.
// If both are empty it just pushed one of the empties.
Some(b_val) if matches!(a_val, Value::Empty(_, Empty::None)) => zipped.push(b_val),
Some(Value::Empty(_, Empty::None)) => zipped.push(a_val),
// If both are empty we just push either of the empties.
Some(b_val) if a_val.is_none() => zipped.push(b_val),
Some(b_val) if b_val.is_none() => zipped.push(a_val),

Some(b_val) => zipped.push(a_val.coalesce(b_val, order)),
None => zipped.push(a_val),
Expand Down
2 changes: 0 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ pub fn nest(key: &str, value: Value) -> Value {
return value;
};

// TODO: Is it ok to use empty for this, or should there be a special
// placeholder value that cannot be deserialized, with a special error?
if let Ok(index) = key.parse::<usize>() {
let mut vec = vec![Value::from(Empty::None); index + 1];
vec[index] = value_from(keys, value);
Expand Down
4 changes: 4 additions & 0 deletions src/value/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ impl Value {
}
}

pub(crate) fn is_none(&self) -> bool {
matches!(self, Value::Empty(_, Empty::None))
}

/// Attempts to retrieve a nested value through dictionary key or array index.
pub(crate) fn index(self, key: &str) -> Option<Value> {
fn try_remove<T>(mut vec: Vec<T>, key: &str) -> Option<T> {
Expand Down

0 comments on commit 567a344

Please sign in to comment.