From 0a3fe673589edf89c5a22553ce7439f1b3e6cb97 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 5 May 2022 05:51:13 +0100 Subject: [PATCH] Implement SQL Cond as a newtype tuple struct --- lib/src/doc/check.rs | 2 +- lib/src/sql/cond.rs | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/src/doc/check.rs b/lib/src/doc/check.rs index 16d0b633..d5c4d914 100644 --- a/lib/src/doc/check.rs +++ b/lib/src/doc/check.rs @@ -16,7 +16,7 @@ impl<'a> Document<'a> { // Check where condition if let Some(cond) = stm.conds() { // Check if the expression is truthy - if !cond.expr.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() { + if !cond.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() { // Ignore this document return Err(Error::Ignore); } diff --git a/lib/src/sql/cond.rs b/lib/src/sql/cond.rs index 40031806..f1565a11 100644 --- a/lib/src/sql/cond.rs +++ b/lib/src/sql/cond.rs @@ -4,15 +4,21 @@ use crate::sql::value::{value, Value}; use nom::bytes::complete::tag_no_case; use serde::{Deserialize, Serialize}; use std::fmt; +use std::ops::Deref; #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] -pub struct Cond { - pub expr: Value, +pub struct Cond(pub Value); + +impl Deref for Cond { + type Target = Value; + fn deref(&self) -> &Self::Target { + &self.0 + } } impl fmt::Display for Cond { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "WHERE {}", self.expr) + write!(f, "WHERE {}", self.0) } } @@ -20,12 +26,7 @@ pub fn cond(i: &str) -> IResult<&str, Cond> { let (i, _) = tag_no_case("WHERE")(i)?; let (i, _) = shouldbespace(i)?; let (i, v) = value(i)?; - Ok(( - i, - Cond { - expr: v, - }, - )) + Ok((i, Cond(v))) } #[cfg(test)]