From 21f7bdf493fc1be5bee9a28307bdc9e9b82f9560 Mon Sep 17 00:00:00 2001 From: Jordan Santell Date: Wed, 8 Feb 2017 15:45:09 -0800 Subject: [PATCH] Consolidate Entity::{Add, Retract} to Entity::AddOrRetract. Fixes #255. r=nalexander (#265) --- db/src/db.rs | 11 ++++++----- tx-parser/src/lib.rs | 28 ++++++++++++++-------------- tx-parser/tests/parser.rs | 9 +++++---- tx/src/entities.rs | 15 ++++++++------- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/db/src/db.rs b/db/src/db.rs index 299bc3e5..c5156e66 100644 --- a/db/src/db.rs +++ b/db/src/db.rs @@ -24,7 +24,7 @@ use edn::types::Value; use entids; use errors::*; use mentat_tx::entities as entmod; -use mentat_tx::entities::Entity; +use mentat_tx::entities::{Entity, OpType}; use types::*; pub fn new_connection() -> rusqlite::Connection { @@ -710,11 +710,11 @@ impl DB { // underlying entities, in which case this expression is more natural than for loops. let r: Vec> = entities.into_iter().map(|entity: &Entity| -> Result<()> { match *entity { - Entity::Add { + Entity::AddOrRetract { + op: OpType::Add, e: entmod::EntidOrLookupRef::Entid(ref e_), a: ref a_, - v: entmod::ValueOrLookupRef::Value(ref v_), - tx: _ } => { + v: entmod::ValueOrLookupRef::Value(ref v_)} => { let e: i64 = match e_ { &entmod::Entid::Entid(ref e__) => *e__, @@ -745,7 +745,8 @@ impl DB { Ok(()) }, - Entity::Retract { + Entity::AddOrRetract { + op: OpType::Retract, e: entmod::EntidOrLookupRef::Entid(ref e_), a: ref a_, v: entmod::ValueOrLookupRef::Value(ref v_) } => { diff --git a/tx-parser/src/lib.rs b/tx-parser/src/lib.rs index 87290c08..0de570a5 100644 --- a/tx-parser/src/lib.rs +++ b/tx-parser/src/lib.rs @@ -14,11 +14,11 @@ extern crate edn; extern crate combine; extern crate mentat_tx; -use combine::{any, eof, many, optional, parser, satisfy_map, token, Parser, ParseResult, Stream}; +use combine::{any, eof, many, parser, satisfy_map, token, Parser, ParseResult, Stream}; use combine::combinator::{Expected, FnParser}; use edn::symbols::NamespacedKeyword; use edn::types::Value; -use mentat_tx::entities::*; +use mentat_tx::entities::{Entid, EntidOrLookupRef, Entity, LookupRef, OpType, ValueOrLookupRef}; pub struct Tx(::std::marker::PhantomData I>); @@ -114,15 +114,13 @@ impl Tx Tx::<&[Value]>::entid(), // TODO: handle lookup-ref. any(), - // TODO: entid or special keyword :db/tx? - optional(Tx::<&[Value]>::entid()), eof()) - .map(|(_, e, a, v, tx, _)| { - Entity::Add { + .map(|(_, e, a, v, _)| { + Entity::AddOrRetract { + op: OpType::Add, e: e, a: a, v: ValueOrLookupRef::Value(v), - tx: tx, } }); // TODO: use ok() with a type annotation rather than explicit match. @@ -138,7 +136,7 @@ impl Tx } fn add() -> TxParser { - fn_parser(Tx::::add_, "[:db/add e a v tx?]") + fn_parser(Tx::::add_, "[:db/add e a v]") } fn retract_(input: I) -> ParseResult { @@ -152,7 +150,8 @@ impl Tx any(), eof()) .map(|(_, e, a, v, _)| { - Entity::Retract { + Entity::AddOrRetract { + op: OpType::Retract, e: e, a: a, v: ValueOrLookupRef::Value(v), @@ -235,12 +234,12 @@ mod tests { let mut parser = Tx::entity(); let result = parser.parse(&input[..]); assert_eq!(result, - Ok((Entity::Add { + Ok((Entity::AddOrRetract { + op: OpType::Add, e: EntidOrLookupRef::Entid(Entid::Ident(NamespacedKeyword::new("test", "entid"))), a: Entid::Ident(NamespacedKeyword::new("test", "a")), v: ValueOrLookupRef::Value(Value::Text("v".into())), - tx: None, }, &[][..]))); } @@ -254,7 +253,8 @@ mod tests { let mut parser = Tx::entity(); let result = parser.parse(&input[..]); assert_eq!(result, - Ok((Entity::Retract { + Ok((Entity::AddOrRetract { + op: OpType::Retract, e: EntidOrLookupRef::Entid(Entid::Entid(101)), a: Entid::Ident(NamespacedKeyword::new("test", "a")), v: ValueOrLookupRef::Value(Value::Text("v".into())), @@ -272,14 +272,14 @@ mod tests { let mut parser = Tx::entity(); let result = parser.parse(&input[..]); assert_eq!(result, - Ok((Entity::Add { + Ok((Entity::AddOrRetract { + op: OpType::Add, e: EntidOrLookupRef::LookupRef(LookupRef { a: Entid::Ident(NamespacedKeyword::new("test", "a1")), v: Value::Text("v1".into()), }), a: Entid::Ident(NamespacedKeyword::new("test", "a")), v: ValueOrLookupRef::Value(Value::Text("v".into())), - tx: None, }, &[][..]))); } diff --git a/tx-parser/tests/parser.rs b/tx-parser/tests/parser.rs index 77424560..66d29e43 100644 --- a/tx-parser/tests/parser.rs +++ b/tx-parser/tests/parser.rs @@ -16,7 +16,7 @@ extern crate mentat_tx_parser; use edn::parse; use edn::symbols::NamespacedKeyword; use edn::types::Value; -use mentat_tx::entities::*; +use mentat_tx::entities::{Entid, EntidOrLookupRef, Entity, OpType, ValueOrLookupRef}; use mentat_tx_parser::Tx; #[test] @@ -32,16 +32,17 @@ fn test_entities() { let result = Tx::parse(&input[..]); assert_eq!(result, Ok(vec![ - Entity::Add { + Entity::AddOrRetract { e: EntidOrLookupRef::Entid(Entid::Entid(101)), a: Entid::Ident(NamespacedKeyword::new("test", "a")), v: ValueOrLookupRef::Value(Value::Text("v".into())), - tx: None, + op: OpType::Add, }, - Entity::Retract { + Entity::AddOrRetract { e: EntidOrLookupRef::Entid(Entid::Entid(102)), a: Entid::Ident(NamespacedKeyword::new("test", "b")), v: ValueOrLookupRef::Value(Value::Text("w".into())), + op: OpType::Retract, }, ])); } diff --git a/tx/src/entities.rs b/tx/src/entities.rs index 2eb411c2..c714cd3a 100644 --- a/tx/src/entities.rs +++ b/tx/src/entities.rs @@ -40,15 +40,16 @@ pub enum ValueOrLookupRef { LookupRef(LookupRef), } +#[derive(Clone, Debug, PartialEq)] +pub enum OpType { + Add, + Retract, +} + #[derive(Clone, Debug, PartialEq)] pub enum Entity { - Add { - e: EntidOrLookupRef, - a: Entid, - v: ValueOrLookupRef, - tx: Option, - }, - Retract { + AddOrRetract { + op: OpType, e: EntidOrLookupRef, a: Entid, v: ValueOrLookupRef,