Reworking Node types

This commit is contained in:
Dandaedre 2024-04-08 22:19:35 +02:00
parent a4b1206cb0
commit 1c7511211e
3 changed files with 65 additions and 155 deletions

View File

@ -3,7 +3,7 @@ use std::{fs, path::Path};
use crate::{
loader::LoadError,
skills_structures::{Section, Skill, SkillList, SkillTypes, SubSection, SubSkill},
skills_structures::{Node, Renderable, SkillList, SkillTypes},
};
/// Defines a parser, able to extract a character sheet
@ -51,11 +51,13 @@ impl Parser<SkillList> for SkillList {
}
SkillTypes::NewLine => (),
SkillTypes::Error => (),
SkillTypes::Root => (),
SkillTypes::Repeating => todo!(),
}
}
let list_sect = globlist.make_skilllist()?;
// 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)
@ -87,7 +89,7 @@ impl ListId {
}
/// Populate all section and return a SkillList
fn make_skilllist(&mut self) -> Result<Vec<Section>, LoadError> {
fn make_skilllist(&mut self) -> Result<Vec<Node>, LoadError> {
let mut index = 0;
let mut list_section = Vec::new();
while index < self.list.len() {
@ -103,7 +105,7 @@ impl ListId {
}
/// Make a section starting from the given index
fn make_section(&self, index: &mut usize) -> Result<Section, LoadError> {
fn make_section(&self, index: &mut usize) -> Result<Node, LoadError> {
let mut list_subsection = Vec::new();
let (name, id, global_id) = self.get_info(index);
while *index < self.list.len() {
@ -114,16 +116,17 @@ impl ListId {
}
*index += 1;
}
Ok(Section::from_vec(
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<SubSection, LoadError> {
fn make_subsection(&self, index: &mut usize) -> Result<Node, LoadError> {
let mut list_skills = Vec::new();
let (name, id, global_id) = self.get_info(index);
while *index < self.list.len() {
@ -134,11 +137,17 @@ impl ListId {
}
*index += 1;
}
Ok(SubSection::from_vec(name, id, global_id, Some(list_skills)))
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<Skill, LoadError> {
fn make_skill(&self, index: &mut usize) -> Result<Node, LoadError> {
let mut list_subskills = Vec::new();
let (name, id, global_id) = self.get_info(index);
while *index < self.list.len() {
@ -149,17 +158,23 @@ impl ListId {
}
*index += 1;
}
Ok(Skill::from_vec(name, id, global_id, Some(list_subskills)))
Ok(Node::new(
name,
id,
global_id,
Some(list_subskills),
SkillTypes::Skill,
))
}
/// Make a subskill
fn make_subskill(&self, index: &mut usize) -> Result<SubSkill, LoadError> {
fn make_subskill(&self, index: &mut usize) -> Result<Node, 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);
let sec = Node::new(name, id, global_id, None, SkillTypes::SubSkill);
Ok(sec)
}

View File

@ -38,100 +38,43 @@ 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
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
pub struct DataCell {
/// The name, untouched, as a string
@ -191,17 +134,8 @@ 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 {
@ -210,7 +144,7 @@ pub(crate) trait Renderable {
/// 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;
fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self;
/// Get the vars hashmap
fn get_vars(&self) -> HashReplace;
// /// Render the element and all sub elements
@ -235,7 +169,7 @@ 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
}
@ -245,84 +179,41 @@ impl Renderable for Section {
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,
}
}
}
impl Renderable for SkillList {
fn get_data(&self) -> &DataCell {
todo!()
&self.data
}
fn get_mut_data(&mut self) -> &DataCell {
todo!()
}
fn new(name: &str, _id: usize, _global_id: usize, _size_hint: usize) -> Self {
let _ = name;
todo!()
&mut self.data
}
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!()
self.data.to_hashmap()
}
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!()
fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self {
Self {
childs,
data: DataCell::new(name, id, global_id),
level,
}
}
}
/// A parsable type for dispatch
pub enum SkillTypes {
/// This is the Root of the tree (unrepresentable)
Root,
/// This line is a Section
Section,
/// This line is a SubSection
@ -331,6 +222,8 @@ pub enum SkillTypes {
Skill,
/// This line is a Subskill
SubSkill,
/// This is a repeating section
Repeating,
/// This line is an empty new line
NewLine,
/// This line couldn't be parsed
@ -356,7 +249,9 @@ impl FromStr for SkillTypes {
mod tests {
use std::collections::HashMap;
use super::{DataCell, Renderable, Section};
use crate::skills_structures::{Node, SkillTypes};
use super::{DataCell, Renderable};
static S1: &str =
"This is a test sentence with some non-ASCII characters like café and résumé.";
@ -385,7 +280,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);

File diff suppressed because one or more lines are too long