Read sub sections

This commit is contained in:
Jaby 2023-07-03 22:17:44 +02:00
parent 394dbb1e85
commit 278ee844a0
2 changed files with 50 additions and 21 deletions

View File

@ -2,7 +2,7 @@ pub mod types;
use tool_helper::{Error, Input};
use std::io::BufRead;
use types::{Section};
use types::{Content, Section};
pub fn scan(input: Input) -> Result<Vec<Section>, Error> {
process(input.lines().map(|l| l.unwrap()).into_iter())?;
@ -32,11 +32,16 @@ fn process_section<F: std::iter::Iterator<Item=String>>(line: String, line_iter:
};
let (mut section, line) = read_section(line, &mut next_line_closure)?;
println!("Section: {} @{:?} {:?}", section.name, section.adr, section.size);
println!("Section: {} @{:?} {:?} \"{:?}\"", section.name, section.adr, section.size, section.file);
process_subsection(&mut section, line, &mut next_line_closure)
}
fn process_subsection<F:FnMut()-> Result<String, Error>>(_section: &mut Section, line: Option<String>, next_line: &mut F) -> Result<String, Error> {
fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section, line: Option<String>, next_line: &mut F) -> Result<String, Error> {
fn push_sub_section(section: &mut Section, sub_section: Option<Section>) {
if let Some(sub_section) = sub_section {
section.content.push(Content::Section(sub_section));
}
}
let mut line = {
if let Some(line) = line {
line
@ -47,25 +52,39 @@ fn process_subsection<F:FnMut()-> Result<String, Error>>(_section: &mut Section,
}
};
loop {
if line.starts_with(" .") {
println!(">>> SubSection: {}", line);
}
else if line.is_empty() {
let mut next_line_closure = || -> Result<String, Error> {
if let Ok(line) = next_line() {
return Ok(line);
}
line = {
if let Ok(line) = next_line() {
line
}
else {
// EOF
return Ok(String::from(""));
}
};
else {
// EOF
return Ok(String::from(""));
let mut sub_section = None;
loop {
if line.starts_with(" .") {
push_sub_section(section, sub_section);
let (section, new_line) = read_section(line, &mut next_line_closure)?;
println!("\t>>>>> {:?} {:?} {:?} {:?}", section.name, section.adr, section.size, section.file);
sub_section = Some(section);
if let Some(new_line) = new_line {
line = new_line;
continue;
}
};
}
else if line.is_empty() {
push_sub_section(section, sub_section);
return Ok(line);
}
line = next_line_closure()?;
}
}
@ -81,7 +100,7 @@ fn read_section<F:FnMut()-> Result<String, Error>>(mut line: String, next_line:
let adr = read_as::<u64>(split_line.next());
if adr.is_none() {
return Ok((Section::new(name, None, None), {
return Ok((Section::new(name, None, None, None), {
if need_new_line {
Some(line)
}
@ -93,7 +112,16 @@ fn read_section<F:FnMut()-> Result<String, Error>>(mut line: String, next_line:
}
let size = read_as::<usize>(split_line.next());
Ok((Section::new(name, adr, size), None))
let file = {
if let Some(file) = split_line.next() {
Some(file.to_string())
}
else {
None
}
};
Ok((Section::new(name, adr, size, file), None))
}
fn read_as<F: num_traits::Num>(str: Option<&str>) -> Option<F> {

View File

@ -5,6 +5,7 @@ pub struct Section {
pub name: String,
pub adr: Option<u64>,
pub size: Option<usize>,
pub file: Option<String>,
pub content: Vec<Content>
}
@ -27,8 +28,8 @@ pub enum Content {
}
impl Section {
pub fn new(name: String, adr: Option<u64>, size: Option<usize>) -> Section {
Section{name, adr, size, content: Vec::new()}
pub fn new(name: String, adr: Option<u64>, size: Option<usize>, file: Option<String>) -> Section {
Section{name, adr, size, file, content: Vec::new()}
}
}