surrealpatch/lib/src/sql/dir.rs

64 lines
1.1 KiB
Rust
Raw Normal View History

2022-06-08 17:46:17 +00:00
use crate::sql::error::IResult;
use nom::{branch::alt, bytes::complete::tag, combinator::value};
use revision::revisioned;
2022-06-08 17:46:17 +00:00
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[revisioned(revision = 1)]
2022-06-08 17:46:17 +00:00
pub enum Dir {
In,
Out,
Both,
}
impl Default for Dir {
fn default() -> Self {
Self::Both
2022-06-08 17:46:17 +00:00
}
}
impl fmt::Display for Dir {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
Self::In => "<-",
Self::Out => "->",
Self::Both => "<->",
})
2022-06-08 17:46:17 +00:00
}
}
pub fn dir(i: &str) -> IResult<&str, Dir> {
alt((value(Dir::Both, tag("<->")), value(Dir::In, tag("<-")), value(Dir::Out, tag("->"))))(i)
2022-06-08 17:46:17 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dir_in() {
let sql = "<-";
let res = dir(sql);
let out = res.unwrap().1;
assert_eq!("<-", format!("{}", out));
}
#[test]
fn dir_out() {
let sql = "->";
let res = dir(sql);
let out = res.unwrap().1;
assert_eq!("->", format!("{}", out));
}
#[test]
fn dir_both() {
let sql = "<->";
let res = dir(sql);
let out = res.unwrap().1;
assert_eq!("<->", format!("{}", out));
}
}