2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
|
|
|
use crate::sql::duration::{duration, Duration};
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::tag_no_case;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
2022-05-04 21:45:00 +00:00
|
|
|
use std::ops::Deref;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2022-05-04 21:45:00 +00:00
|
|
|
pub struct Timeout(pub Duration);
|
|
|
|
|
|
|
|
impl Deref for Timeout {
|
|
|
|
type Target = Duration;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Timeout {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-05-04 21:45:00 +00:00
|
|
|
write!(f, "TIMEOUT {}", self.0)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timeout(i: &str) -> IResult<&str, Timeout> {
|
|
|
|
let (i, _) = tag_no_case("TIMEOUT")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, v) = duration(i)?;
|
2022-05-04 21:45:00 +00:00
|
|
|
Ok((i, Timeout(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn timeout_statement() {
|
|
|
|
let sql = "TIMEOUT 5s";
|
|
|
|
let res = timeout(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("TIMEOUT 5s", format!("{}", out));
|
2023-04-25 10:13:04 +00:00
|
|
|
assert_eq!(out, Timeout(Duration::try_from("5s").unwrap()));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|