Simplify: Move header verification from Value extension to Entry type

This commit is contained in:
Matthias Beyer 2018-06-08 01:59:19 +02:00
parent 9ad1c8d6bd
commit f6a7345b4a
2 changed files with 12 additions and 55 deletions

View File

@ -23,7 +23,6 @@ use libimagrt::runtime::Runtime;
use libimagutil::warn_exit::warn_exit; use libimagutil::warn_exit::warn_exit;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagstore::store::Header;
/// Verify the store. /// Verify the store.
/// ///
@ -41,13 +40,13 @@ pub fn verify(rt: &Runtime) {
.all(|fle| { .all(|fle| {
let p = fle.get_location(); let p = fle.get_location();
let content_len = fle.get_content().len(); let content_len = fle.get_content().len();
let (header, status) = if fle.get_header().verify().is_ok() { let (verify, status) = if fle.verify().is_ok() {
("ok", true) ("ok", true)
} else { } else {
("broken", false) ("broken", false)
}; };
info!("{: >6} | {: >14} | {:?}", header, content_len, p.deref()); info!("{: >6} | {: >14} | {:?}", verify, content_len, p.deref());
status status
}); });

View File

@ -864,7 +864,16 @@ impl Entry {
/// ///
/// Currently, this only verifies the header. This might change in the future. /// Currently, this only verifies the header. This might change in the future.
pub fn verify(&self) -> Result<()> { pub fn verify(&self) -> Result<()> {
self.header.verify() if !has_main_section(&self.header)? {
Err(SE::from_kind(SEK::MissingMainSection))
} else if !has_imag_version_in_main_section(&self.header)? {
Err(SE::from_kind(SEK::MissingVersionInfo))
} else if !has_only_tables(&self.header)? {
debug!("Could not verify that it only has tables in its base table");
Err(SE::from_kind(SEK::NonTableInBaseTable))
} else {
Ok(())
}
} }
} }
@ -882,15 +891,11 @@ impl PartialEq for Entry {
/// Extension trait for top-level toml::Value::Table, will only yield correct results on the /// Extension trait for top-level toml::Value::Table, will only yield correct results on the
/// top-level Value::Table, but not on intermediate tables. /// top-level Value::Table, but not on intermediate tables.
pub trait Header { pub trait Header {
fn verify(&self) -> Result<()>;
fn parse(s: &str) -> Result<Value>; fn parse(s: &str) -> Result<Value>;
} }
impl Header for Value { impl Header for Value {
fn verify(&self) -> Result<()> {
}
fn parse(s: &str) -> Result<Value> { fn parse(s: &str) -> Result<Value> {
use toml::de::from_str; use toml::de::from_str;
@ -929,7 +934,6 @@ mod test {
use std::collections::BTreeMap; use std::collections::BTreeMap;
use storeid::StoreId; use storeid::StoreId;
use store::Header;
use store::has_main_section; use store::has_main_section;
use store::has_imag_version_in_main_section; use store::has_imag_version_in_main_section;
@ -979,52 +983,6 @@ mod test {
assert!(has_imag_version_in_main_section(&Value::Table(map)).is_err()); assert!(has_imag_version_in_main_section(&Value::Table(map)).is_err());
} }
#[test]
fn test_verification_good() {
let mut header = BTreeMap::new();
let sub = {
let mut sub = BTreeMap::new();
sub.insert("version".into(), Value::String(String::from("0.0.0")));
Value::Table(sub)
};
header.insert("imag".into(), sub);
assert!(Value::Table(header).verify().is_ok());
}
#[test]
fn test_verification_invalid_versionstring() {
let mut header = BTreeMap::new();
let sub = {
let mut sub = BTreeMap::new();
sub.insert("version".into(), Value::String(String::from("000")));
Value::Table(sub)
};
header.insert("imag".into(), sub);
assert!(!Value::Table(header).verify().is_ok());
}
#[test]
fn test_verification_current_version() {
let mut header = BTreeMap::new();
let sub = {
let mut sub = BTreeMap::new();
sub.insert("version".into(), Value::String(String::from(env!("CARGO_PKG_VERSION"))));
Value::Table(sub)
};
header.insert("imag".into(), sub);
assert!(Value::Table(header).verify().is_ok());
}
static TEST_ENTRY : &'static str = "--- static TEST_ENTRY : &'static str = "---
[imag] [imag]
version = '0.0.3' version = '0.0.3'