From 6de9e6622fc1301157b1af06e3114c9b0736a8c4 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Sun, 15 Sep 2024 15:39:19 -0400 Subject: [PATCH] chore: make merge_right return valid --- src/core/merge_right.rs | 68 +++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/core/merge_right.rs b/src/core/merge_right.rs index 34f7d5f2e1..f0396e6d4d 100644 --- a/src/core/merge_right.rs +++ b/src/core/merge_right.rs @@ -2,34 +2,37 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::sync::Arc; use serde_yaml::Value; +use crate::core::valid::{Valid, Validator}; pub trait MergeRight { - fn merge_right(self, other: Self) -> Self; + fn merge_right(self, other: Self) -> Valid; } impl MergeRight for Option { - fn merge_right(self, other: Self) -> Self { - match (self, other) { + fn merge_right(self, other: Self) -> Valid { + let valid = match (self, other) { (Some(this), Some(that)) => Some(this.merge_right(that)), (None, Some(that)) => Some(that), (Some(this), None) => Some(this), (None, None) => None, - } + }; + Valid::succeed(valid) } } impl MergeRight for Arc { - fn merge_right(self, other: Self) -> Self { + fn merge_right(self, other: Self) -> Valid { let l = Arc::into_inner(self); let r = Arc::into_inner(other); - Arc::new(l.merge_right(r).unwrap_or_default()) + let valid = Arc::new(l.merge_right(r).unwrap_or_default()); + Valid::succeed(valid) } } impl MergeRight for Vec { - fn merge_right(mut self, other: Self) -> Self { + fn merge_right(mut self, other: Self) -> Valid { self.extend(other); - self + Valid::succeed(self) } } @@ -38,7 +41,7 @@ where K: Ord, V: Clone + MergeRight, { - fn merge_right(mut self, other: Self) -> Self { + fn merge_right(mut self, other: Self) -> Valid { for (other_name, mut other_value) in other { if let Some(self_value) = self.remove(&other_name) { other_value = self_value.merge_right(other_value); @@ -46,7 +49,7 @@ where self.insert(other_name, other_value); } - self + Valid::succeed(self) } } @@ -54,9 +57,9 @@ impl MergeRight for BTreeSet where V: Ord, { - fn merge_right(mut self, other: Self) -> Self { + fn merge_right(mut self, other: Self) -> Valid { self.extend(other); - self + Valid::succeed(self) } } @@ -74,52 +77,59 @@ impl MergeRight for HashMap where K: Eq + std::hash::Hash, { - fn merge_right(mut self, other: Self) -> Self { + fn merge_right(mut self, other: Self) -> Valid { self.extend(other); - self + Valid::succeed(self) } } impl MergeRight for Value { - fn merge_right(self, other: Self) -> Self { + fn merge_right(self, other: Self) -> Valid { match (self, other) { - (Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_), other) => other, + (Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_), other) => Valid::succeed(other), (Value::Sequence(mut lhs), other) => match other { Value::Sequence(rhs) => { lhs.extend(rhs); - Value::Sequence(lhs) + Valid::succeed( Value::Sequence(lhs)) } other => { lhs.push(other); - Value::Sequence(lhs) + Valid::succeed(Value::Sequence(lhs)) } }, (Value::Mapping(mut lhs), other) => match other { Value::Mapping(rhs) => { - for (key, mut value) in rhs { + Valid::from_iter(rhs, |(key, value)| { if let Some(lhs_value) = lhs.remove(&key) { - value = lhs_value.merge_right(value); + value.merge_right(lhs_value).map(|value| (key, value)) + } else { + Valid::succeed((key, value)) } - lhs.insert(key, value); - } - Value::Mapping(lhs) + }).and_then(|(value)| { + for (k,v) in value { + lhs.insert(k,v); + } + Valid::succeed(Value::Mapping(lhs)) + }) } Value::Sequence(mut rhs) => { rhs.push(Value::Mapping(lhs)); - Value::Sequence(rhs) + Valid::succeed(Value::Sequence(rhs)) } - other => other, + other => Valid::succeed(other), }, (Value::Tagged(mut lhs), other) => match other { Value::Tagged(rhs) => { if lhs.tag == rhs.tag { - lhs.value = lhs.value.merge_right(rhs.value); - Value::Tagged(lhs) + lhs.value.clone().merge_right(rhs.value).and_then(|value| { + lhs.value = value; + Valid::succeed(Value::Tagged(lhs)) + }) } else { - Value::Tagged(rhs) + Valid::succeed(Value::Tagged(rhs)) } } - other => other, + other => Valid::succeed(other), }, } }