section_config: further parser improvements

This commit is contained in:
Dietmar Maurer 2018-11-26 18:22:07 +01:00
parent 0d97734e92
commit b943ed8d2b
1 changed files with 13 additions and 11 deletions

View File

@ -16,9 +16,9 @@ pub struct SectionConfig {
parse_section_header: fn(&str) -> Option<(String, String)>, parse_section_header: fn(&str) -> Option<(String, String)>,
} }
enum ParseState { enum ParseState<'a> {
BeforeHeader, BeforeHeader,
InsideSection, InsideSection(&'a SectionConfigPlugin),
} }
impl SectionConfig { impl SectionConfig {
@ -34,9 +34,6 @@ impl SectionConfig {
let mut line_no = 0; let mut line_no = 0;
//let error_prefix = format!("file '{}' line {}", filename, line_no);
const ERROR_FORMAT: &str = "file '{}' line {} - {}";
let mut state = ParseState::BeforeHeader; let mut state = ParseState::BeforeHeader;
for line in raw.lines() { for line in raw.lines() {
@ -52,12 +49,17 @@ impl SectionConfig {
if let Some((section_type, section_id)) = (self.parse_section_header)(line) { if let Some((section_type, section_id)) = (self.parse_section_header)(line) {
println!("OKLINE: type: {} ID: {}", section_type, section_id); println!("OKLINE: type: {} ID: {}", section_type, section_id);
state = ParseState::InsideSection; if let Some(ref plugin) = self.plugins.get(&section_type) {
state = ParseState::InsideSection(plugin);
} else { } else {
println!("file '{}' line {} - {}", filename, line_no, "syntax error - expected header"); bail!("file '{}' line {} - unknown section type '{}'",
filename, line_no, section_type);
}
} else {
bail!("file '{}' line {} - syntax error (expected header)", filename, line_no);
} }
} }
ParseState::InsideSection => { ParseState::InsideSection(plugin) => {
if line.trim().is_empty() { if line.trim().is_empty() {
// finish section // finish section
@ -69,7 +71,7 @@ impl SectionConfig {
} }
} }
if let ParseState::InsideSection = state { if let ParseState::InsideSection(plugin) = state {
// finish section // finish section
} }
@ -133,7 +135,7 @@ lvmthin: local-lvm
content rootdir,images content rootdir,images
"; ";
config.parse(filename, &raw); let res = config.parse(filename, &raw);
println!("RES: {:?}", res);
} }