Consolidate Entity::{Add, Retract} to Entity::AddOrRetract. Fixes #255. r=nalexander (#265)

This commit is contained in:
Jordan Santell 2017-02-08 15:45:09 -08:00 committed by GitHub
parent 688a644bd9
commit 21f7bdf493
4 changed files with 33 additions and 30 deletions

View file

@ -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<Result<()>> = 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_) } => {

View file

@ -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<I>(::std::marker::PhantomData<fn(I) -> I>);
@ -114,15 +114,13 @@ impl<I> Tx<I>
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<I> Tx<I>
}
fn add() -> TxParser<Entity, I> {
fn_parser(Tx::<I>::add_, "[:db/add e a v tx?]")
fn_parser(Tx::<I>::add_, "[:db/add e a v]")
}
fn retract_(input: I) -> ParseResult<Entity, I> {
@ -152,7 +150,8 @@ impl<I> Tx<I>
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,
},
&[][..])));
}

View file

@ -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,
},
]));
}

View file

@ -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<Entid>,
},
Retract {
AddOrRetract {
op: OpType,
e: EntidOrLookupRef,
a: Entid,
v: ValueOrLookupRef,