From 1dd30957830fbb9c572ce0fdc2dab29dea94a2b8 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 4 May 2022 22:45:00 +0100 Subject: [PATCH] Implement SQL Timeout as a newtype tuple struct --- lib/src/sql/statement.rs | 12 ++++++------ lib/src/sql/timeout.rs | 26 +++++++++++--------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/src/sql/statement.rs b/lib/src/sql/statement.rs index 1c43f435..b474ce64 100644 --- a/lib/src/sql/statement.rs +++ b/lib/src/sql/statement.rs @@ -84,12 +84,12 @@ pub enum Statement { impl Statement { pub fn timeout(&self) -> Option { match self { - Statement::Select(v) => v.timeout.as_ref().map(|v| v.expr.value), - Statement::Create(v) => v.timeout.as_ref().map(|v| v.expr.value), - Statement::Update(v) => v.timeout.as_ref().map(|v| v.expr.value), - Statement::Relate(v) => v.timeout.as_ref().map(|v| v.expr.value), - Statement::Delete(v) => v.timeout.as_ref().map(|v| v.expr.value), - Statement::Insert(v) => v.timeout.as_ref().map(|v| v.expr.value), + Statement::Select(v) => v.timeout.as_ref().map(|v| v.value), + Statement::Create(v) => v.timeout.as_ref().map(|v| v.value), + Statement::Update(v) => v.timeout.as_ref().map(|v| v.value), + Statement::Relate(v) => v.timeout.as_ref().map(|v| v.value), + Statement::Delete(v) => v.timeout.as_ref().map(|v| v.value), + Statement::Insert(v) => v.timeout.as_ref().map(|v| v.value), _ => None, } } diff --git a/lib/src/sql/timeout.rs b/lib/src/sql/timeout.rs index becb9bf3..81b2cdca 100644 --- a/lib/src/sql/timeout.rs +++ b/lib/src/sql/timeout.rs @@ -4,15 +4,21 @@ use crate::sql::error::IResult; 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 Timeout { - pub expr: Duration, +pub struct Timeout(pub Duration); + +impl Deref for Timeout { + type Target = Duration; + fn deref(&self) -> &Self::Target { + &self.0 + } } impl fmt::Display for Timeout { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "TIMEOUT {}", self.expr) + write!(f, "TIMEOUT {}", self.0) } } @@ -20,12 +26,7 @@ pub fn timeout(i: &str) -> IResult<&str, Timeout> { let (i, _) = tag_no_case("TIMEOUT")(i)?; let (i, _) = shouldbespace(i)?; let (i, v) = duration(i)?; - Ok(( - i, - Timeout { - expr: v, - }, - )) + Ok((i, Timeout(v))) } #[cfg(test)] @@ -39,12 +40,7 @@ mod tests { let res = timeout(sql); assert!(res.is_ok()); let out = res.unwrap().1; - assert_eq!( - out, - Timeout { - expr: Duration::from("5s") - } - ); + assert_eq!(out, Timeout(Duration::from("5s"))); assert_eq!("TIMEOUT 5s", format!("{}", out)); } }