Compare commits
11 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
5596c4564f | |
|
|
b7148e3604 | |
|
|
0ee3275fce | |
|
|
46ec258469 | |
|
|
475ad9b6b1 | |
|
|
897c8933dd | |
|
|
e4d0b26425 | |
|
|
25c1b11538 | |
|
|
ce77dfb7e9 | |
|
|
7b77e9dbe5 | |
|
|
1c7511211e |
|
|
@ -8,7 +8,9 @@ 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"] }
|
||||
strfmt = "0.2.4"
|
||||
toml = { version = "0.8.12", features = ["preserve_order"] }
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ pub enum LoadError {
|
|||
LineParseError,
|
||||
/// Indicate an element was improperly hierarchized
|
||||
WrongHierarchy,
|
||||
/// Empty line, skip it
|
||||
EmptyLine,
|
||||
}
|
||||
|
||||
/// Represents a model once loaded
|
||||
|
|
@ -115,6 +117,7 @@ mod tests {
|
|||
crate::loader::LoadError::InvalidFormat => (),
|
||||
crate::loader::LoadError::LineParseError => (),
|
||||
crate::loader::LoadError::WrongHierarchy => (),
|
||||
crate::loader::LoadError::EmptyLine => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
500
src/parser.rs
500
src/parser.rs
|
|
@ -1,10 +1,10 @@
|
|||
//! Reads and parse a skill sheet
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use crate::{
|
||||
loader::LoadError,
|
||||
skills_structures::{Section, Skill, SkillList, SkillTypes, SubSection, SubSkill},
|
||||
skills_structures::{Node, Renderable, SkillTypes},
|
||||
};
|
||||
use debug_print::debug_println;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
/// Defines a parser, able to extract a character sheet
|
||||
trait Parser<T> {
|
||||
|
|
@ -14,56 +14,24 @@ trait Parser<T> {
|
|||
fn from_path(path: impl AsRef<Path> + ToString) -> Result<T, Self::Err>;
|
||||
}
|
||||
|
||||
impl Parser<SkillList> for SkillList {
|
||||
impl Parser<Node> for Node {
|
||||
type Err = LoadError;
|
||||
fn from_path(path: impl AsRef<Path> + ToString) -> Result<SkillList, LoadError> {
|
||||
fn from_path(path: impl AsRef<Path> + ToString) -> Result<Node, LoadError> {
|
||||
let file_content = fs::read_to_string(&path).map_err(LoadError::Io)?;
|
||||
let mut global_id = 0;
|
||||
// let mut globlist = Vec::with_capacity(100);
|
||||
let mut globlist = ListId::new(100);
|
||||
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);
|
||||
let kind = index.parse::<SkillTypes>()?;
|
||||
match kind {
|
||||
SkillTypes::Section => {
|
||||
last_sect += 1;
|
||||
(last_subs, last_skill, last_sskill) = (0, 0, 0);
|
||||
globlist.add_item(kind, skill, global_id, last_sect);
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::SubSection => {
|
||||
last_subs += 1;
|
||||
(last_skill, last_sskill) = (0, 0);
|
||||
globlist.add_item(kind, skill, global_id, last_subs);
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::Skill => {
|
||||
last_skill += 1;
|
||||
last_sskill = 0;
|
||||
globlist.add_item(kind, skill, global_id, last_skill);
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::SubSkill => {
|
||||
last_sskill += 1;
|
||||
globlist.add_item(kind, skill, global_id, last_sskill);
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::NewLine => (),
|
||||
SkillTypes::Error => (),
|
||||
}
|
||||
}
|
||||
let list_sect = globlist.make_skilllist()?;
|
||||
let mut globlist = ListId::from_string(file_content)?;
|
||||
let list_sect = globlist.make_all_nodes()?;
|
||||
// Make a skillist with the given vec
|
||||
let ls = SkillList::from_vec(&path.to_string(), 0, 0, list_sect);
|
||||
// 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));
|
||||
// ls.populate(globlist);
|
||||
Ok(ls)
|
||||
Ok(list_sect)
|
||||
}
|
||||
}
|
||||
|
||||
/// The structure representing the list of LineIds
|
||||
struct ListId {
|
||||
#[derive(Debug)]
|
||||
pub struct ListId {
|
||||
/// The inner list holding all values
|
||||
list: Vec<LineId>,
|
||||
}
|
||||
|
|
@ -73,7 +41,7 @@ impl ListId {
|
|||
pub fn add_item(&mut self, kind: SkillTypes, value: &str, global_id: usize, id: usize) {
|
||||
self.list.push(LineId {
|
||||
kind,
|
||||
value: value.to_owned(),
|
||||
value: value.trim_start().to_owned(),
|
||||
global_id,
|
||||
id,
|
||||
});
|
||||
|
|
@ -87,80 +55,52 @@ impl ListId {
|
|||
}
|
||||
|
||||
/// Populate all section and return a SkillList
|
||||
fn make_skilllist(&mut self) -> Result<Vec<Section>, LoadError> {
|
||||
fn make_all_nodes(&mut self) -> Result<Node, LoadError> {
|
||||
let mut index = 0;
|
||||
let mut list_section = Vec::new();
|
||||
while index < self.list.len() {
|
||||
if matches!(self.get_kind(&index), SkillTypes::Section) {
|
||||
// Mutate index in place
|
||||
list_section.push(self.make_section(&mut index)?);
|
||||
} else {
|
||||
return Err(LoadError::WrongHierarchy);
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
Ok(list_section)
|
||||
let node = self.make_node(&mut index, SkillTypes::Root)?;
|
||||
Ok(node)
|
||||
}
|
||||
|
||||
/// Make a section starting from the given index
|
||||
fn make_section(&self, index: &mut usize) -> Result<Section, LoadError> {
|
||||
let mut list_subsection = Vec::new();
|
||||
let (name, id, global_id) = self.get_info(index);
|
||||
/// Make a node of a certain level and all subsequent subnodes
|
||||
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 level = *self.get_kind(index);
|
||||
debug_println!("[{}] => Processing {} [{}/{}]", index, name, global_id, id);
|
||||
*index += 1; // Very ugly
|
||||
match level {
|
||||
// SkillTypes::Root => self.make_node(, ),
|
||||
SkillTypes::Root | SkillTypes::Section | SkillTypes::SubSection | SkillTypes::Skill => {
|
||||
while *index < self.list.len() {
|
||||
if matches!(self.get_kind(index), SkillTypes::SubSection) {
|
||||
list_subsection.push(self.make_subsection(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) => {
|
||||
debug_println!(" <: Retreiving :\n{}", value.debug_display());
|
||||
list_childs.push(value);
|
||||
}
|
||||
Err(LoadError::EmptyLine) => (),
|
||||
_ => (),
|
||||
}
|
||||
} else {
|
||||
return Err(LoadError::WrongHierarchy);
|
||||
break;
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
Ok(Section::from_vec(
|
||||
name,
|
||||
id,
|
||||
global_id,
|
||||
Some(list_subsection),
|
||||
))
|
||||
let node = Node::new(name, id, global_id, Some(list_childs), level);
|
||||
debug_println!(" <| Exiting with:\n{}", node.debug_display());
|
||||
Ok(node)
|
||||
}
|
||||
|
||||
/// Make a section starting from the given index
|
||||
fn make_subsection(&self, index: &mut usize) -> Result<SubSection, LoadError> {
|
||||
let mut list_skills = Vec::new();
|
||||
let (name, id, global_id) = self.get_info(index);
|
||||
while *index < self.list.len() {
|
||||
if matches!(self.get_kind(index), SkillTypes::SubSection) {
|
||||
list_skills.push(self.make_skill(index)?);
|
||||
} else {
|
||||
return Err(LoadError::WrongHierarchy);
|
||||
SkillTypes::RepeatingSkills | SkillTypes::RepeatingItem | SkillTypes::SubSkill => {
|
||||
let node = Node::new(name, id, global_id, None, level);
|
||||
debug_println!(
|
||||
" <: Finished at : index {} with ({})",
|
||||
index,
|
||||
node.debug_display().trim()
|
||||
);
|
||||
Ok(node)
|
||||
}
|
||||
*index += 1;
|
||||
SkillTypes::NewLine => Err(LoadError::EmptyLine),
|
||||
}
|
||||
Ok(SubSection::from_vec(name, id, global_id, Some(list_skills)))
|
||||
}
|
||||
|
||||
/// Make a skill from the given index
|
||||
fn make_skill(&self, index: &mut usize) -> Result<Skill, LoadError> {
|
||||
let mut list_subskills = Vec::new();
|
||||
let (name, id, global_id) = self.get_info(index);
|
||||
while *index < self.list.len() {
|
||||
if matches!(self.get_kind(index), SkillTypes::SubSection) {
|
||||
list_subskills.push(self.make_subsection(index)?);
|
||||
} else {
|
||||
return Err(LoadError::WrongHierarchy);
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
Ok(Skill::from_vec(name, id, global_id, Some(list_subskills)))
|
||||
}
|
||||
|
||||
/// Make a subskill
|
||||
fn make_subskill(&self, index: &mut usize) -> Result<SubSkill, LoadError> {
|
||||
let (name, id, global_id) = self.get_info(index);
|
||||
if !matches!(self.get_kind(index), SkillTypes::SubSkill) {
|
||||
return Err(LoadError::WrongHierarchy);
|
||||
}
|
||||
*index += 1;
|
||||
let sec = SubSkill::from_vec(name, id, global_id);
|
||||
Ok(sec)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
@ -176,7 +116,7 @@ impl ListId {
|
|||
#[inline]
|
||||
/// Get the global_id of the element at the given index
|
||||
fn get_global_id(&self, index: &usize) -> usize {
|
||||
self.list[*index].id
|
||||
self.list[*index].global_id
|
||||
}
|
||||
#[inline]
|
||||
/// Get the name of the element at the given index
|
||||
|
|
@ -184,17 +124,79 @@ impl ListId {
|
|||
&self.list[*index].value
|
||||
}
|
||||
|
||||
/// Returns the value, id and global_id of the element
|
||||
/// Returns the value, global_id and id of the element
|
||||
fn get_info(&self, index: &usize) -> (&str, usize, usize) {
|
||||
(
|
||||
&self.list[*index].value,
|
||||
self.list[*index].id,
|
||||
self.list[*index].global_id,
|
||||
self.list[*index].id,
|
||||
)
|
||||
}
|
||||
/// Convert the content of a file to a list of lines
|
||||
fn from_string(file_content: String) -> Result<Self, LoadError> {
|
||||
let mut global_id = 1;
|
||||
let mut globlist = ListId::new(100);
|
||||
globlist.add_item(SkillTypes::Root, "[Root]", 0, 0);
|
||||
let (mut last_sect, mut last_subs, mut last_skill, mut last_sskill) = (0, 0, 0, 0);
|
||||
for line in file_content.lines() {
|
||||
// Can't parse an empty symbol
|
||||
if line.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
let (index, skill) = line.split_at(1);
|
||||
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);
|
||||
globlist.add_item(kind, skill, global_id, last_sect);
|
||||
last_sect += 1;
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::SubSection => {
|
||||
(last_skill, last_sskill) = (0, 0);
|
||||
globlist.add_item(kind, skill, global_id, last_subs);
|
||||
last_subs += 1;
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::Skill => {
|
||||
last_sskill = 0;
|
||||
globlist.add_item(kind, skill, global_id, last_skill);
|
||||
last_skill += 1;
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::SubSkill => {
|
||||
globlist.add_item(kind, skill, global_id, last_sskill);
|
||||
last_sskill += 1;
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::RepeatingSkills => {
|
||||
globlist.add_item(SkillTypes::RepeatingSkills, skill, global_id, 0);
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::RepeatingItem => {
|
||||
globlist.add_item(SkillTypes::RepeatingItem, skill, global_id, 0);
|
||||
global_id += 1;
|
||||
}
|
||||
SkillTypes::NewLine => (),
|
||||
// SkillTypes::Error => panic!(
|
||||
// "An unrecoverable error occured while parsing line {}.\n{}",
|
||||
// global_id, skill
|
||||
// ),
|
||||
SkillTypes::Root => (),
|
||||
}
|
||||
}
|
||||
Ok(globlist)
|
||||
}
|
||||
}
|
||||
|
||||
/// Temporary data to preparse the file
|
||||
#[derive(Debug)]
|
||||
pub struct LineId {
|
||||
/// The level of the idem (Section to subskill)
|
||||
kind: SkillTypes,
|
||||
|
|
@ -205,3 +207,279 @@ pub struct LineId {
|
|||
/// The id relative to the indent level
|
||||
id: usize,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use core::panic;
|
||||
use std::cmp::min;
|
||||
use std::fmt::Display;
|
||||
use std::iter::zip;
|
||||
|
||||
use crate::skills_structures::Node;
|
||||
use crate::skills_structures::Renderable;
|
||||
use crate::skills_structures::SkillTypes;
|
||||
|
||||
use super::LineId;
|
||||
use super::ListId;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_lineids() {
|
||||
let _line = LineId {
|
||||
kind: SkillTypes::Section,
|
||||
value: "EE".to_string(),
|
||||
global_id: 20,
|
||||
id: 2,
|
||||
};
|
||||
|
||||
let mut list = ListId::new(100);
|
||||
list.add_item(SkillTypes::Section, " EE", 20, 2);
|
||||
assert_eq!(list.get_info(&0), ("EE", 20, 2));
|
||||
assert_eq!(list.get_name(&0), "EE");
|
||||
assert_eq!(list.get_global_id(&0), 20);
|
||||
assert_eq!(list.get_id(&0), 2);
|
||||
assert_eq!(list.get_kind(&0), &SkillTypes::Section);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct BareNode {
|
||||
list: Option<Vec<BareNode>>,
|
||||
level: SkillTypes,
|
||||
}
|
||||
|
||||
impl Display for BareNode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{:0width$} {:?}",
|
||||
" ",
|
||||
self.level,
|
||||
width = min(self.level as usize, 5)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl BareNode {
|
||||
fn new(level: SkillTypes, list_lower: Option<Vec<BareNode>>) -> Self {
|
||||
Self {
|
||||
list: list_lower,
|
||||
level,
|
||||
}
|
||||
}
|
||||
|
||||
fn new_end(level: SkillTypes) -> BareNode {
|
||||
Self { list: None, level }
|
||||
}
|
||||
|
||||
fn print_self(&self) {
|
||||
println!("{}", self.to_string());
|
||||
}
|
||||
fn print_all(&self) {
|
||||
self.print_self();
|
||||
if let Some(list) = &self.list {
|
||||
for elt in list {
|
||||
elt.print_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
fn compare(&self, node: &Node) {
|
||||
assert_eq!(&self.level, node.get_level());
|
||||
if let Some(cha) = &self.list {
|
||||
if let Some(chb) = node.get_childs() {
|
||||
assert_eq!(cha.len(), chb.len());
|
||||
for (a, b) in zip(cha, chb) {
|
||||
a.compare(b)
|
||||
}
|
||||
} else {
|
||||
panic!("{} has childs and the node doesn't", self)
|
||||
}
|
||||
} else if let Some(_chb) = node.get_childs() {
|
||||
panic!("The node doesn't have childs and {} does", self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! ends {
|
||||
($($a:ident),+) => {
|
||||
vec![$(BareNode::new_end(SkillTypes::$a),)+]
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! a {
|
||||
($a:ident, $b:expr) => {
|
||||
BareNode::new(SkillTypes::$a, Some($b))
|
||||
};
|
||||
}
|
||||
macro_rules! l {
|
||||
($($b:ident)+) => {
|
||||
vec![$($b),+]
|
||||
};
|
||||
}
|
||||
#[test]
|
||||
fn test_barenodes() {
|
||||
let node = a!(Skill, ends!(NewLine, RepeatingSkills, RepeatingItem));
|
||||
let node = a!(SubSection, l!(node));
|
||||
let node = a!(Section, l!(node));
|
||||
|
||||
node.print_self();
|
||||
node.print_all();
|
||||
// assert!(false);
|
||||
}
|
||||
#[test]
|
||||
fn test_edge_cases() {
|
||||
let data = ["\n", "", "\n \n ope", " Noe"];
|
||||
for d in data.iter() {
|
||||
let line_id = ListId::from_string(d.to_string()).unwrap();
|
||||
println!("{:?}", line_id);
|
||||
assert_eq!(line_id.list.len(), 1);
|
||||
match line_id.get_kind(&0) {
|
||||
SkillTypes::Root => (),
|
||||
_ => panic!("The first section wasn't root"),
|
||||
};
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn test_fromstring() {
|
||||
let tst = [
|
||||
(
|
||||
"@ Sect",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::Section, "Sect", 1, 0),
|
||||
],
|
||||
),
|
||||
(
|
||||
"+ SubSecté",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::SubSection, "SubSecté", 1, 0),
|
||||
],
|
||||
),
|
||||
(
|
||||
"= Skill",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::Skill, "Skill", 1, 0),
|
||||
],
|
||||
),
|
||||
(
|
||||
"- SubSkill",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::SubSkill, "SubSkill", 1, 0),
|
||||
],
|
||||
),
|
||||
(
|
||||
". rpts",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::RepeatingSkills, "rpts", 1, 0),
|
||||
],
|
||||
),
|
||||
(
|
||||
"#rpitm",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::RepeatingItem, "rpitm", 1, 0),
|
||||
],
|
||||
),
|
||||
(
|
||||
"@s\n+ss\n=sk\n-sks",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::Section, "s", 1, 0),
|
||||
(SkillTypes::SubSection, "ss", 2, 0),
|
||||
(SkillTypes::Skill, "sk", 3, 0),
|
||||
(SkillTypes::SubSkill, "sks", 4, 0),
|
||||
],
|
||||
),
|
||||
(
|
||||
"@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@s1\n+ss0\n#ri0\n#ri1\n+ss1\n.rpts0\n.rpts1",
|
||||
vec![
|
||||
(SkillTypes::Root, "[Root]", 0, 0),
|
||||
(SkillTypes::Section, "s0", 1, 0),
|
||||
(SkillTypes::SubSection, "ss0", 2, 0),
|
||||
(SkillTypes::Skill, "sk0", 3, 0),
|
||||
(SkillTypes::SubSkill, "sks0", 4, 0),
|
||||
(SkillTypes::SubSkill, "sks1", 5, 1),
|
||||
(SkillTypes::SubSkill, "sks2", 6, 2),
|
||||
(SkillTypes::SubSkill, "sks3", 7, 3),
|
||||
(SkillTypes::Skill, "sk1", 8, 1),
|
||||
(SkillTypes::SubSkill, "sks0", 9, 0),
|
||||
(SkillTypes::SubSkill, "sks1", 10, 1),
|
||||
(SkillTypes::SubSkill, "sks2", 11, 2),
|
||||
(SkillTypes::SubSkill, "sks3", 12, 3),
|
||||
(SkillTypes::SubSection, "ss1", 13, 1),
|
||||
(SkillTypes::Skill, "sk0", 14, 0),
|
||||
(SkillTypes::SubSkill, "sks0", 15, 0),
|
||||
(SkillTypes::SubSkill, "sks1", 16, 1),
|
||||
(SkillTypes::SubSkill, "sks2", 17, 2),
|
||||
(SkillTypes::SubSkill, "sks3", 18, 3),
|
||||
(SkillTypes::Skill, "sk1", 19, 1),
|
||||
(SkillTypes::SubSkill, "sks0", 20, 0),
|
||||
(SkillTypes::SubSkill, "sks1", 21, 1),
|
||||
(SkillTypes::SubSkill, "sks2", 22, 2),
|
||||
(SkillTypes::SubSkill, "sks3", 23, 3),
|
||||
(SkillTypes::Section, "s1", 24, 1),
|
||||
(SkillTypes::SubSection, "ss0", 25, 0),
|
||||
(SkillTypes::RepeatingItem, "ri0", 26, 0),
|
||||
(SkillTypes::RepeatingItem, "ri1", 27, 0),
|
||||
(SkillTypes::SubSection, "ss1", 28, 1),
|
||||
(SkillTypes::RepeatingSkills, "rpts0", 29, 0),
|
||||
(SkillTypes::RepeatingSkills, "rpts1", 30, 0),
|
||||
],
|
||||
),
|
||||
];
|
||||
|
||||
for (elt, res) in tst {
|
||||
let line = ListId::from_string(elt.to_string());
|
||||
|
||||
println!("Testing list {}", elt);
|
||||
let list_id = line.unwrap();
|
||||
assert_eq!(list_id.list.len(), res.len());
|
||||
for (i, (kind, value, global_id, id)) in res.iter().enumerate() {
|
||||
println!("Testing element '{}' gid:{} id:{}", value, global_id, id);
|
||||
assert_eq!(list_id.get_kind(&i), kind);
|
||||
assert_eq!(list_id.get_name(&i), *value);
|
||||
assert_eq!(list_id.get_global_id(&i), *global_id);
|
||||
assert_eq!(list_id.get_id(&i), *id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parser() {}
|
||||
|
||||
#[test]
|
||||
fn test_node() {
|
||||
let test_dt = [
|
||||
("@s0\n\n+ss0\n=sk0\n-sks0", " [Root] [Root] [0/0]\n [Section] s0 [0/1]\n [SubSection] ss0 [0/2]\n [Skill] sk0 [0/3]\n [SubSkill] sks0 [0/4]"),
|
||||
("@s0\n \n \n \n+ss0\n=sk0\n-sks0\n-sks1\n-sks2", " [Root] [Root] [0/0]\n [Section] s0 [0/1]\n [SubSection] ss0 [0/2]\n [Skill] sk0 [0/3]\n [SubSkill] sks0 [0/4]\n [SubSkill] sks1 [1/5]\n [SubSkill] sks2 [2/6]"),
|
||||
("@s0\n\n+ss0\n=sk0\n-sks0\n-sks1\n-sks2\n=sk1\n-sks0\n-sks1\n-sks2", " [Root] [Root] [0/0]\n [Section] s0 [0/1]\n [SubSection] ss0 [0/2]\n [Skill] sk0 [0/3]\n [SubSkill] sks0 [0/4]\n [SubSkill] sks1 [1/5]\n [SubSkill] sks2 [2/6]\n [Skill] sk1 [1/7]\n [SubSkill] sks0 [0/8]\n [SubSkill] sks1 [1/9]\n [SubSkill] sks2 [2/10]"),
|
||||
("@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"," [Root] [Root] [0/0]\n [Section] s0 [0/1]\n [SubSection] ss0 [0/2]\n [Skill] sk0 [0/3]\n [SubSkill] sks0 [0/4]\n [SubSkill] sks1 [1/5]\n [SubSkill] sks2 [2/6]\n [SubSkill] sks3 [3/7]\n [Skill] sk1 [1/8]\n [SubSkill] sks0 [0/9]\n [SubSkill] sks1 [1/10]\n [SubSkill] sks2 [2/11]\n [SubSkill] sks3 [3/12]\n [SubSection] ss1 [1/13]\n [Skill] sk0 [0/14]\n [SubSkill] sks0 [0/15]\n [SubSkill] sks1 [1/16]\n [SubSkill] sks2 [2/17]\n [SubSkill] sks3 [3/18]\n [Skill] sk1 [1/19]\n [SubSkill] sks0 [0/20]\n [SubSkill] sks1 [1/21]\n [SubSkill] sks2 [2/22]\n [SubSkill] sks3 [3/23]\n [SubSection] ss2 [2/24]\n [RepeatingItem] ri0 [0/25]\n [RepeatingItem] ri1 [0/26]\n [SubSection] ss3 [3/27]\n [RepeatingSkills] rpts0 [0/28]\n [RepeatingSkills] rpts1 [0/29]\n [Section] s1 [1/30]\n [SubSection] ss1 [0/31]\n [RepeatingItem] ri0 [0/32]"),
|
||||
];
|
||||
|
||||
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::Root);
|
||||
let node = node.unwrap();
|
||||
let mut line_ids = ListId::from_string(file_content.to_string()).unwrap();
|
||||
let node2 = line_ids.make_all_nodes().unwrap();
|
||||
println!("Final Nodes: \n{}", node.debug_display());
|
||||
assert_eq!(node.debug_display(), expected.to_string());
|
||||
assert_eq!(node.debug_display(), node2.debug_display());
|
||||
}
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_listid() {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@
|
|||
//!
|
||||
//! Includes all Section/Subsection/Skill/Sub-skill and a skill-list parser
|
||||
// #![warn(dead_code)]
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
use std::fmt::Write;
|
||||
use std::{cmp::min, collections::HashMap, fmt::Display, str::FromStr};
|
||||
|
||||
use crate::{generator::to_skill_name, loader::LoadError};
|
||||
|
||||
|
|
@ -38,101 +39,46 @@ impl ConfigList {
|
|||
/// The global list of skills
|
||||
pub struct SkillList {
|
||||
/// The list of all section in this skilllist
|
||||
pub list: Vec<Section>,
|
||||
pub childs: ChildList,
|
||||
/// The name of the list (unused)
|
||||
pub data: DataCell,
|
||||
/// The type (always root)
|
||||
pub level: SkillTypes,
|
||||
}
|
||||
|
||||
impl SkillList {
|
||||
/// Create a SkillList with a config
|
||||
pub fn with_config(cfg: &ConfigList) -> Self {
|
||||
Self {
|
||||
list: Vec::new(),
|
||||
childs: None,
|
||||
data: DataCell::new(&cfg.name, cfg.id, cfg.global_id),
|
||||
level: SkillTypes::Root,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_vec(name: &str, id: usize, global_id: usize, list_sect: Vec<Section>) -> SkillList {
|
||||
/// Create a Skilllist from the given list
|
||||
pub fn from_vec(name: &str, id: usize, global_id: usize, list_sect: ChildList) -> SkillList {
|
||||
Self {
|
||||
list: list_sect,
|
||||
childs: list_sect,
|
||||
data: DataCell::new(name, id, global_id),
|
||||
level: SkillTypes::Root,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A node, with a level instead of a hierarchy
|
||||
#[derive(Debug)]
|
||||
pub struct Node {
|
||||
list: Option<Vec<Node>>,
|
||||
/// The (optional) list of subnodes
|
||||
childs: Option<Vec<Node>>,
|
||||
/// The underlying data
|
||||
data: DataCell,
|
||||
/// The level of the node
|
||||
level: SkillTypes,
|
||||
}
|
||||
|
||||
/// A section, marked by @
|
||||
pub struct Section {
|
||||
/// The list of subsection depending on this section
|
||||
pub list: Vec<SubSection>,
|
||||
/// The name of the cell
|
||||
data: DataCell,
|
||||
}
|
||||
impl Section {
|
||||
pub(crate) fn from_vec(
|
||||
name: &str,
|
||||
id: usize,
|
||||
global_id: usize,
|
||||
list_subsection: Option<Vec<SubSection>>,
|
||||
) -> Section {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// A subsection, marked by +
|
||||
pub struct SubSection {
|
||||
/// The list of skills depending of this SubSection
|
||||
pub list: Vec<Skill>,
|
||||
/// The name of the cell
|
||||
data: DataCell,
|
||||
}
|
||||
impl SubSection {
|
||||
pub(crate) fn from_vec(
|
||||
name: &str,
|
||||
id: usize,
|
||||
global_id: usize,
|
||||
list_skills: Option<Vec<Skill>>,
|
||||
) -> SubSection {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// A skill, marked by =
|
||||
pub struct Skill {
|
||||
/// The list of subskills depending of this skill
|
||||
pub list: Vec<SubSkill>,
|
||||
/// The name of the cell
|
||||
data: DataCell,
|
||||
}
|
||||
impl Skill {
|
||||
pub(crate) fn from_vec(
|
||||
name: &str,
|
||||
id: usize,
|
||||
global_id: usize,
|
||||
list_subskills: Option<Vec<SubSection>>,
|
||||
) -> Skill {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// A subskill, marked by -
|
||||
pub struct SubSkill {
|
||||
/// The name of the cell
|
||||
data: DataCell,
|
||||
}
|
||||
impl SubSkill {
|
||||
pub(crate) fn from_vec(name: &str, id: usize, global_id: usize) -> SubSkill {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// The data of a cell
|
||||
#[derive(Debug)]
|
||||
pub struct DataCell {
|
||||
/// The name, untouched, as a string
|
||||
name: String,
|
||||
|
|
@ -146,19 +92,6 @@ pub struct DataCell {
|
|||
pub childs: usize,
|
||||
}
|
||||
|
||||
impl DataCell {
|
||||
/// Create a datacell with the given string a ids
|
||||
pub fn with_string_and_id(name: &str, id: usize, global_id: usize) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
stripped_name: to_skill_name(name),
|
||||
id,
|
||||
global_id,
|
||||
childs: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Data point, holds the name and stripped name of element
|
||||
impl DataCell {
|
||||
/// Create a datacell from the given raw string
|
||||
|
|
@ -191,28 +124,23 @@ impl DataCell {
|
|||
}
|
||||
}
|
||||
|
||||
/// Can be appended to
|
||||
pub(crate) trait Appendable {
|
||||
/// Append to the main list
|
||||
fn append_list(&mut self, element: Section) -> Result<(), LoadError>;
|
||||
/// Append to a section
|
||||
fn append_section(&mut self, element: SubSection) -> Result<(), LoadError>;
|
||||
/// Append to a subsection
|
||||
fn append_subsection(&mut self, element: Skill) -> Result<(), LoadError>;
|
||||
/// Append to a skill
|
||||
fn append_skill(&mut self, element: SubSkill) -> Result<(), LoadError>;
|
||||
}
|
||||
/// A list of subnodes (or childs)
|
||||
type ChildList = Option<Vec<Node>>;
|
||||
|
||||
/// A A skill that contains all information to be rendered by the external renderer
|
||||
pub(crate) trait Renderable {
|
||||
/// Get a imutable view of the data
|
||||
fn get_data(&self) -> &DataCell;
|
||||
/// Get a mutable reference of the data
|
||||
fn get_mut_data(&mut self) -> &DataCell;
|
||||
/// Get the list of childs
|
||||
fn get_level(&self) -> &SkillTypes;
|
||||
/// Get the level of the object
|
||||
fn get_childs(&self) -> &ChildList;
|
||||
/// Create a new renderable
|
||||
fn new(name: &str, id: usize, global_id: usize, size_hint: usize) -> Self;
|
||||
fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self;
|
||||
/// Get the vars hashmap
|
||||
fn get_vars(&self) -> HashReplace;
|
||||
/// Debug display of the whole structure
|
||||
fn debug_display(&self) -> String;
|
||||
// /// Render the element and all sub elements
|
||||
// fn render(&self, model: &str) -> Result<String, ErrorSkills>;
|
||||
// /// Render the element header
|
||||
|
|
@ -235,94 +163,102 @@ pub(crate) trait Renderable {
|
|||
// fn set_childs(&mut self, number: usize);
|
||||
}
|
||||
|
||||
impl Renderable for Section {
|
||||
impl Renderable for Node {
|
||||
fn get_data(&self) -> &DataCell {
|
||||
&self.data
|
||||
}
|
||||
fn get_mut_data(&mut self) -> &DataCell {
|
||||
&self.data
|
||||
}
|
||||
fn get_vars(&self) -> HashReplace {
|
||||
self.data.to_hashmap()
|
||||
}
|
||||
fn new(name: &str, id: usize, global_id: usize, size_hint: usize) -> Self {
|
||||
fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self {
|
||||
Self {
|
||||
list: Vec::with_capacity(size_hint),
|
||||
childs,
|
||||
data: DataCell::new(name, id, global_id),
|
||||
level,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_level(&self) -> &SkillTypes {
|
||||
&self.level
|
||||
}
|
||||
|
||||
fn get_childs(&self) -> &ChildList {
|
||||
&self.childs
|
||||
}
|
||||
|
||||
fn debug_display(&self) -> String {
|
||||
let mut f = String::new();
|
||||
write!(f, "{}", self.to_string()).unwrap();
|
||||
if let Some(childs) = &self.childs {
|
||||
write!(
|
||||
f,
|
||||
"\n{}",
|
||||
childs
|
||||
.iter()
|
||||
.map(|v| v.debug_display())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
f
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for SkillList {
|
||||
fn get_data(&self) -> &DataCell {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_mut_data(&mut self) -> &DataCell {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn new(name: &str, _id: usize, _global_id: usize, _size_hint: usize) -> Self {
|
||||
let _ = name;
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_vars(&self) -> HashReplace {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for SubSection {
|
||||
fn get_data(&self) -> &DataCell {
|
||||
todo!()
|
||||
}
|
||||
fn get_mut_data(&mut self) -> &DataCell {
|
||||
todo!()
|
||||
}
|
||||
fn new(_name: &str, _id: usize, _global_id: usize, _size_hint: usize) -> Self {
|
||||
todo!()
|
||||
&self.data
|
||||
}
|
||||
fn get_vars(&self) -> HashReplace {
|
||||
todo!()
|
||||
self.data.to_hashmap()
|
||||
}
|
||||
fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self {
|
||||
Self {
|
||||
childs,
|
||||
data: DataCell::new(name, id, global_id),
|
||||
level,
|
||||
}
|
||||
impl Renderable for Skill {
|
||||
fn get_data(&self) -> &DataCell {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_mut_data(&mut self) -> &DataCell {
|
||||
todo!()
|
||||
fn get_level(&self) -> &SkillTypes {
|
||||
&self.level
|
||||
}
|
||||
|
||||
fn new(_name: &str, _id: usize, _global_id: usize, _size_hint: usize) -> Self {
|
||||
todo!()
|
||||
fn get_childs(&self) -> &ChildList {
|
||||
&self.childs
|
||||
}
|
||||
|
||||
fn get_vars(&self) -> HashReplace {
|
||||
todo!()
|
||||
fn debug_display(&self) -> String {
|
||||
let mut s = String::new();
|
||||
writeln!(s, "[{:?}]{:?}", self.level, self.data.name).unwrap();
|
||||
if let Some(childs) = &self.childs {
|
||||
for chld in childs.iter() {
|
||||
write!(s, "{}", chld.debug_display()).unwrap();
|
||||
}
|
||||
}
|
||||
impl Renderable for SubSkill {
|
||||
fn get_data(&self) -> &DataCell {
|
||||
todo!()
|
||||
s
|
||||
}
|
||||
|
||||
fn get_mut_data(&mut self) -> &DataCell {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn new(_name: &str, _id: usize, _global_id: usize, _size_hint: usize) -> Self {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_vars(&self) -> HashReplace {
|
||||
todo!()
|
||||
impl Display for Node {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{:0width$} [{level:?}] {name} [{id}/{global_id}]",
|
||||
" ",
|
||||
width = min(self.level as usize, SkillTypes::NewLine as usize),
|
||||
level = self.get_level(),
|
||||
name = self.get_data().name,
|
||||
id = self.get_data().id,
|
||||
global_id = self.get_data().global_id
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A parsable type for dispatch
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub enum SkillTypes {
|
||||
/// This is the Root of the tree (unrepresentable)
|
||||
Root,
|
||||
/// This line is a Section
|
||||
Section,
|
||||
/// This line is a SubSection
|
||||
|
|
@ -331,10 +267,14 @@ pub enum SkillTypes {
|
|||
Skill,
|
||||
/// This line is a Subskill
|
||||
SubSkill,
|
||||
/// This is a repeating section
|
||||
RepeatingSkills,
|
||||
/// This is a repeating item list
|
||||
RepeatingItem,
|
||||
/// This line is an empty new line
|
||||
NewLine,
|
||||
/// This line couldn't be parsed
|
||||
Error,
|
||||
// /// This line couldn't be parsed
|
||||
// Error,
|
||||
}
|
||||
|
||||
impl FromStr for SkillTypes {
|
||||
|
|
@ -344,6 +284,8 @@ impl FromStr for SkillTypes {
|
|||
Some(b'+') => Ok(SkillTypes::SubSection),
|
||||
Some(b'=') => Ok(SkillTypes::Skill),
|
||||
Some(b'-') => Ok(SkillTypes::SubSkill),
|
||||
Some(b'#') => Ok(SkillTypes::RepeatingItem),
|
||||
Some(b'.') => Ok(SkillTypes::RepeatingSkills),
|
||||
None => Ok(SkillTypes::NewLine),
|
||||
_ => Err(LoadError::LineParseError),
|
||||
}
|
||||
|
|
@ -351,12 +293,58 @@ impl FromStr for SkillTypes {
|
|||
|
||||
type Err = LoadError;
|
||||
}
|
||||
impl SkillTypes {
|
||||
/// Returns true if the child node is a children of parent node
|
||||
pub(crate) fn is_child(&self, child_node: SkillTypes) -> bool {
|
||||
match (self, child_node) {
|
||||
(SkillTypes::Root, SkillTypes::Root) => false,
|
||||
(SkillTypes::Root, _) => true,
|
||||
(SkillTypes::Section, SkillTypes::Root | SkillTypes::Section) => false,
|
||||
(SkillTypes::Section, _) => true,
|
||||
(
|
||||
SkillTypes::SubSection,
|
||||
SkillTypes::Root | SkillTypes::Section | SkillTypes::SubSection,
|
||||
) => false,
|
||||
(SkillTypes::SubSection, _) => true,
|
||||
(
|
||||
SkillTypes::Skill,
|
||||
SkillTypes::Root | SkillTypes::Section | SkillTypes::SubSection | SkillTypes::Skill,
|
||||
) => false,
|
||||
(SkillTypes::Skill, _) => true,
|
||||
(SkillTypes::SubSkill, _) => false,
|
||||
(SkillTypes::RepeatingSkills, _) => false,
|
||||
(SkillTypes::RepeatingItem, _) => false,
|
||||
(SkillTypes::NewLine, _) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the SkillType of lower level
|
||||
pub(crate) fn get_lower_level(&self) -> Option<SkillTypes> {
|
||||
match self {
|
||||
SkillTypes::Root => Some(SkillTypes::Section),
|
||||
SkillTypes::Section => Some(SkillTypes::SubSection),
|
||||
SkillTypes::SubSection => Some(SkillTypes::Skill),
|
||||
SkillTypes::Skill => Some(SkillTypes::SubSkill),
|
||||
SkillTypes::SubSkill => None,
|
||||
SkillTypes::RepeatingSkills => None,
|
||||
SkillTypes::RepeatingItem => None,
|
||||
SkillTypes::NewLine => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use core::panic;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::{DataCell, Renderable, Section};
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use crate::{
|
||||
loader::LoadError,
|
||||
skills_structures::{Node, SkillTypes},
|
||||
};
|
||||
|
||||
use super::{ConfigList, DataCell, Renderable, SkillList};
|
||||
|
||||
static S1: &str =
|
||||
"This is a test sentence with some non-ASCII characters like café and résumé.";
|
||||
|
|
@ -385,7 +373,7 @@ mod tests {
|
|||
}
|
||||
#[test]
|
||||
fn test_section() {
|
||||
let s1 = Section::new(S1, 1, 200, 30);
|
||||
let s1 = Node::new(S1, 1, 200, None, SkillTypes::Section);
|
||||
// assert_eq!(s1.get_name(), S1);
|
||||
// assert_eq!(s1.get_stripped_name(), S2);
|
||||
// assert_eq!(s1.get_id(), 1);
|
||||
|
|
@ -398,4 +386,156 @@ mod tests {
|
|||
map.insert("selected".to_string(), "checked=\"checked\"".to_string());
|
||||
assert_eq!(s1.get_vars(), map);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config() {
|
||||
let name = "Johnéé".to_string();
|
||||
let id = 12;
|
||||
let global_id = 33;
|
||||
let capacity = 44;
|
||||
let cfg = ConfigList::new(name.clone(), id, global_id, capacity);
|
||||
let ckll = SkillList::with_config(&cfg);
|
||||
assert_eq!(ckll.get_data().name, name);
|
||||
assert_eq!(ckll.get_data().global_id, global_id);
|
||||
assert_eq!(ckll.get_data().id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skilllist() {
|
||||
let name = "Jognéé".to_string();
|
||||
let id = 20;
|
||||
let global_id = 30;
|
||||
let list = Vec::new();
|
||||
let skll = SkillList::from_vec(&name, id, global_id, Some(list));
|
||||
assert!(skll.childs.is_some());
|
||||
let vars = skll.get_vars();
|
||||
assert_eq!(vars["name"], name);
|
||||
assert_eq!(vars["id"], id.to_string());
|
||||
assert_eq!(vars["pname"], "jogn".to_string());
|
||||
assert_eq!(vars["selected"], "".to_string());
|
||||
assert_eq!(vars["global_id"], global_id.to_string());
|
||||
|
||||
let skill2 = SkillList::new(&name, id, global_id, None, SkillTypes::Root);
|
||||
assert_eq!(skill2.get_vars(), skll.get_vars());
|
||||
assert_eq!(skll.get_level(), &SkillTypes::Root);
|
||||
assert!(skll.get_childs().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_node() {
|
||||
let name = "Eeeé".to_string();
|
||||
let (id, global_id) = (10, 20);
|
||||
let skt = SkillTypes::Section;
|
||||
let childs = None;
|
||||
let n = Node::new(&name, id, global_id, childs, skt);
|
||||
let dat = n.get_data();
|
||||
assert_eq!(dat.name, name);
|
||||
assert_eq!(dat.id, id);
|
||||
assert_eq!(n.get_level(), &skt);
|
||||
assert!(n.get_childs().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_display() {
|
||||
let n1 = Node::new("Name0", 0, 2, None, SkillTypes::SubSection);
|
||||
let n2 = Node::new("Name1", 0, 1, Some(vec![n1]), SkillTypes::Section);
|
||||
assert_eq!(n2.to_string(), " [Section] Name1 [0/1]".to_owned());
|
||||
assert_eq!(
|
||||
n2.debug_display(),
|
||||
" [Section] Name1 [0/1]\n [SubSection] Name0 [0/2]".to_owned()
|
||||
);
|
||||
let sk = SkillList::new("Biglist", 0, 0, Some(vec![n2]), SkillTypes::Root);
|
||||
assert_eq!(sk.get_childs().as_ref().unwrap().len(), 1);
|
||||
assert_eq!(
|
||||
sk.debug_display(),
|
||||
"[Root]\"Biglist\"\n [Section] Name1 [0/1]\n [SubSection] Name0 [0/2]".to_owned()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fromstr() {
|
||||
let vals = [
|
||||
("+ ra", Ok(SkillTypes::SubSection)),
|
||||
("@ azé", Ok(SkillTypes::Section)),
|
||||
("=", Ok(SkillTypes::Skill)),
|
||||
("-é", Ok(SkillTypes::SubSkill)),
|
||||
("", Ok(SkillTypes::NewLine)),
|
||||
("f", Err(LoadError::LineParseError)),
|
||||
(".", Ok(SkillTypes::RepeatingSkills)),
|
||||
("#raE", Ok(SkillTypes::RepeatingItem)),
|
||||
];
|
||||
for v in vals.iter() {
|
||||
let res = v.0.parse::<SkillTypes>();
|
||||
|
||||
match (&res, &v.1) {
|
||||
(Ok(a), Ok(b)) => assert_eq!(a, b),
|
||||
(Err(_a), Err(_b)) => (),
|
||||
_ => panic!("{} value generated an error", v.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_child() {
|
||||
macro_rules! add_val {
|
||||
($a:ident,$b:ident,$c:ident) => {
|
||||
(SkillTypes::$a, SkillTypes::$b, $c)
|
||||
};
|
||||
}
|
||||
|
||||
let test_list = [
|
||||
add_val!(Root, Root, false),
|
||||
add_val!(Section, Root, false),
|
||||
add_val!(Section, Section, false),
|
||||
add_val!(Section, Skill, true),
|
||||
add_val!(SubSection, Section, false),
|
||||
add_val!(SubSection, SubSection, false),
|
||||
add_val!(SubSection, Skill, true),
|
||||
add_val!(Skill, SubSection, false),
|
||||
add_val!(Skill, Skill, false),
|
||||
add_val!(Skill, SubSkill, true),
|
||||
add_val!(RepeatingSkills, SubSkill, false),
|
||||
add_val!(RepeatingItem, Section, false),
|
||||
add_val!(SubSkill, Section, false),
|
||||
add_val!(NewLine, Section, false),
|
||||
];
|
||||
for (a, b, res) in test_list.iter() {
|
||||
assert!(
|
||||
a.is_child(*b) == *res,
|
||||
"{:?}.is_child({:?})=={}, found {}",
|
||||
a,
|
||||
b,
|
||||
res,
|
||||
!res
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_lower() {
|
||||
macro_rules! add {
|
||||
($a:ident,$b:ident) => {
|
||||
(SkillTypes::$a, SkillTypes::$b)
|
||||
};
|
||||
}
|
||||
|
||||
let testl = [
|
||||
add!(Root, Section),
|
||||
add!(Section, SubSection),
|
||||
add!(SubSection, Skill),
|
||||
add!(Skill, SubSkill),
|
||||
];
|
||||
let nope = [
|
||||
SkillTypes::SubSkill,
|
||||
SkillTypes::RepeatingSkills,
|
||||
SkillTypes::RepeatingItem,
|
||||
SkillTypes::NewLine,
|
||||
];
|
||||
for elt in nope.iter() {
|
||||
assert!(elt.get_lower_level().is_none());
|
||||
}
|
||||
for (elt, value) in testl.iter() {
|
||||
assert_eq!(elt.get_lower_level().unwrap(), *value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Reference in New Issue