From 6173cff76297dd6764643c4bc94ffc22ed14fbb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sat, 6 Feb 2016 19:26:00 +0100 Subject: [PATCH] Verify that nothing but tables exist in base table --- libimagstore/src/error.rs | 4 ++++ libimagstore/src/store.rs | 31 ++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 28e0174d..bcf42c38 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -130,6 +130,8 @@ pub enum ParserErrorKind { TOMLParserErrors, MissingMainSection, MissingVersionInfo, + NonTableInBaseTable, + HeaderInconsistency, } pub struct ParserError { @@ -173,6 +175,8 @@ impl Error for ParserError { ParserErrorKind::TOMLParserErrors => "Several TOML-Parser-Errors", ParserErrorKind::MissingMainSection => "Missing 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", } } diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index fae9094a..85c50ca0 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -327,13 +327,7 @@ impl EntryHeader { } pub fn verify(&self) -> Result<()> { - if !has_main_section(&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(()) - } + verify_header(&self.toml) } } @@ -352,17 +346,32 @@ fn build_default_header() -> BTreeMap { 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 { - if !has_main_section(&t) { - Err(ParserError::new(ParserErrorKind::MissingMainSection, None)) - } else if !has_imag_version_in_main_section(&t) { - Err(ParserError::new(ParserErrorKind::MissingVersionInfo, None)) + use std::error::Error; + if let Err(e) = verify_header(&t) { + Err(ParserError::new(ParserErrorKind::HeaderInconsistency, None)) } else { Ok(t) } } +fn has_only_tables(t: &Table) -> bool { + t.iter().all(|(_, x)| if let &Value::Table(_) = x { true } else { false }) +} + fn has_main_section(t: &Table) -> bool { t.contains_key("imag") && match t.get("imag") {