src/section_config.rs - write: improve error message

This commit is contained in:
Dietmar Maurer 2020-01-14 12:02:25 +01:00
parent f51420ba1f
commit 4566303b05
1 changed files with 52 additions and 50 deletions

View File

@ -113,66 +113,68 @@ impl SectionConfig {
self.plugins.insert(plugin.type_name.clone(), plugin); self.plugins.insert(plugin.type_name.clone(), plugin);
} }
pub fn write(&self, _filename: &str, config: &SectionConfigData) -> Result<String, Error> { pub fn write(&self, filename: &str, config: &SectionConfigData) -> Result<String, Error> {
let mut list = VecDeque::new(); try_block!({
let mut list = VecDeque::new();
let mut done = HashSet::new(); let mut done = HashSet::new();
for section_id in &config.order { for section_id in &config.order {
if config.sections.get(section_id) == None { continue }; if config.sections.get(section_id) == None { continue };
list.push_back(section_id); list.push_back(section_id);
done.insert(section_id); done.insert(section_id);
}
for (section_id, _) in &config.sections {
if done.contains(section_id) { continue };
list.push_back(section_id);
}
let mut raw = String::new();
for section_id in list {
let (type_name, section_config) = config.sections.get(section_id).unwrap();
let plugin = self.plugins.get(type_name).unwrap();
if let Err(err) = parse_simple_value(&section_id, &self.id_schema) {
bail!("syntax error in section identifier: {}", err.to_string());
}
if section_id.chars().any(|c| c.is_control()) {
bail!("detected unexpected control character in section ID.");
} }
verify_json_object(section_config, &plugin.properties)?; for (section_id, _) in &config.sections {
if done.contains(section_id) { continue };
list.push_back(section_id);
}
let head = (self.format_section_header)(type_name, section_id, section_config); let mut raw = String::new();
if !raw.is_empty() { raw += "\n" } for section_id in list {
let (type_name, section_config) = config.sections.get(section_id).unwrap();
let plugin = self.plugins.get(type_name).unwrap();
raw += &head; if let Err(err) = parse_simple_value(&section_id, &self.id_schema) {
bail!("syntax error in section identifier: {}", err.to_string());
for (key, value) in section_config.as_object().unwrap() { }
let text = match value { if section_id.chars().any(|c| c.is_control()) {
Value::Null => { continue; }, // do nothing (delete) bail!("detected unexpected control character in section ID.");
Value::Bool(v) => v.to_string(),
Value::String(v) => v.to_string(),
Value::Number(v) => v.to_string(),
_ => {
bail!("got unsupported type in section '{}' key '{}'", section_id, key);
},
};
if text.chars().any(|c| c.is_control()) {
bail!("detected unexpected control character in section '{}' key '{}'", section_id, key);
} }
raw += "\t";
raw += &key;
raw += " ";
raw += &text;
raw += "\n";
}
}
Ok(raw) verify_json_object(section_config, &plugin.properties)?;
let head = (self.format_section_header)(type_name, section_id, section_config);
if !raw.is_empty() { raw += "\n" }
raw += &head;
for (key, value) in section_config.as_object().unwrap() {
let text = match value {
Value::Null => { continue; }, // do nothing (delete)
Value::Bool(v) => v.to_string(),
Value::String(v) => v.to_string(),
Value::Number(v) => v.to_string(),
_ => {
bail!("got unsupported type in section '{}' key '{}'", section_id, key);
},
};
if text.chars().any(|c| c.is_control()) {
bail!("detected unexpected control character in section '{}' key '{}'", section_id, key);
}
raw += "\t";
raw += &key;
raw += " ";
raw += &text;
raw += "\n";
}
}
Ok(raw)
}).map_err(|e: Error| format_err!("writing '{}' failed: {}", filename, e))
} }
pub fn parse(&self, filename: &str, raw: &str) -> Result<SectionConfigData, Error> { pub fn parse(&self, filename: &str, raw: &str) -> Result<SectionConfigData, Error> {