diff --git a/src/loader.rs b/src/loader.rs index 40661fc..2a78a83 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -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 => (), } } } diff --git a/src/parser.rs b/src/parser.rs index de2052b..049c40d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -60,7 +60,8 @@ impl ListId { 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)?); + // list_section.push(self.make_section(&mut index)?); + list_section.push(self.make_node(&mut index, SkillTypes::Section)?); } else { return Err(LoadError::WrongHierarchy); } @@ -69,78 +70,37 @@ impl ListId { Ok(list_section) } - /// Make a section starting from the given index - fn make_section(&self, index: &mut usize) -> Result { - let mut list_subsection = Vec::new(); + /// Make a node of a certain level and all subsequent subnodes + fn make_node(&self, index: &mut usize, level: SkillTypes) -> Result { + let mut list_childs = Vec::new(); let (name, global_id, id) = self.get_info(index); - while *index < self.list.len() { - if matches!(self.get_kind(index), SkillTypes::SubSection) { - list_subsection.push(self.make_subsection(index)?); - } else { - return Err(LoadError::WrongHierarchy); - } - *index += 1; - } - Ok(Node::new( - name, - id, - global_id, - Some(list_subsection), - SkillTypes::Section, - )) - } - - /// Make a section starting from the given index - fn make_subsection(&self, index: &mut usize) -> Result { - let mut list_skills = Vec::new(); - let (name, global_id, 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); - } - *index += 1; - } - Ok(Node::new( - name, - id, - global_id, - Some(list_skills), - SkillTypes::SubSection, - )) - } - - /// Make a skill from the given index - fn make_skill(&self, index: &mut usize) -> Result { - let mut list_subskills = Vec::new(); - let (name, global_id, 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(Node::new( - name, - id, - global_id, - Some(list_subskills), - SkillTypes::Skill, - )) - } - - /// Make a subskill - fn make_subskill(&self, index: &mut usize) -> Result { - let (name, global_id, id) = self.get_info(index); - if !matches!(self.get_kind(index), SkillTypes::SubSkill) { + let lower_level = level.get_lower_level(); + if *self.get_kind(index) != level { return Err(LoadError::WrongHierarchy); } - *index += 1; - let sec = Node::new(name, id, global_id, None, SkillTypes::SubSkill); - Ok(sec) + match level { + SkillTypes::Root => unimplemented!(), + SkillTypes::Section | SkillTypes::SubSection | SkillTypes::Skill => { + let lower_level = &lower_level.unwrap(); + while *index < self.list.len() { + if level.is_child(*self.get_kind(index)) { + match self.make_node(index, *lower_level) { + Ok(value) => { + list_childs.push(value); + } + Err(LoadError::EmptyLine) => (), + _ => return Err(LoadError::LineParseError), + } + *index += 1; + } + } + Ok(Node::new(name, id, global_id, Some(list_childs), level)) + } + SkillTypes::RepeatingSkills | SkillTypes::RepeatingItem | SkillTypes::SubSkill => { + Ok(Node::new(name, id, global_id, None, level)) + } + SkillTypes::NewLine => Err(LoadError::EmptyLine), + } } #[inline] diff --git a/src/skills_structures.rs b/src/skills_structures.rs index 018744f..cacd8e8 100644 --- a/src/skills_structures.rs +++ b/src/skills_structures.rs @@ -197,7 +197,7 @@ impl Renderable for SkillList { } /// A parsable type for dispatch -#[derive(PartialEq, Debug)] +#[derive(Copy, Clone, PartialEq, Debug)] pub enum SkillTypes { /// This is the Root of the tree (unrepresentable) Root, @@ -235,7 +235,45 @@ 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 { + 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; @@ -355,4 +393,68 @@ mod tests { } } } + + #[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); + } + } } diff --git a/tarpaulin-report.html b/tarpaulin-report.html index 880121f..79d6946 100644 --- a/tarpaulin-report.html +++ b/tarpaulin-report.html @@ -118,8 +118,8 @@