Skip to content

Commit

Permalink
chore: make merge_right return valid
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed Sep 15, 2024
1 parent d02b6e6 commit 6de9e66
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/core/merge_right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::sync::Arc;

Check warning on line 2 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/core/merge_right.rs

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<Self, String>;
}

impl<A: MergeRight> MergeRight for Option<A> {
fn merge_right(self, other: Self) -> Self {
match (self, other) {
fn merge_right(self, other: Self) -> Valid<Self, String> {
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<A: MergeRight + Default> MergeRight for Arc<A> {
fn merge_right(self, other: Self) -> Self {
fn merge_right(self, other: Self) -> Valid<Self, String> {
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<A> MergeRight for Vec<A> {
fn merge_right(mut self, other: Self) -> Self {
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
self.extend(other);
self
Valid::succeed(self)
}
}

Expand All @@ -38,25 +41,25 @@ where
K: Ord,
V: Clone + MergeRight,
{
fn merge_right(mut self, other: Self) -> Self {
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
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);
}

self.insert(other_name, other_value);
}
self
Valid::succeed(self)
}
}

impl<V> MergeRight for BTreeSet<V>
where
V: Ord,
{
fn merge_right(mut self, other: Self) -> Self {
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
self.extend(other);
self
Valid::succeed(self)
}
}

Expand All @@ -74,52 +77,59 @@ impl<K, V> MergeRight for HashMap<K, V>
where
K: Eq + std::hash::Hash,
{
fn merge_right(mut self, other: Self) -> Self {
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
self.extend(other);
self
Valid::succeed(self)
}
}

impl MergeRight for Value {

Check warning on line 86 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/core/merge_right.rs
fn merge_right(self, other: Self) -> Self {
fn merge_right(self, other: Self) -> Valid<Self, String> {
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))

Check warning on line 93 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/core/merge_right.rs
}
other => {
lhs.push(other);
Value::Sequence(lhs)
Valid::succeed(Value::Sequence(lhs))
}

Check warning on line 98 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/core/merge_right.rs
},
(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)| {

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Check Examples

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (Cloudflare)

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-gnu

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

unnecessary parentheses around pattern

Check failure on line 108 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

unnecessary parentheses around pattern
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),
},
}
}
Expand Down

0 comments on commit 6de9e66

Please sign in to comment.