Small improvements

This commit is contained in:
jaby 2022-12-03 03:08:10 +01:00
parent 5a38e5def1
commit a931dbfe5a
3 changed files with 18 additions and 6 deletions

View File

@ -8,7 +8,6 @@
}
},
"slot_1": {
"following": "slot_0",
"blubb": {
"pattern": ["cody/*"]
}

View File

@ -1,13 +1,22 @@
use super::{OverlaySection, OverlaySlot};
use tool_helper::{Error, format_if_error, Input};
const FOLLOW_KEYWORD:&'static str = "following";
pub fn read_config(input: Input) -> Result<Vec<OverlaySlot>, Error> {
let json_reader:serde_json::Value = format_if_error!(serde_json::from_reader(input), "Parsing JSON failed with: {error_text}")?;
let mut overlay_slots = Vec::new();
let mut overlay_slots = Vec::<OverlaySlot>::new();
if let Some(objects) = json_reader.as_object() {
for object in objects {
overlay_slots.push(read_as_slot(object)?);
let mut slot = read_as_slot(object)?;
if slot.start_adr.is_none() {
if let Some(prev_slot) = overlay_slots.last() {
slot.set_start_adr_as_end_of(prev_slot.name.as_ref());
}
}
overlay_slots.push(slot);
}
Ok(overlay_slots)
@ -24,13 +33,13 @@ fn read_as_slot(json_value: (&String, &serde_json::Value)) -> Result<OverlaySlot
if let Some(json_value) = json_value.as_object() {
for (name, json_value) in json_value {
if name == "following" {
if name == FOLLOW_KEYWORD {
if let Some(start_adr) = json_value.as_str() {
slot.start_adr = Some(format!("__{}_end", start_adr));
slot.set_start_adr_as_end_of(start_adr);
}
else {
return Err(Error::from_text(format!("\"following\" option of {} must be a string", slot_name)));
return Err(Error::from_text(format!("\"{}\" option of {} must be a string", FOLLOW_KEYWORD, slot_name)));
}
}

View File

@ -37,6 +37,10 @@ impl OverlaySlot {
OverlaySlot{name, start_adr, sections: Vec::new()}
}
pub fn set_start_adr_as_end_of(&mut self, end: &str) {
self.start_adr = Some(format!("__{}_end", end));
}
pub fn add_section(&mut self, section: OverlaySection) {
self.sections.push(section);
}