Implement SQL Timeout as a newtype tuple struct
This commit is contained in:
parent
42fa91c7bd
commit
1dd3095783
2 changed files with 17 additions and 21 deletions
|
@ -84,12 +84,12 @@ pub enum Statement {
|
||||||
impl Statement {
|
impl Statement {
|
||||||
pub fn timeout(&self) -> Option<Duration> {
|
pub fn timeout(&self) -> Option<Duration> {
|
||||||
match self {
|
match self {
|
||||||
Statement::Select(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.expr.value),
|
Statement::Create(v) => v.timeout.as_ref().map(|v| v.value),
|
||||||
Statement::Update(v) => v.timeout.as_ref().map(|v| v.expr.value),
|
Statement::Update(v) => v.timeout.as_ref().map(|v| v.value),
|
||||||
Statement::Relate(v) => v.timeout.as_ref().map(|v| v.expr.value),
|
Statement::Relate(v) => v.timeout.as_ref().map(|v| v.value),
|
||||||
Statement::Delete(v) => v.timeout.as_ref().map(|v| v.expr.value),
|
Statement::Delete(v) => v.timeout.as_ref().map(|v| v.value),
|
||||||
Statement::Insert(v) => v.timeout.as_ref().map(|v| v.expr.value),
|
Statement::Insert(v) => v.timeout.as_ref().map(|v| v.value),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,15 +4,21 @@ use crate::sql::error::IResult;
|
||||||
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 Timeout {
|
pub struct Timeout(pub Duration);
|
||||||
pub expr: Duration,
|
|
||||||
|
impl Deref for Timeout {
|
||||||
|
type Target = Duration;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Timeout {
|
impl fmt::Display for Timeout {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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, _) = tag_no_case("TIMEOUT")(i)?;
|
||||||
let (i, _) = shouldbespace(i)?;
|
let (i, _) = shouldbespace(i)?;
|
||||||
let (i, v) = duration(i)?;
|
let (i, v) = duration(i)?;
|
||||||
Ok((
|
Ok((i, Timeout(v)))
|
||||||
i,
|
|
||||||
Timeout {
|
|
||||||
expr: v,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -39,12 +40,7 @@ mod tests {
|
||||||
let res = timeout(sql);
|
let res = timeout(sql);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
let out = res.unwrap().1;
|
let out = res.unwrap().1;
|
||||||
assert_eq!(
|
assert_eq!(out, Timeout(Duration::from("5s")));
|
||||||
out,
|
|
||||||
Timeout {
|
|
||||||
expr: Duration::from("5s")
|
|
||||||
}
|
|
||||||
);
|
|
||||||
assert_eq!("TIMEOUT 5s", format!("{}", out));
|
assert_eq!("TIMEOUT 5s", format!("{}", out));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue