surrealpatch/lib/src/sql/version.rs
Tobie Morgan Hitchcock 66b105dac0
Add support for stricter typings (#1861)
Co-authored-by: Rushmore Mushambi <rushmore@surrealdb.com>
2023-04-25 11:13:04 +01:00

38 lines
998 B
Rust

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