From 1333ecf0dfa060a8be0cf8c2b57abed6d3df7f2e Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 8 Jun 2022 18:46:17 +0100 Subject: [PATCH] Use separate module for Dir type --- lib/src/doc/edges.rs | 2 +- lib/src/key/graph.rs | 2 +- lib/src/sql/dir.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++ lib/src/sql/graph.rs | 28 ++------------- lib/src/sql/mod.rs | 3 +- 5 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 lib/src/sql/dir.rs diff --git a/lib/src/doc/edges.rs b/lib/src/doc/edges.rs index 712bc72b..61b6b7eb 100644 --- a/lib/src/doc/edges.rs +++ b/lib/src/doc/edges.rs @@ -5,7 +5,7 @@ use crate::dbs::Transaction; use crate::dbs::Workable; use crate::doc::Document; use crate::err::Error; -use crate::sql::graph::Dir; +use crate::sql::dir::Dir; impl<'a> Document<'a> { pub async fn edges( diff --git a/lib/src/key/graph.rs b/lib/src/key/graph.rs index 7d07f5d7..376c2b6d 100644 --- a/lib/src/key/graph.rs +++ b/lib/src/key/graph.rs @@ -1,5 +1,5 @@ use crate::err::Error; -use crate::sql::graph::Dir; +use crate::sql::dir::Dir; use crate::sql::id::Id; use crate::sql::thing::Thing; use serde::{Deserialize, Serialize}; diff --git a/lib/src/sql/dir.rs b/lib/src/sql/dir.rs new file mode 100644 index 00000000..832e160d --- /dev/null +++ b/lib/src/sql/dir.rs @@ -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)); + } +} diff --git a/lib/src/sql/graph.rs b/lib/src/sql/graph.rs index 05809c91..8fb05304 100644 --- a/lib/src/sql/graph.rs +++ b/lib/src/sql/graph.rs @@ -1,5 +1,6 @@ use crate::sql::comment::mightbespace; use crate::sql::comment::shouldbespace; +use crate::sql::dir::{dir, Dir}; use crate::sql::error::IResult; use crate::sql::idiom::{idiom, Idiom}; use crate::sql::table::{table, tables, Tables}; @@ -12,29 +13,6 @@ use nom::combinator::opt; 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, "<->"), - } - } -} - #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] pub struct Graph { pub dir: Dir, @@ -46,13 +24,13 @@ pub struct Graph { impl fmt::Display for Graph { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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() { 0 => write!(f, "?"), _ => write!(f, "{}", self.what), } } else { - write!(f, "{}(", self.dir,)?; + write!(f, "{}(", self.dir)?; match self.what.len() { 0 => write!(f, "?"), _ => write!(f, "{}", self.what), diff --git a/lib/src/sql/mod.rs b/lib/src/sql/mod.rs index 277865eb..b82826ea 100644 --- a/lib/src/sql/mod.rs +++ b/lib/src/sql/mod.rs @@ -6,6 +6,7 @@ pub(crate) mod common; pub(crate) mod cond; pub(crate) mod data; pub(crate) mod datetime; +pub(crate) mod dir; pub(crate) mod duration; pub(crate) mod error; pub(crate) mod escape; @@ -61,6 +62,7 @@ pub use self::base::Base; pub use self::cond::Cond; pub use self::data::Data; pub use self::datetime::Datetime; +pub use self::dir::Dir; pub use self::duration::Duration; pub use self::error::Error; pub use self::expression::Expression; @@ -70,7 +72,6 @@ pub use self::field::Field; pub use self::field::Fields; pub use self::function::Function; pub use self::geometry::Geometry; -pub use self::graph::Dir; pub use self::graph::Graph; pub use self::group::Group; pub use self::group::Groups;