section_config: parse section content
This commit is contained in:
parent
803b95a01d
commit
1fa897cfed
@ -25,6 +25,7 @@ pub struct SectionConfig {
|
|||||||
plugins: HashMap<String, SectionConfigPlugin>,
|
plugins: HashMap<String, SectionConfigPlugin>,
|
||||||
|
|
||||||
parse_section_header: fn(&str) -> Option<(String, String)>,
|
parse_section_header: fn(&str) -> Option<(String, String)>,
|
||||||
|
parse_section_content: fn(&str) -> Option<(String, String)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ParseState<'a> {
|
enum ParseState<'a> {
|
||||||
@ -38,6 +39,7 @@ impl SectionConfig {
|
|||||||
Self {
|
Self {
|
||||||
plugins: HashMap::new(),
|
plugins: HashMap::new(),
|
||||||
parse_section_header: SectionConfig::default_parse_section_header,
|
parse_section_header: SectionConfig::default_parse_section_header,
|
||||||
|
parse_section_content: SectionConfig::default_parse_section_content,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,6 +84,11 @@ impl SectionConfig {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
println!("CONTENT: {}", line);
|
println!("CONTENT: {}", line);
|
||||||
|
if let Some((key, value)) = (self.parse_section_content)(line) {
|
||||||
|
println!("CONTENT: key: {} value: {}", key, value);
|
||||||
|
} else {
|
||||||
|
bail!("file '{}' line {} - syntax error (expected section properties)", filename, line_no);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,6 +100,30 @@ impl SectionConfig {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn default_parse_section_content(line: &str) -> Option<(String, String)> {
|
||||||
|
|
||||||
|
if line.is_empty() { return None; }
|
||||||
|
let first_char = line.chars().next().unwrap();
|
||||||
|
|
||||||
|
if !first_char.is_whitespace() { return None }
|
||||||
|
|
||||||
|
let mut kv_iter = line.trim_left().splitn(2, |c: char| c.is_whitespace());
|
||||||
|
|
||||||
|
let key = match kv_iter.next() {
|
||||||
|
Some(v) => v.trim(),
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
if key.len() == 0 { return None; }
|
||||||
|
|
||||||
|
let value = match kv_iter.next() {
|
||||||
|
Some(v) => v.trim(),
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
Some((key.into(), value.into()))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn default_parse_section_header(line: &str) -> Option<(String, String)> {
|
pub fn default_parse_section_header(line: &str) -> Option<(String, String)> {
|
||||||
|
|
||||||
if line.is_empty() { return None; };
|
if line.is_empty() { return None; };
|
||||||
@ -104,24 +135,19 @@ impl SectionConfig {
|
|||||||
let mut head_iter = line.splitn(2, ':');
|
let mut head_iter = line.splitn(2, ':');
|
||||||
|
|
||||||
let section_type = match head_iter.next() {
|
let section_type = match head_iter.next() {
|
||||||
Some(v) => v,
|
Some(v) => v.trim(),
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let section_type = section_type.trim();
|
|
||||||
|
|
||||||
if section_type.len() == 0 { return None; }
|
if section_type.len() == 0 { return None; }
|
||||||
|
|
||||||
// fixme: verify format
|
// fixme: verify format
|
||||||
|
|
||||||
let section_id = match head_iter.next() {
|
let section_id = match head_iter.next() {
|
||||||
Some(v) => v,
|
Some(v) => v.trim(),
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let section_id = section_id.trim();
|
|
||||||
|
|
||||||
|
|
||||||
Some((section_type.into(), section_id.into()))
|
Some((section_type.into(), section_id.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user