Alter: EntryHeader should contain a Value, not the BTreeMap<>

This commit is contained in:
Matthias Beyer 2016-02-11 15:21:26 +01:00
parent 74a9b844ca
commit b4362f5c65
2 changed files with 16 additions and 24 deletions

View file

@ -27,6 +27,7 @@ pub enum StoreErrorKind {
HeaderPathSyntaxError, HeaderPathSyntaxError,
HeaderPathTypeFailure, HeaderPathTypeFailure,
HeaderKeyNotFound, HeaderKeyNotFound,
HeaderTypeFailure,
// maybe more // maybe more
} }
@ -50,6 +51,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
&StoreErrorKind::HeaderPathSyntaxError => "Syntax error in accessor string", &StoreErrorKind::HeaderPathSyntaxError => "Syntax error in accessor string",
&StoreErrorKind::HeaderPathTypeFailure => "Header has wrong type for path", &StoreErrorKind::HeaderPathTypeFailure => "Header has wrong type for path",
&StoreErrorKind::HeaderKeyNotFound => "Header Key not found", &StoreErrorKind::HeaderKeyNotFound => "Header Key not found",
&StoreErrorKind::HeaderTypeFailure => "Header type is wrong",
} }
} }

View file

@ -315,7 +315,7 @@ pub type EntryContent = String;
*/ */
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EntryHeader { pub struct EntryHeader {
toml: Table, header: Value,
} }
pub type EntryResult<V> = RResult<V, ParserError>; pub type EntryResult<V> = RResult<V, ParserError>;
@ -333,27 +333,16 @@ impl EntryHeader {
pub fn new() -> EntryHeader { pub fn new() -> EntryHeader {
EntryHeader { EntryHeader {
toml: build_default_header() header: build_default_header()
} }
} }
fn from_table(t: Table) -> EntryHeader { fn from_table(t: Table) -> EntryHeader {
EntryHeader { EntryHeader {
toml: t header: Value::Table(t)
} }
} }
/**
* Get the table which lives in the background
*/
pub fn toml(&self) -> &Table {
&self.toml
}
pub fn toml_mut(&mut self) -> &mut Table {
&mut self.toml
}
pub fn parse(s: &str) -> EntryResult<EntryHeader> { pub fn parse(s: &str) -> EntryResult<EntryHeader> {
use toml::Parser; use toml::Parser;
@ -365,7 +354,10 @@ impl EntryHeader {
} }
pub fn verify(&self) -> Result<()> { pub fn verify(&self) -> Result<()> {
verify_header(&self.toml) match &self.header {
&Value::Table(ref t) => verify_header(&t),
_ => Err(StoreError::new(StoreErrorKind::HeaderTypeFailure, None)),
}
} }
/** /**
@ -407,8 +399,7 @@ impl EntryHeader {
let destination = destination.unwrap(); let destination = destination.unwrap();
let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens
let mut table = Value::Table(self.toml.clone()); // oh fuck, but yes, we clone() here let mut value = EntryHeader::walk_header(&mut self.header, path_to_dest); // walk N-1 tokens
let mut value = EntryHeader::walk_header(&mut table, path_to_dest); // walk N-1 tokens
if value.is_err() { if value.is_err() {
return value.map(|_| false); return value.map(|_| false);
} }
@ -502,8 +493,7 @@ impl EntryHeader {
debug!("destination = {:?}", destination); debug!("destination = {:?}", destination);
let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens
let mut table = Value::Table(self.toml.clone()); // oh fuck, but yes, we clone() here let mut value = EntryHeader::walk_header(&mut self.header, path_to_dest); // walk N-1 tokens
let mut value = EntryHeader::walk_header(&mut table, path_to_dest); // walk N-1 tokens
if value.is_err() { if value.is_err() {
return Err(value.err().unwrap()); return Err(value.err().unwrap());
} }
@ -597,8 +587,8 @@ impl EntryHeader {
} }
let tokens = tokens.unwrap(); let tokens = tokens.unwrap();
let mut table = Value::Table(self.toml.clone()); // oh fuck, but yes, we clone() here let mut header_clone = self.header.clone(); // we clone as READing is simpler this way
let mut value = EntryHeader::walk_header(&mut table, tokens); // walk N-1 tokens let mut value = EntryHeader::walk_header(&mut header_clone, tokens); // walk N-1 tokens
if value.is_err() { if value.is_err() {
let e = value.err().unwrap(); let e = value.err().unwrap();
match e.err_type() { match e.err_type() {
@ -672,7 +662,7 @@ impl EntryHeader {
} }
fn build_default_header() -> BTreeMap<String, Value> { fn build_default_header() -> Value { // BTreeMap<String, Value>
let mut m = BTreeMap::new(); let mut m = BTreeMap::new();
m.insert(String::from("imag"), { m.insert(String::from("imag"), {
@ -684,7 +674,7 @@ fn build_default_header() -> BTreeMap<String, Value> {
Value::Table(imag_map) Value::Table(imag_map)
}); });
m Value::Table(m)
} }
fn verify_header(t: &Table) -> Result<()> { fn verify_header(t: &Table) -> Result<()> {
if !has_main_section(t) { if !has_main_section(t) {
@ -808,7 +798,7 @@ impl Entry {
pub fn to_str(&self) -> String { pub fn to_str(&self) -> String {
format!("---{header}---\n{content}", format!("---{header}---\n{content}",
header = ::toml::encode_str(&self.header.toml), header = ::toml::encode_str(&self.header.header),
content = self.content) content = self.content)
} }