Merge pull request #186 from TheNeikos/verify-toml_table

Verify that nothing but tables exist in base table
This commit is contained in:
Matthias Beyer 2016-02-07 01:54:35 +01:00
commit a0afc9245b
2 changed files with 29 additions and 12 deletions

View file

@ -132,6 +132,8 @@ pub enum ParserErrorKind {
TOMLParserErrors, TOMLParserErrors,
MissingMainSection, MissingMainSection,
MissingVersionInfo, MissingVersionInfo,
NonTableInBaseTable,
HeaderInconsistency,
} }
pub struct ParserError { pub struct ParserError {
@ -175,6 +177,8 @@ impl Error for ParserError {
ParserErrorKind::TOMLParserErrors => "Several TOML-Parser-Errors", ParserErrorKind::TOMLParserErrors => "Several TOML-Parser-Errors",
ParserErrorKind::MissingMainSection => "Missing main section", ParserErrorKind::MissingMainSection => "Missing main section",
ParserErrorKind::MissingVersionInfo => "Missing version information in main section", ParserErrorKind::MissingVersionInfo => "Missing version information in main section",
ParserErrorKind::NonTableInBaseTable => "A non-table was found in the base table",
ParserErrorKind::HeaderInconsistency => "The header is inconsistent",
} }
} }

View file

@ -194,8 +194,10 @@ impl Store {
assert!(se.is_borrowed(), "Tried to update a non borrowed entry."); assert!(se.is_borrowed(), "Tried to update a non borrowed entry.");
debug!("Verifying Entry");
try!(entry.entry.verify()); try!(entry.entry.verify());
debug!("Writing Entry");
try!(se.write_entry(&entry.entry)); try!(se.write_entry(&entry.entry));
se.status = StoreEntryStatus::Present; se.status = StoreEntryStatus::Present;
@ -286,8 +288,9 @@ impl<'a> ::std::ops::DerefMut for FileLockEntry<'a> {
} }
impl<'a> Drop for FileLockEntry<'a> { impl<'a> Drop for FileLockEntry<'a> {
/// This will silently ignore errors, use `Store::update` if you want to catch the errors
fn drop(&mut self) { fn drop(&mut self) {
self.store._update(self).unwrap() let _ = self.store._update(self);
} }
} }
@ -348,13 +351,7 @@ impl EntryHeader {
} }
pub fn verify(&self) -> Result<()> { pub fn verify(&self) -> Result<()> {
if !has_main_section(&self.toml) { verify_header(&self.toml)
Err(StoreError::from(ParserError::new(ParserErrorKind::MissingMainSection, None)))
} else if !has_imag_version_in_main_section(&self.toml) {
Err(StoreError::from(ParserError::new(ParserErrorKind::MissingVersionInfo, None)))
} else {
Ok(())
}
} }
} }
@ -373,17 +370,33 @@ fn build_default_header() -> BTreeMap<String, Value> {
m m
} }
fn verify_header(t: &Table) -> Result<()> {
if !has_main_section(t) {
Err(StoreError::from(ParserError::new(ParserErrorKind::MissingMainSection, None)))
} else if !has_imag_version_in_main_section(t) {
Err(StoreError::from(ParserError::new(ParserErrorKind::MissingVersionInfo, None)))
} else if !has_only_tables(t) {
debug!("Could not verify that it only has tables in its base table");
Err(StoreError::from(ParserError::new(ParserErrorKind::NonTableInBaseTable, None)))
} else {
Ok(())
}
}
fn verify_header_consistency(t: Table) -> EntryResult<Table> { fn verify_header_consistency(t: Table) -> EntryResult<Table> {
if !has_main_section(&t) { use std::error::Error;
Err(ParserError::new(ParserErrorKind::MissingMainSection, None)) if let Err(e) = verify_header(&t) {
} else if !has_imag_version_in_main_section(&t) { Err(ParserError::new(ParserErrorKind::HeaderInconsistency, None))
Err(ParserError::new(ParserErrorKind::MissingVersionInfo, None))
} else { } else {
Ok(t) Ok(t)
} }
} }
fn has_only_tables(t: &Table) -> bool {
debug!("Verifying that table has only tables");
t.iter().all(|(_, x)| if let &Value::Table(_) = x { true } else { false })
}
fn has_main_section(t: &Table) -> bool { fn has_main_section(t: &Table) -> bool {
t.contains_key("imag") && t.contains_key("imag") &&
match t.get("imag") { match t.get("imag") {