From 643592d750e8ad7171b9f075c9d46643bef51e93 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 4 May 2022 22:02:26 +0100 Subject: [PATCH] Implement SQL Split as a newtype tuple struct --- lib/src/dbs/iterator.rs | 6 +++--- lib/src/sql/split.rs | 41 ++++++++++++----------------------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/lib/src/dbs/iterator.rs b/lib/src/dbs/iterator.rs index 189eecb0..be4b0117 100644 --- a/lib/src/dbs/iterator.rs +++ b/lib/src/dbs/iterator.rs @@ -165,7 +165,7 @@ impl Iterator { // Loop over each value for obj in &res { // Get the value at the path - let val = obj.pick(&split.split); + let val = obj.pick(split); // Set the value at the path match val { Value::Array(v) => { @@ -173,7 +173,7 @@ impl Iterator { // Make a copy of object let mut obj = obj.clone(); // Set the value at the path - obj.set(ctx, opt, txn, &split.split, val).await?; + obj.set(ctx, opt, txn, split, val).await?; // Add the object to the results self.results.push(obj); } @@ -182,7 +182,7 @@ impl Iterator { // Make a copy of object let mut obj = obj.clone(); // Set the value at the path - obj.set(ctx, opt, txn, &split.split, val).await?; + obj.set(ctx, opt, txn, split, val).await?; // Add the object to the results self.results.push(obj); } diff --git a/lib/src/sql/split.rs b/lib/src/sql/split.rs index 4d506e66..5dbf185a 100644 --- a/lib/src/sql/split.rs +++ b/lib/src/sql/split.rs @@ -48,13 +48,18 @@ impl fmt::Display for Splits { } #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] -pub struct Split { - pub split: Idiom, +pub struct Split(pub Idiom); + +impl Deref for Split { + type Target = Idiom; + fn deref(&self) -> &Self::Target { + &self.0 + } } impl fmt::Display for Split { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.split) + write!(f, "{}", self.0) } } @@ -68,12 +73,7 @@ pub fn split(i: &str) -> IResult<&str, Splits> { fn split_raw(i: &str) -> IResult<&str, Split> { let (i, v) = basic(i)?; - Ok(( - i, - Split { - split: v, - }, - )) + Ok((i, Split(v))) } #[cfg(test)] @@ -88,12 +88,7 @@ mod tests { let res = split(sql); assert!(res.is_ok()); let out = res.unwrap().1; - assert_eq!( - out, - Splits(vec![Split { - split: Idiom::parse("field") - }]) - ); + assert_eq!(out, Splits(vec![Split(Idiom::parse("field"))]),); assert_eq!("SPLIT ON field", format!("{}", out)); } @@ -103,12 +98,7 @@ mod tests { let res = split(sql); assert!(res.is_ok()); let out = res.unwrap().1; - assert_eq!( - out, - Splits(vec![Split { - split: Idiom::parse("field") - }]) - ); + assert_eq!(out, Splits(vec![Split(Idiom::parse("field"))]),); assert_eq!("SPLIT ON field", format!("{}", out)); } @@ -120,14 +110,7 @@ mod tests { let out = res.unwrap().1; assert_eq!( out, - Splits(vec![ - Split { - split: Idiom::parse("field") - }, - Split { - split: Idiom::parse("other.field") - }, - ]) + Splits(vec![Split(Idiom::parse("field")), Split(Idiom::parse("other.field")),]) ); assert_eq!("SPLIT ON field, other.field", format!("{}", out)); }