Use separate module for Dir type

This commit is contained in:
Tobie Morgan Hitchcock 2022-06-08 18:46:17 +01:00
parent a8fa9ecfb7
commit 1333ecf0df
5 changed files with 89 additions and 28 deletions

View file

@ -5,7 +5,7 @@ use crate::dbs::Transaction;
use crate::dbs::Workable; use crate::dbs::Workable;
use crate::doc::Document; use crate::doc::Document;
use crate::err::Error; use crate::err::Error;
use crate::sql::graph::Dir; use crate::sql::dir::Dir;
impl<'a> Document<'a> { impl<'a> Document<'a> {
pub async fn edges( pub async fn edges(

View file

@ -1,5 +1,5 @@
use crate::err::Error; use crate::err::Error;
use crate::sql::graph::Dir; use crate::sql::dir::Dir;
use crate::sql::id::Id; use crate::sql::id::Id;
use crate::sql::thing::Thing; use crate::sql::thing::Thing;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

82
lib/src/sql/dir.rs Normal file
View file

@ -0,0 +1,82 @@
use crate::sql::error::IResult;
use nom::branch::alt;
use nom::character::complete::char;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum Dir {
In,
Out,
Both,
}
impl Default for Dir {
fn default() -> Dir {
Dir::Both
}
}
impl fmt::Display for Dir {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Dir::In => write!(f, "<-"),
Dir::Out => write!(f, "->"),
Dir::Both => write!(f, "<->"),
}
}
}
pub fn dir(i: &str) -> IResult<&str, Dir> {
alt((
|i| {
let (i, _) = char('<')(i)?;
let (i, _) = char('-')(i)?;
let (i, _) = char('>')(i)?;
Ok((i, Dir::Both))
},
|i| {
let (i, _) = char('<')(i)?;
let (i, _) = char('-')(i)?;
Ok((i, Dir::In))
},
|i| {
let (i, _) = char('-')(i)?;
let (i, _) = char('>')(i)?;
Ok((i, Dir::Out))
},
))(i)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dir_in() {
let sql = "<-";
let res = dir(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("<-", format!("{}", out));
}
#[test]
fn dir_out() {
let sql = "->";
let res = dir(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("->", format!("{}", out));
}
#[test]
fn dir_both() {
let sql = "<->";
let res = dir(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("<->", format!("{}", out));
}
}

View file

@ -1,5 +1,6 @@
use crate::sql::comment::mightbespace; use crate::sql::comment::mightbespace;
use crate::sql::comment::shouldbespace; use crate::sql::comment::shouldbespace;
use crate::sql::dir::{dir, Dir};
use crate::sql::error::IResult; use crate::sql::error::IResult;
use crate::sql::idiom::{idiom, Idiom}; use crate::sql::idiom::{idiom, Idiom};
use crate::sql::table::{table, tables, Tables}; use crate::sql::table::{table, tables, Tables};
@ -12,29 +13,6 @@ use nom::combinator::opt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum Dir {
In,
Out,
Both,
}
impl Default for Dir {
fn default() -> Dir {
Dir::Both
}
}
impl fmt::Display for Dir {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Dir::In => write!(f, "<-"),
Dir::Out => write!(f, "->"),
Dir::Both => write!(f, "<->"),
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Graph { pub struct Graph {
pub dir: Dir, pub dir: Dir,
@ -46,13 +24,13 @@ pub struct Graph {
impl fmt::Display for Graph { impl fmt::Display for Graph {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.what.0.len() <= 1 && self.cond.is_none() && self.alias.is_none() { if self.what.0.len() <= 1 && self.cond.is_none() && self.alias.is_none() {
write!(f, "{}", self.dir,)?; write!(f, "{}", self.dir)?;
match self.what.len() { match self.what.len() {
0 => write!(f, "?"), 0 => write!(f, "?"),
_ => write!(f, "{}", self.what), _ => write!(f, "{}", self.what),
} }
} else { } else {
write!(f, "{}(", self.dir,)?; write!(f, "{}(", self.dir)?;
match self.what.len() { match self.what.len() {
0 => write!(f, "?"), 0 => write!(f, "?"),
_ => write!(f, "{}", self.what), _ => write!(f, "{}", self.what),

View file

@ -6,6 +6,7 @@ pub(crate) mod common;
pub(crate) mod cond; pub(crate) mod cond;
pub(crate) mod data; pub(crate) mod data;
pub(crate) mod datetime; pub(crate) mod datetime;
pub(crate) mod dir;
pub(crate) mod duration; pub(crate) mod duration;
pub(crate) mod error; pub(crate) mod error;
pub(crate) mod escape; pub(crate) mod escape;
@ -61,6 +62,7 @@ pub use self::base::Base;
pub use self::cond::Cond; pub use self::cond::Cond;
pub use self::data::Data; pub use self::data::Data;
pub use self::datetime::Datetime; pub use self::datetime::Datetime;
pub use self::dir::Dir;
pub use self::duration::Duration; pub use self::duration::Duration;
pub use self::error::Error; pub use self::error::Error;
pub use self::expression::Expression; pub use self::expression::Expression;
@ -70,7 +72,6 @@ pub use self::field::Field;
pub use self::field::Fields; pub use self::field::Fields;
pub use self::function::Function; pub use self::function::Function;
pub use self::geometry::Geometry; pub use self::geometry::Geometry;
pub use self::graph::Dir;
pub use self::graph::Graph; pub use self::graph::Graph;
pub use self::group::Group; pub use self::group::Group;
pub use self::group::Groups; pub use self::group::Groups;