Adds the basics of parsing

This commit is contained in:
Dandaedre 2024-04-07 02:30:55 +02:00
parent e6a7da8c0f
commit b0bea0d953
3 changed files with 47 additions and 38 deletions

View File

@ -1,9 +1,4 @@
/// Reads and parse a skill sheet
use crate::skills_structures::Renderable;
use crate::skills_structures::Section;
use crate::skills_structures::Skill;
use crate::skills_structures::SubSection;
use crate::skills_structures::SubSkill;
use std::{fs, path::Path};
use crate::{
@ -19,54 +14,67 @@ trait Parser<T> {
impl Parser<SkillList> for SkillList {
type Err = LoadError;
fn from_path(path: impl AsRef<Path> + ToString) -> Result<SkillList, LoadError> {
let mut ls = SkillList::with_config(&ConfigList::new(path.to_string(), 0, 0, 50));
let file_content = fs::read_to_string(&path).map_err(LoadError::Io)?;
let mut linecount = 1;
let mut global_id = 0;
let mut globlist = Vec::with_capacity(100);
struct line_id {
kind: SkillTypes,
value: String,
}
// let (mut last_sect, mut last_subs, mut last_skill) = (None, None, None);
// let mut inner: Box<dyn Appendable> = Box::new(&mut ls);
let (mut last_sect, mut last_subs, mut last_skill, mut last_sskill) = (0, 0, 0, 0);
for line in file_content.lines() {
let (index, skill) = line.split_at(1);
match index.parse::<SkillTypes>()? {
// inner.append_list(Section::new(skill, 0, 0, 0))?
let kind = index.parse::<SkillTypes>()?;
match kind {
SkillTypes::Section => {
let mut s = Section::new(skill, ls.get_data().childs, global_id, 0);
ls.get_mut_data().childs += 1;
ls.list.push(&mut s);
last_sect = ls.list.last();
last_sect += 1;
(last_subs, last_skill, last_sskill) = (0, 0, 0);
globlist.push(LineId {
kind,
value: skill.to_owned(),
global_id,
id: last_sect,
});
}
SkillTypes::SubSection => {
if let Some(sec) = last_sect {
let mut ss = SubSection::new(skill, sec.get_data().childs, global_id, 0);
sec.get_mut_data().childs + 1;
sec.list.push(&mut ss);
last_subs = sec.list.last();
}
last_subs += 1;
(last_skill, last_sskill) = (0, 0);
globlist.push(LineId {
kind,
value: skill.to_owned(),
global_id,
id: last_subs,
});
}
SkillTypes::Skill => {
if let Some(ssc) = last_subs {
let mut sk = Skill::new(skill, ssc.get_data().childs, global_id, 0);
ssc.get_mut_data().childs += 1;
ssc.list.push(&mut sk);
last_skill = ssc.list.last();
}
last_skill += 1;
last_sskill = 0;
globlist.push(LineId {
kind,
value: skill.to_owned(),
global_id,
id: last_skill,
});
}
SkillTypes::SubSkill => {
if let Some(_sk) = last_skill {
// let ssk = SubSkill::new(skill, sk.get_data().childs, global_id, 0);
}
last_sskill += 1;
globlist.push(LineId {
kind,
value: skill.to_owned(),
global_id,
id: last_sskill,
});
}
SkillTypes::NewLine => (),
SkillTypes::Error => return Err(LoadError::LineParseError),
SkillTypes::Error => (),
}
global_id += 1;
linecount += 1;
}
let ls = SkillList::with_config(&ConfigList::new(path.to_string(), 0, 0, global_id));
todo!();
Ok(ls)
}
}
struct LineId {
kind: SkillTypes,
value: String,
global_id: usize,
id: usize,
}

View File

@ -250,6 +250,7 @@ impl Renderable for SkillList {
}
fn new(name: &str, _id: usize, _global_id: usize, _size_hint: usize) -> Self {
let _ = name;
todo!()
}

File diff suppressed because one or more lines are too long