Fixes parser and updates coverage

This commit is contained in:
Dandaedre 2024-04-13 05:06:14 +02:00
parent 46ec258469
commit 0ee3275fce
3 changed files with 69 additions and 44 deletions

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
criterion = "0.5.1"
debug_print = "1.0.0"
divan = "0.1.14"
pretty_assertions = "1.4.0"
serde = { version = "1.0.197", features = ["derive"] }

View File

@ -1,10 +1,10 @@
//! Reads and parse a skill sheet
use std::{fs, path::Path};
use crate::{
loader::LoadError,
skills_structures::{Node, Renderable, SkillList, SkillTypes},
};
use debug_print::debug_println;
use std::{fs, path::Path};
/// Defines a parser, able to extract a character sheet
trait Parser<T> {
@ -20,7 +20,7 @@ impl Parser<SkillList> for SkillList {
let file_content = fs::read_to_string(&path).map_err(LoadError::Io)?;
// let mut globlist = Vec::with_capacity(100);
let mut globlist = ListId::from_string(file_content)?;
let list_sect = globlist.make_skilllist()?;
let list_sect = globlist.make_list_sections()?;
// Make a skillist with the given vec
let ls = SkillList::from_vec(&path.to_string(), 0, 0, Some(list_sect));
// let ls = SkillList::with_config(&ConfigList::new(path.to_string(), 0, 0, global_id));
@ -55,70 +55,64 @@ impl ListId {
}
/// Populate all section and return a SkillList
fn make_skilllist(&mut self) -> Result<Vec<Node>, LoadError> {
fn make_list_sections(&mut self) -> Result<Vec<Node>, LoadError> {
let mut index = 0;
let mut list_section = Vec::new();
while index < self.list.len() {
let (name, global_id, id) = self.get_info(&index);
debug_println!(
"[{}]|| => Processing {} [{}/{}]",
index,
name,
global_id,
id
);
if matches!(self.get_kind(&index), SkillTypes::Section) {
// Mutate index in place
// list_section.push(self.make_section(&mut index)?);
list_section.push(self.make_node(&mut index, SkillTypes::Section)?);
} else {
return Err(LoadError::WrongHierarchy);
// return Err(LoadError::WrongHierarchy);
index += 1;
}
index += 1;
}
Ok(list_section)
}
/// Make a node of a certain level and all subsequent subnodes
fn make_node(&self, index: &mut usize, level: SkillTypes) -> Result<Node, LoadError> {
fn make_node(&self, index: &mut usize, _level: SkillTypes) -> Result<Node, LoadError> {
let mut list_childs = Vec::new();
let (name, global_id, id) = self.get_info(index);
// let lower_level = level.get_lower_level();
// println!(
// "=> Processing {} [{}/{}] => Lower({:?})",
// name, global_id, id, lower_level
// );
if *self.get_kind(index) != level {
return Err(LoadError::WrongHierarchy);
}
// println!(" > Entering at : index {}", index);
let level = *self.get_kind(index);
debug_println!("[{}] => Processing {} [{}/{}]", index, name, global_id, id);
*index += 1; // Very ugly
match level {
SkillTypes::Root => unimplemented!(),
SkillTypes::Section | SkillTypes::SubSection | SkillTypes::Skill => {
// let lower_level = &lower_level.unwrap();
// SkillTypes::Root => self.make_node(, ),
SkillTypes::Root | SkillTypes::Section | SkillTypes::SubSection | SkillTypes::Skill => {
while *index < self.list.len() {
// println!(" - Iterating at : index {}", index);
debug_println!(" - Iterating at : index {}", index);
if level.is_child(*self.get_kind(index)) {
match self.make_node(index, *self.get_kind(index)) {
Ok(value) => {
// println!(" < Retreiving :\n{}", value.debug_display());
debug_println!(" <: Retreiving :\n{}", value.debug_display());
list_childs.push(value);
}
// This is not good behavious
Err(LoadError::EmptyLine) => (),
// Err(LoadError::LineParseError) => (),
_ => (),
// _ => return Err(LoadError::LineParseError),
}
} else {
// println!(" Not child");
break;
}
}
let node = Node::new(name, id, global_id, Some(list_childs), level);
// println!("x= Exiting with:\n{}", node.debug_display());
debug_println!(" <| Exiting with:\n{}", node.debug_display());
Ok(node)
}
SkillTypes::RepeatingSkills | SkillTypes::RepeatingItem | SkillTypes::SubSkill => {
let node = Node::new(name, id, global_id, None, level);
// println!(
// "<= Finished at : index {} with {}",
// index,
// node.debug_display()
// );
debug_println!(
" <: Finished at : index {} with ({})",
index,
node.debug_display().trim()
);
Ok(node)
}
SkillTypes::NewLine => Err(LoadError::EmptyLine),
@ -165,7 +159,13 @@ impl ListId {
continue;
}
let (index, skill) = line.split_at(1);
let kind = index.parse::<SkillTypes>()?;
let kind = match index.parse::<SkillTypes>() {
Ok(k) => k,
Err(_) => continue,
// Err(LoadError::LineParseError) => continue,
// Err(LoadError::EmptyLine) => continue,
// Err(e) => return Err(e),
};
match kind {
SkillTypes::Section => {
(last_subs, last_skill, last_sskill) = (0, 0, 0);
@ -343,11 +343,15 @@ mod test {
#[test]
fn test_edge_cases() {
let line_id = ListId::from_string("\n".to_string()).unwrap();
println!("{:?}", line_id);
assert!(line_id.list.is_empty());
let line_id = ListId::from_string("".to_string()).unwrap();
println!("{:?}", line_id);
assert!(line_id.list.is_empty());
let line_id = ListId::from_string("nope".to_string());
assert!(line_id.is_err());
let line_id = ListId::from_string("\n \nnope".to_string());
println!("{:?}", line_id);
// assert!(line_id.is_err());
assert!(line_id.unwrap().list.is_empty());
}
#[test]
fn test_fromstring() {
@ -430,31 +434,51 @@ mod test {
fn test_parser() {}
#[test]
fn test_skilllist() {}
fn test_skilllist() {
let test_dt = [("@s0\n+ss0\n=sk0\n-sks0\n-sks1\n-sks2\n-sks3\n\
=sk1\n-sks0\n-sks1\n-sks2\n-sks3\n+ss1\n=sk0\n\
-sks0\n-sks1\n-sks2\n-sks3\n=sk1\n-sks0\n-sks1\n\
-sks2\n-sks3\n+ss2\n#ri0\n#ri1\n+ss3\n.rpts0\n.rpts1\n\
@s1\n+ss1\n#ri0"," [Section] s0 [0/0]\n [SubSection] ss0 [0/1]\n [Skill] sk0 [0/2]\n [SubSkill] sks0 [0/3]\n [SubSkill] sks1 [1/4]\n [SubSkill] sks2 [2/5]\n [SubSkill] sks3 [3/6]\n [Skill] sk1 [1/7]\n [SubSkill] sks0 [0/8]\n [SubSkill] sks1 [1/9]\n [SubSkill] sks2 [2/10]\n [SubSkill] sks3 [3/11]\n [SubSection] ss1 [1/12]\n [Skill] sk0 [0/13]\n [SubSkill] sks0 [0/14]\n [SubSkill] sks1 [1/15]\n [SubSkill] sks2 [2/16]\n [SubSkill] sks3 [3/17]\n [Skill] sk1 [1/18]\n [SubSkill] sks0 [0/19]\n [SubSkill] sks1 [1/20]\n [SubSkill] sks2 [2/21]\n [SubSkill] sks3 [3/22]\n [SubSection] ss2 [2/23]\n [RepeatingItem] ri0 [0/24]\n [RepeatingItem] ri1 [0/25]\n [SubSection] ss3 [3/26]\n [RepeatingSkills] rpts0 [0/27]\n [RepeatingSkills] rpts1 [0/28]\n [Section] s1 [1/29]\n [SubSection] ss1 [0/30]\n [RepeatingItem] ri0 [0/31]"),
];
for (data, res) in test_dt.iter() {
let mut line_id = ListId::from_string(data.to_string()).unwrap();
let node = line_id.make_list_sections();
let node = node
.unwrap()
.iter()
.map(|v| v.debug_display())
.collect::<Vec<_>>()
.join("\n");
assert_eq!(node, res.to_string());
// println!("{}", node);
}
}
#[test]
fn test_node() {
let test_dt = [
("@s0\n\n+ss0\n=sk0\n-sks0", " [Section] s0 [0/0]\n [SubSection] ss0 [0/1]\n [Skill] sk0 [0/2]\n [SubSkill] sks0 [0/3]"),
("@s0\n\n+ss0\n=sk0\n-sks0\n-sks1\n-sks2", " [Section] s0 [0/0]\n [SubSection] ss0 [0/1]\n [Skill] sk0 [0/2]\n [SubSkill] sks0 [0/3]\n [SubSkill] sks1 [1/4]\n [SubSkill] sks2 [2/5]"),
("@s0\n \n \n \n+ss0\n=sk0\n-sks0\n-sks1\n-sks2", " [Section] s0 [0/0]\n [SubSection] ss0 [0/1]\n [Skill] sk0 [0/2]\n [SubSkill] sks0 [0/3]\n [SubSkill] sks1 [1/4]\n [SubSkill] sks2 [2/5]"),
("@s0\n\n+ss0\n=sk0\n-sks0\n-sks1\n-sks2\n=sk1\n-sks0\n-sks1\n-sks2", " [Section] s0 [0/0]\n [SubSection] ss0 [0/1]\n [Skill] sk0 [0/2]\n [SubSkill] sks0 [0/3]\n [SubSkill] sks1 [1/4]\n [SubSkill] sks2 [2/5]\n [Skill] sk1 [1/6]\n [SubSkill] sks0 [0/7]\n [SubSkill] sks1 [1/8]\n [SubSkill] sks2 [2/9]"),
("@s0\n+ss0\n=sk0\n-sks0\n-sks1\n-sks2\n-sks3\n\
=sk1\n-sks0\n-sks1\n-sks2\n-sks3\n+ss1\n=sk0\n\
-sks0\n-sks1\n-sks2\n-sks3\n=sk1\n-sks0\n-sks1\n\
-sks2\n-sks3\n+ss2\n#ri0\n#ri1\n+ss3\n.rpts0\n.rpts1"," [Section] s0 [0/0]\n [SubSection] ss0 [0/1]\n [Skill] sk0 [0/2]\n [SubSkill] sks0 [0/3]\n [SubSkill] sks1 [1/4]\n [SubSkill] sks2 [2/5]\n [SubSkill] sks3 [3/6]\n [Skill] sk1 [1/7]\n [SubSkill] sks0 [0/8]\n [SubSkill] sks1 [1/9]\n [SubSkill] sks2 [2/10]\n [SubSkill] sks3 [3/11]\n [SubSection] ss1 [1/12]\n [Skill] sk0 [0/13]\n [SubSkill] sks0 [0/14]\n [SubSkill] sks1 [1/15]\n [SubSkill] sks2 [2/16]\n [SubSkill] sks3 [3/17]\n [Skill] sk1 [1/18]\n [SubSkill] sks0 [0/19]\n [SubSkill] sks1 [1/20]\n [SubSkill] sks2 [2/21]\n [SubSkill] sks3 [3/22]\n [SubSection] ss2 [2/23]\n [RepeatingItem] ri0 [0/24]\n [RepeatingItem] ri1 [0/25]\n [SubSection] ss3 [3/26]\n [RepeatingSkills] rpts0 [0/27]\n [RepeatingSkills] rpts1 [0/28]"),
-sks2\n-sks3\n+ss2\n#ri0\n#ri1\n+ss3\n.rpts0\n.rpts1\n\
@s1\n+ss1\n#ri0"," [Section] s0 [0/0]\n [SubSection] ss0 [0/1]\n [Skill] sk0 [0/2]\n [SubSkill] sks0 [0/3]\n [SubSkill] sks1 [1/4]\n [SubSkill] sks2 [2/5]\n [SubSkill] sks3 [3/6]\n [Skill] sk1 [1/7]\n [SubSkill] sks0 [0/8]\n [SubSkill] sks1 [1/9]\n [SubSkill] sks2 [2/10]\n [SubSkill] sks3 [3/11]\n [SubSection] ss1 [1/12]\n [Skill] sk0 [0/13]\n [SubSkill] sks0 [0/14]\n [SubSkill] sks1 [1/15]\n [SubSkill] sks2 [2/16]\n [SubSkill] sks3 [3/17]\n [Skill] sk1 [1/18]\n [SubSkill] sks0 [0/19]\n [SubSkill] sks1 [1/20]\n [SubSkill] sks2 [2/21]\n [SubSkill] sks3 [3/22]\n [SubSection] ss2 [2/23]\n [RepeatingItem] ri0 [0/24]\n [RepeatingItem] ri1 [0/25]\n [SubSection] ss3 [3/26]\n [RepeatingSkills] rpts0 [0/27]\n [RepeatingSkills] rpts1 [0/28]"),
];
for (file_content, expected) in test_dt.iter() {
println!("::<> Testing for:\n {}", file_content);
let line_ids = ListId::from_string(file_content.to_string()).unwrap();
let mut index = 0;
let node = line_ids.make_node(&mut index, SkillTypes::Section);
let node = line_ids.make_node(&mut index, SkillTypes::Root);
let node = node.unwrap();
println!("Final Nodes: \n{}", node.debug_display());
assert_eq!(node.debug_display(), expected.to_string());
}
}
#[test]
fn test_listid() {}
// #[test]
// fn test_listid() {}
}

File diff suppressed because one or more lines are too long