surrealpatch/src/sql/version.rs

51 lines
997 B
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use crate::sql::comment::shouldbespace;
use crate::sql::datetime::{datetime, Datetime};
use nom::bytes::complete::tag_no_case;
use nom::IResult;
use serde::{Deserialize, Serialize};
use std::fmt;
2021-03-29 15:43:37 +00:00
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
2020-06-29 15:36:01 +00:00
pub struct Version {
pub expr: Datetime,
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "VERSION {}", self.expr)
}
}
pub fn version(i: &str) -> IResult<&str, Version> {
let (i, _) = tag_no_case("VERSION")(i)?;
let (i, _) = shouldbespace(i)?;
let (i, v) = datetime(i)?;
2021-03-29 15:43:37 +00:00
Ok((
i,
Version {
expr: v,
},
))
2020-06-29 15:36:01 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_statement() {
let sql = "VERSION '2020-01-01'";
let res = version(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!(
out,
Version {
expr: Datetime::from("2020-01-01")
}
);
assert_eq!("VERSION \"2020-01-01T00:00:00Z\"", format!("{}", out));
}
}