Implement SQL Cond as a newtype tuple struct

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-05 05:51:13 +01:00
parent 1ed5df005e
commit 0a3fe67358
2 changed files with 11 additions and 10 deletions

View file

@ -16,7 +16,7 @@ impl<'a> Document<'a> {
// Check where condition // Check where condition
if let Some(cond) = stm.conds() { if let Some(cond) = stm.conds() {
// Check if the expression is truthy // 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 // Ignore this document
return Err(Error::Ignore); return Err(Error::Ignore);
} }

View file

@ -4,15 +4,21 @@ use crate::sql::value::{value, Value};
use nom::bytes::complete::tag_no_case; use nom::bytes::complete::tag_no_case;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::ops::Deref;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Cond { pub struct Cond(pub Value);
pub expr: Value,
impl Deref for Cond {
type Target = Value;
fn deref(&self) -> &Self::Target {
&self.0
}
} }
impl fmt::Display for Cond { impl fmt::Display for Cond {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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, _) = tag_no_case("WHERE")(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = value(i)?; let (i, v) = value(i)?;
Ok(( Ok((i, Cond(v)))
i,
Cond {
expr: v,
},
))
} }
#[cfg(test)] #[cfg(test)]