diff --git a/src/loader.rs b/src/loader.rs index 750dd83..40661fc 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -13,6 +13,10 @@ pub enum LoadError { Io(io::Error), /// Indicate the file format is invalid InvalidFormat, + /// Indicate a line couldn't be parsed + LineParseError, + /// Indicate an element was improperly hierarchized + WrongHierarchy, } /// Represents a model once loaded @@ -109,6 +113,8 @@ mod tests { match modl.err().unwrap() { crate::loader::LoadError::Io(_e) => (), crate::loader::LoadError::InvalidFormat => (), + crate::loader::LoadError::LineParseError => (), + crate::loader::LoadError::WrongHierarchy => (), } } } diff --git a/src/main.rs b/src/main.rs index e366e81..b394048 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ mod data_structure; mod export; mod generator; mod loader; +mod parser; mod skills_structures; #[cfg(not(tarpaulin_include))] diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..2049131 --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,72 @@ +/// 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::{ + loader::LoadError, + skills_structures::{ConfigList, SkillList, SkillTypes}, +}; + +trait Parser { + type Err; + fn from_path(path: impl AsRef + ToString) -> Result; +} + +impl Parser for SkillList { + type Err = LoadError; + fn from_path(path: impl AsRef + ToString) -> Result { + 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 = Box::new(&mut ls); + for line in file_content.lines() { + let (index, skill) = line.split_at(1); + match index.parse::()? { + // inner.append_list(Section::new(skill, 0, 0, 0))? + 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(); + } + 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(); + } + } + 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(); + } + } + SkillTypes::SubSkill => { + if let Some(_sk) = last_skill { + // let ssk = SubSkill::new(skill, sk.get_data().childs, global_id, 0); + } + } + SkillTypes::NewLine => (), + SkillTypes::Error => return Err(LoadError::LineParseError), + } + global_id += 1; + linecount += 1; + } + Ok(ls) + } +} diff --git a/src/skills_structures.rs b/src/skills_structures.rs index dee2ff3..b33cdd3 100644 --- a/src/skills_structures.rs +++ b/src/skills_structures.rs @@ -6,23 +6,56 @@ // #![warn(dead_code)] use std::{collections::HashMap, str::FromStr}; -use crate::generator::to_skill_name; +use crate::{generator::to_skill_name, loader::LoadError}; /// Type sugarcoat of a replacement hashmap type HashReplace = HashMap; +/// A list config +pub struct ConfigList { + pub capacity: usize, + pub global_id: usize, + pub id: usize, + pub name: String, +} + +impl ConfigList { + pub(crate) fn new(name: String, id: usize, global_id: usize, capacity: usize) -> Self { + Self { + name, + id, + global_id, + capacity, + } + } +} + +// trait NewTrait { +// /// Creates a new [`ConfigList`]. +// fn new(name: String, id: usize, global_id: usize, capacity: usize) -> Self; +// } + /// The global list of skills pub struct SkillList { /// The list of all section in this skilllist - list: Vec
, + pub list: Vec
, /// The name of the list (unused) - data: DataCell, + pub data: DataCell, +} + +impl SkillList { + pub fn with_config(cfg: &ConfigList) -> Self { + Self { + list: Vec::new(), + data: DataCell::new(&cfg.name, cfg.id, cfg.global_id), + } + } } /// A section, marked by @ -struct Section { +pub struct Section { /// The list of subsection depending on this section - list: Vec, + pub list: Vec, /// The name of the cell data: DataCell, // /// The identifier number of the cell @@ -34,9 +67,9 @@ struct Section { } /// A subsection, marked by + -struct SubSection { +pub struct SubSection { /// The list of skills depending of this SubSection - list: Vec, + pub list: Vec, /// The name of the cell data: DataCell, // /// The identifier number of the cell @@ -48,9 +81,9 @@ struct SubSection { } /// A skill, marked by = -struct Skill { +pub struct Skill { /// The list of subskills depending of this skill - list: Vec, + pub list: Vec, /// The name of the cell data: DataCell, // /// The identifier number of the cell @@ -62,7 +95,7 @@ struct Skill { } /// A subskill, marked by - -struct SubSkill { +pub struct SubSkill { /// The name of the cell data: DataCell, // /// The various fields needed to transform a subskill into html @@ -70,7 +103,7 @@ struct SubSkill { } /// The data of a cell -struct DataCell { +pub struct DataCell { /// The name, untouched, as a string name: String, /// The name, stripped of any non AZaz @@ -79,6 +112,20 @@ struct DataCell { id: usize, /// The global identifier of the cell global_id: usize, + /// The number of of childs in the array + pub childs: usize, +} + +impl DataCell { + 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 @@ -93,6 +140,7 @@ impl DataCell { stripped_name, id, global_id, + childs: 0, } } @@ -112,8 +160,24 @@ 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 A skill that contains all information to be rendered by the external renderer -trait Renderable { +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; /// Create a new renderable fn new(name: &str, id: usize, global_id: usize, size_hint: usize) -> Self; /// Get the vars hashmap @@ -126,79 +190,185 @@ trait Renderable { // fn content(&self, model: &str) -> Result; // /// Render the element footer // fn footer(&self, model: &str) -> Result; - /// Get the underlying name of the skill - fn get_name(&self) -> &str; - /// Get the underlying short name - fn get_stripped_name(&self) -> &str; - /// Get the id (compared to elements of the same level) - fn get_id(&self) -> usize; - /// Get the global id (compared to every element) - fn get_global_id(&self) -> usize; + // /// Get the underlying name of the skill + // fn get_name(&self) -> &str; + // /// Get the underlying short name + // fn get_stripped_name(&self) -> &str; + // /// Get the id (compared to elements of the same level) + // fn get_id(&self) -> usize; + // /// Get the global id (compared to every element) + // fn get_global_id(&self) -> usize; + // /// Get the number of childs + // fn get_childs(&self) -> usize; + // /// Set the number of childs + // fn set_childs(&mut self, number: usize); } impl Renderable for Section { - fn get_id(&self) -> usize { - self.data.id + fn get_data(&self) -> &DataCell { + &self.data } - fn get_global_id(&self) -> usize { - self.data.global_id - } - fn get_name(&self) -> &str { - &self.data.name - } - fn get_stripped_name(&self) -> &str { - &self.data.stripped_name + fn get_mut_data(&mut self) -> &DataCell { + &self.data } + // fn get_id(&self) -> usize { + // self.data.id + // } + // fn get_global_id(&self) -> usize { + // self.data.global_id + // } + // fn get_name(&self) -> &str { + // &self.data.name + // } + // fn get_stripped_name(&self) -> &str { + // &self.data.stripped_name + // } fn get_vars(&self) -> HashReplace { self.data.to_hashmap() } - - // /// A section cannot be rendered as a whole - // fn render(&self, _model: &str) -> Result { - // Err(ErrorSkills::NonCallableMethod) - // } - - // fn header(&self, model: &str) -> Result { - // let mut vars = HashMap::new(); - // vars.insert("number".to_string(), "0"); - // vars.insert("name".to_string(), self.get_name()); - - // match self.get_id() { - // 0 => vars.insert("checked".to_string(), "checked=\"checked\""), - // _ => vars.insert("checked".to_string(), ""), - // }; - // match strfmt(model, &vars) { - // Ok(res) => Ok(res), - // Err(e) => Err(ErrorSkills::FormatError(e)), - // } - // } - - // fn content(&self, model: &str) -> Result { - // match strfmt(model, &self.get_vars()) { - // Ok(r) => Ok(r), - // Err(e) => Err(ErrorSkills::FormatError(e)), - // } - // } - - // fn footer(&self, _model: &str) -> Result { - // Err(ErrorSkills::NonCallableMethod) - // } - fn new(name: &str, id: usize, global_id: usize, size_hint: usize) -> Self { Self { list: Vec::with_capacity(size_hint), data: DataCell::new(name, id, global_id), } } + // fn get_childs(&self) -> usize { + // self.data.childs + // } + // fn set_childs(&mut self, number: usize) { + // self.data.childs = number; + // } } +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 { + 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!() + } + fn get_vars(&self) -> HashReplace { + todo!() + } +} +impl Renderable for Skill { + 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!() + } + + fn get_vars(&self) -> HashReplace { + todo!() + } +} +impl Renderable for SubSkill { + 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!() + } + + fn get_vars(&self) -> HashReplace { + todo!() + } +} +// impl Appendable for &mut Section { +// fn append_section(&mut self, element: SubSection) -> Result<(), LoadError> { +// self.list.push(element); +// Ok(()) +// } +// fn append_subsection(&mut self, _element: Skill) -> Result<(), LoadError> { +// Err(LoadError::WrongHierarchy) +// } +// fn append_skill(&mut self, _element: SubSkill) -> Result<(), LoadError> { +// Err(LoadError::WrongHierarchy) +// } +// fn append_list(&mut self, _element: Section) -> Result<(), LoadError> { +// Err(LoadError::WrongHierarchy) +// } +// } + +// impl Appendable for &mut SkillList { +// fn append_list(&mut self, element: Section) -> Result<(), LoadError> { +// self.list.push(element); +// Ok(()) +// } +// fn append_section(&mut self, _element: SubSection) -> Result<(), LoadError> { +// Err(LoadError::WrongHierarchy) +// } +// fn append_subsection(&mut self, _element: Skill) -> Result<(), LoadError> { +// Err(LoadError::WrongHierarchy) +// } +// fn append_skill(&mut self, _element: SubSkill) -> Result<(), LoadError> { +// Err(LoadError::WrongHierarchy) +// } +// } /// All errors regarding the skill handling -#[derive(Debug)] -pub enum ErrorSkills { - /// The formatting failed - FormatError(strfmt::FmtError), - /// This method can't be called in this implementation, lighter than a panic - NonCallableMethod, +// #[derive(Debug)] +// pub enum ErrorSkills { +// /// The formatting failed +// FormatError(strfmt::FmtError), +// /// This method can't be called in this implementation, lighter than a panic +// NonCallableMethod, +// } + +/// A parsable type for dispatch +pub enum SkillTypes { + Section, + SubSection, + Skill, + SubSkill, + NewLine, + Error, +} + +impl FromStr for SkillTypes { + fn from_str(s: &str) -> Result { + match s.bytes().next() { + Some(b'@') => Ok(SkillTypes::Section), + Some(b'+') => Ok(SkillTypes::SubSection), + Some(b'=') => Ok(SkillTypes::Skill), + Some(b'-') => Ok(SkillTypes::SubSkill), + None => Ok(SkillTypes::NewLine), + _ => Err(LoadError::LineParseError), + } + } + + type Err = LoadError; } #[cfg(test)] @@ -235,10 +405,10 @@ mod tests { #[test] fn test_section() { let s1 = Section::new(S1, 1, 200, 30); - assert_eq!(s1.get_name(), S1); - assert_eq!(s1.get_stripped_name(), S2); - assert_eq!(s1.get_id(), 1); - assert_eq!(s1.get_global_id(), 200); + // assert_eq!(s1.get_name(), S1); + // assert_eq!(s1.get_stripped_name(), S2); + // assert_eq!(s1.get_id(), 1); + // assert_eq!(s1.get_global_id(), 200); let mut map = HashMap::new(); map.insert("name".to_string(), S1.to_string()); map.insert("pname".to_string(), S2.to_string()); diff --git a/tarpaulin-report.html b/tarpaulin-report.html index 794f2da..a62165e 100644 --- a/tarpaulin-report.html +++ b/tarpaulin-report.html @@ -118,8 +118,8 @@