From 48ffa20d4c16e33cb7bf490224fd741c0fd4949e Mon Sep 17 00:00:00 2001 From: Emily Toop Date: Thu, 15 Feb 2018 18:09:48 +0000 Subject: [PATCH] Add retract_kw --- src/entity_builder.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/entity_builder.rs b/src/entity_builder.rs index d166a54f..7f1ff23f 100644 --- a/src/entity_builder.rs +++ b/src/entity_builder.rs @@ -259,11 +259,21 @@ impl<'a, 'c> InProgressBuilder<'a, 'c> { pub fn add_kw(&mut self, e: E, a: &NamespacedKeyword, v: V) -> Result<()> where E: IntoThing>, V: IntoThing> { + let (attribute, value) = self.extract_kw_value(a, v.into_thing())?; + self.add(e, attribute, value) + } + + pub fn retract_kw(&mut self, e: E, a: &NamespacedKeyword, v: V) -> Result<()> + where E: IntoThing>, + V: IntoThing> { + let (attribute, value) = self.extract_kw_value(a, v.into_thing())?; + self.retract(e, attribute, value) + } + + fn extract_kw_value(&mut self, a: &NamespacedKeyword, v: TypedValueOr) -> Result<(KnownEntid, TypedValueOr)> { let attribute: KnownEntid; - let value: TypedValueOr; if let Some((attr, aa)) = self.in_progress.attribute_for_ident(a) { - let vv = v.into_thing(); - if let Either::Left(ref tv) = vv { + if let Either::Left(ref tv) = v { let provided = tv.value_type(); let expected = attr.value_type; if provided != expected { @@ -271,11 +281,10 @@ impl<'a, 'c> InProgressBuilder<'a, 'c> { } } attribute = aa; - value = vv; } else { bail!(ErrorKind::UnknownAttribute(a.to_string())); } - self.add(e, attribute, value) + Ok((attribute, v)) } } @@ -285,6 +294,11 @@ impl<'a, 'c> EntityBuilder> { self.builder.add_kw(self.entity.clone(), a, v) } + pub fn retract_kw(&mut self, a: &NamespacedKeyword, v: V) -> Result<()> + where V: IntoThing> { + self.builder.retract_kw(self.entity.clone(), a, v) + } + /// Build the terms from this builder and transact them against the current /// `InProgress`. This method _always_ returns the `InProgress` -- failure doesn't /// imply an automatic rollback.