From b6cc3ca1ffd04cb0695910b14f86fbb3b4f1fa61 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 4 May 2022 21:05:21 +0100 Subject: [PATCH] Implement SQL Fetch as a newtype tuple struct --- lib/src/dbs/iterator.rs | 8 +++---- lib/src/sql/fetch.rs | 50 ++++++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/src/dbs/iterator.rs b/lib/src/dbs/iterator.rs index 1b335c29..189eecb0 100644 --- a/lib/src/dbs/iterator.rs +++ b/lib/src/dbs/iterator.rs @@ -351,24 +351,24 @@ impl Iterator { // Loop over each value for obj in &mut self.results { // Get the value at the path - let val = obj.get(ctx, opt, txn, &fetch.fetch).await?; + let val = obj.get(ctx, opt, txn, fetch).await?; // Set the value at the path match val { Value::Array(v) => { // Fetch all remote records let val = Value::Array(v).get(ctx, opt, txn, &[Part::All]).await?; // Set the value at the path - obj.set(ctx, opt, txn, &fetch.fetch, val).await?; + obj.set(ctx, opt, txn, fetch, val).await?; } Value::Thing(v) => { // Fetch all remote records let val = Value::Thing(v).get(ctx, opt, txn, &[Part::All]).await?; // Set the value at the path - obj.set(ctx, opt, txn, &fetch.fetch, val).await?; + obj.set(ctx, opt, txn, fetch, val).await?; } _ => { // Set the value at the path - obj.set(ctx, opt, txn, &fetch.fetch, val).await?; + obj.set(ctx, opt, txn, fetch, val).await?; } } } diff --git a/lib/src/sql/fetch.rs b/lib/src/sql/fetch.rs index 7c224091..65ab912f 100644 --- a/lib/src/sql/fetch.rs +++ b/lib/src/sql/fetch.rs @@ -6,10 +6,26 @@ use nom::bytes::complete::tag_no_case; use nom::multi::separated_list1; use serde::{Deserialize, Serialize}; use std::fmt; +use std::ops::Deref; #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct Fetchs(pub Vec); +impl Deref for Fetchs { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl IntoIterator for Fetchs { + type Item = Fetch; + type IntoIter = std::vec::IntoIter; + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + impl fmt::Display for Fetchs { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( @@ -21,13 +37,18 @@ impl fmt::Display for Fetchs { } #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] -pub struct Fetch { - pub fetch: Idiom, +pub struct Fetch(pub Idiom); + +impl Deref for Fetch { + type Target = Idiom; + fn deref(&self) -> &Self::Target { + &self.0 + } } impl fmt::Display for Fetch { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.fetch) + write!(f, "{}", self.0) } } @@ -40,12 +61,7 @@ pub fn fetch(i: &str) -> IResult<&str, Fetchs> { fn fetch_raw(i: &str) -> IResult<&str, Fetch> { let (i, v) = idiom(i)?; - Ok(( - i, - Fetch { - fetch: v, - }, - )) + Ok((i, Fetch(v))) } #[cfg(test)] @@ -60,12 +76,7 @@ mod tests { let res = fetch(sql); assert!(res.is_ok()); let out = res.unwrap().1; - assert_eq!( - out, - Fetchs(vec![Fetch { - fetch: Idiom::parse("field") - }]) - ); + assert_eq!(out, Fetchs(vec![Fetch(Idiom::parse("field"))])); assert_eq!("FETCH field", format!("{}", out)); } @@ -77,14 +88,7 @@ mod tests { let out = res.unwrap().1; assert_eq!( out, - Fetchs(vec![ - Fetch { - fetch: Idiom::parse("field") - }, - Fetch { - fetch: Idiom::parse("other.field") - }, - ]) + Fetchs(vec![Fetch(Idiom::parse("field")), Fetch(Idiom::parse("other.field")),]) ); assert_eq!("FETCH field, other.field", format!("{}", out)); }