From cb87f31a237df42d7b0aebe263468eae776b3e0a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 12 Feb 2016 19:51:05 +0100 Subject: [PATCH] Add EntryHeader::delete() --- libimagstore/src/store.rs | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 10739768..09381b5b 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -601,6 +601,65 @@ impl EntryHeader { Ok(Some(value.unwrap().clone())) } + pub fn delete(&mut self, spec: &str) -> Result> { + let tokens = EntryHeader::tokenize(spec); + if tokens.is_err() { // return parser error if any + return Err(tokens.err().unwrap()); + } + let tokens = tokens.unwrap(); + + let destination = tokens.iter().last(); + if destination.is_none() { + return Err(StoreError::new(StoreErrorKind::HeaderPathSyntaxError, None)); + } + let destination = destination.unwrap(); + debug!("destination = {:?}", destination); + + let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens + let mut value = EntryHeader::walk_header(&mut self.header, path_to_dest); // walk N-1 tokens + if value.is_err() { + return Err(value.err().unwrap()); + } + let mut value = value.unwrap(); + debug!("walked value = {:?}", value); + + match destination { + &Token::Key(ref s) => { // if the destination shall be an map key->value + match value { + &mut Value::Table(ref mut t) => { + debug!("Matched Key->Table, removing {:?}", s); + return Ok(t.remove(s)); + }, + _ => { + debug!("Matched Key->NON-Table"); + return Err(StoreError::new(StoreErrorKind::HeaderPathTypeFailure, None)); + } + } + }, + + &Token::Index(i) => { // if the destination shall be an array + match value { + &mut Value::Array(ref mut a) => { + // if the index is inside the array, we swap-remove the element at this + // index + if a.len() > i { + debug!("Removing in Array {:?}[{:?}]", a, i); + return Ok(Some(a.remove(i))); + } else { + return Ok(None); + } + }, + _ => { + debug!("Matched Index->NON-Array"); + return Err(StoreError::new(StoreErrorKind::HeaderPathTypeFailure, None)); + }, + } + }, + } + + Ok(None) + } + fn tokenize(spec: &str) -> Result> { use std::str::FromStr;