Adds parsing (untested)

This commit is contained in:
Dandaedre 2024-04-08 17:58:34 +02:00
parent b0bea0d953
commit 2af2398389
3 changed files with 215 additions and 90 deletions

View File

@ -1,13 +1,16 @@
/// Reads and parse a skill sheet
//! Reads and parse a skill sheet
use std::{fs, path::Path};
use crate::{
loader::LoadError,
skills_structures::{ConfigList, SkillList, SkillTypes},
skills_structures::{Renderable, Section, Skill, SkillList, SkillTypes, SubSection, SubSkill},
};
/// Defines a parser, able to extract a character sheet
trait Parser<T> {
/// The Error type of the parser
type Err;
/// Generate a character from a path
fn from_path(path: impl AsRef<Path> + ToString) -> Result<T, Self::Err>;
}
@ -16,7 +19,8 @@ impl Parser<SkillList> for SkillList {
fn from_path(path: impl AsRef<Path> + ToString) -> Result<SkillList, 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 = 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);
@ -25,56 +29,179 @@ impl Parser<SkillList> for SkillList {
SkillTypes::Section => {
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,
});
globlist.add_item(kind, skill, global_id, last_sect);
global_id += 1;
}
SkillTypes::SubSection => {
last_subs += 1;
(last_skill, last_sskill) = (0, 0);
globlist.push(LineId {
kind,
value: skill.to_owned(),
global_id,
id: last_subs,
});
globlist.add_item(kind, skill, global_id, last_subs);
global_id += 1;
}
SkillTypes::Skill => {
last_skill += 1;
last_sskill = 0;
globlist.push(LineId {
kind,
value: skill.to_owned(),
global_id,
id: last_skill,
});
globlist.add_item(kind, skill, global_id, last_skill);
global_id += 1;
}
SkillTypes::SubSkill => {
last_sskill += 1;
globlist.push(LineId {
kind,
value: skill.to_owned(),
global_id,
id: last_sskill,
});
globlist.add_item(kind, skill, global_id, last_sskill);
global_id += 1;
}
SkillTypes::NewLine => (),
SkillTypes::Error => (),
}
global_id += 1;
}
let ls = SkillList::with_config(&ConfigList::new(path.to_string(), 0, 0, global_id));
todo!();
let list_sect = globlist.make_skilllist()?;
// 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));
// ls.populate(globlist);
Ok(ls)
}
}
struct LineId {
/// The structure representing the list of LineIds
struct ListId {
/// The inner list holding all values
list: Vec<LineId>,
}
impl ListId {
/// Add an item to the list
pub fn add_item(&mut self, kind: SkillTypes, value: &str, global_id: usize, id: usize) {
self.list.push(LineId {
kind,
value: value.to_owned(),
global_id,
id,
});
}
/// Create a new list with a given size hint
fn new(hint: usize) -> Self {
Self {
list: Vec::with_capacity(hint),
}
}
/// Populate all section and return a SkillList
fn make_skilllist(&mut self) -> Result<Vec<Section>, 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)
}
/// 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);
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(Section::from_vec(
name,
id,
global_id,
Some(list_subsection),
))
}
/// 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);
}
*index += 1;
}
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::<SubSkill>(name, id, global_id, None);
Ok(sec)
}
#[inline]
/// Get the skilltype of the element at the given index
fn get_kind(&self, index: &usize) -> &SkillTypes {
&self.list[*index].kind
}
#[inline]
/// Get the id of the element at the given index
fn get_id(&self, index: &usize) -> usize {
self.list[*index].id
}
#[inline]
/// Get the global_id of the element at the given index
fn get_global_id(&self, index: &usize) -> usize {
self.list[*index].id
}
#[inline]
/// Get the name of the element at the given index
fn get_name(&self, index: &usize) -> &str {
&self.list[*index].value
}
/// Returns the value, id and global_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,
)
}
}
/// Temporary data to preparse the file
pub struct LineId {
/// The level of the idem (Section to subskill)
kind: SkillTypes,
/// The raw line value
value: String,
/// The global skill number
global_id: usize,
/// The id relative to the indent level
id: usize,
}

View File

@ -13,13 +13,18 @@ type HashReplace = HashMap<String, String>;
/// A list config
pub struct ConfigList {
/// The capacity hint of the underlying data
pub capacity: usize,
/// The global id of the item
pub global_id: usize,
/// The individual id of the item
pub id: usize,
/// The name of the item
pub name: String,
}
impl ConfigList {
/// Create a config list from the given info
pub(crate) fn new(name: String, id: usize, global_id: usize, capacity: usize) -> Self {
Self {
name,
@ -44,6 +49,7 @@ pub struct SkillList {
}
impl SkillList {
/// Create a SkillList with a config
pub fn with_config(cfg: &ConfigList) -> Self {
Self {
list: Vec::new(),
@ -117,6 +123,7 @@ pub struct DataCell {
}
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(),
@ -180,6 +187,8 @@ pub(crate) trait Renderable {
fn get_mut_data(&mut self) -> &DataCell;
/// Create a new renderable
fn new(name: &str, id: usize, global_id: usize, size_hint: usize) -> Self;
/// Create a renderable with a given vector
fn from_vec<T>(name: &str, id: usize, global_id: usize, data: Option<T>) -> Self;
/// Get the vars hashmap
fn get_vars(&self) -> HashReplace;
// /// Render the element and all sub elements
@ -211,18 +220,6 @@ impl Renderable for Section {
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()
}
@ -232,12 +229,14 @@ impl Renderable for Section {
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;
// }
fn from_vec<T>(name: &str, id: usize, global_id: usize, data: Option<T>) -> Self {
let _ = id;
let _ = data;
let _ = global_id;
let _ = name;
todo!()
}
}
impl Renderable for SkillList {
@ -257,6 +256,14 @@ impl Renderable for SkillList {
fn get_vars(&self) -> HashReplace {
todo!()
}
fn from_vec<T>(name: &str, id: usize, global_id: usize, data: Option<T>) -> Self {
let _ = data;
let _ = global_id;
let _ = id;
let _ = name;
todo!()
}
}
impl Renderable for SubSection {
@ -272,6 +279,14 @@ impl Renderable for SubSection {
fn get_vars(&self) -> HashReplace {
todo!()
}
fn from_vec<T>(name: &str, id: usize, global_id: usize, data: Option<T>) -> Self {
let _ = data;
let _ = global_id;
let _ = id;
let _ = name;
todo!()
}
}
impl Renderable for Skill {
fn get_data(&self) -> &DataCell {
@ -289,6 +304,14 @@ impl Renderable for Skill {
fn get_vars(&self) -> HashReplace {
todo!()
}
fn from_vec<T>(name: &str, id: usize, global_id: usize, data: Option<T>) -> Self {
let _ = id;
let _ = global_id;
let _ = data;
let _ = name;
todo!()
}
}
impl Renderable for SubSkill {
fn get_data(&self) -> &DataCell {
@ -306,54 +329,29 @@ impl Renderable for SubSkill {
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,
// }
fn from_vec<T>(name: &str, id: usize, global_id: usize, data: Option<T>) -> Self {
let _ = id;
let _ = global_id;
let _ = data;
let _ = name;
todo!()
}
}
/// A parsable type for dispatch
pub enum SkillTypes {
/// This line is a Section
Section,
/// This line is a SubSection
SubSection,
/// This line is a Skill
Skill,
/// This line is a Subskill
SubSkill,
/// This line is an empty new line
NewLine,
/// This line couldn't be parsed
Error,
}

File diff suppressed because one or more lines are too long