Debugs the node generation

This commit is contained in:
Dandaedre 2024-04-12 05:26:13 +02:00
parent 897c8933dd
commit 475ad9b6b1
3 changed files with 81 additions and 27 deletions

View File

@ -30,6 +30,7 @@ impl Parser<SkillList> for SkillList {
}
/// The structure representing the list of LineIds
#[derive(Debug)]
pub struct ListId {
/// The inner list holding all values
list: Vec<LineId>,
@ -74,30 +75,57 @@ impl ListId {
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);
println!("Processing {} [{}/{}]", name, global_id, id);
let lower_level = level.get_lower_level();
println!("=> Lower : {:?}", lower_level);
if *self.get_kind(index) != level {
return Err(LoadError::WrongHierarchy);
}
println!("Entering at : index {}", index);
*index += 1; // Very ugly
match level {
SkillTypes::Root => unimplemented!(),
SkillTypes::Section | SkillTypes::SubSection | SkillTypes::Skill => {
let lower_level = &lower_level.unwrap();
while *index < self.list.len() {
println!("Iterating at : index {}", index);
if level.is_child(*self.get_kind(index)) {
match self.make_node(index, *lower_level) {
Ok(value) => {
println!("Retreiving :\n{}", value.debug_display());
list_childs.push(value);
// println!(
// "Childs: \n{}",
// list_childs
// .iter()
// .map(|v| v.debug_display())
// .collect::<Vec<_>>()
// .join("\n&&&\n")
// );
}
// This is not good behavious
Err(LoadError::EmptyLine) => (),
_ => return Err(LoadError::LineParseError),
// Err(LoadError::LineParseError) => (),
_ => (),
// _ => return Err(LoadError::LineParseError),
}
*index += 1;
}
} else {
break;
} // Else Do Something
*index += 1;
}
Ok(Node::new(name, id, global_id, Some(list_childs), level))
let node = Node::new(name, id, global_id, Some(list_childs), level);
println!("Exiting with:\n{}", node.debug_display());
Ok(node)
}
SkillTypes::RepeatingSkills | SkillTypes::RepeatingItem | SkillTypes::SubSkill => {
Ok(Node::new(name, id, global_id, None, level))
let node = Node::new(name, id, global_id, None, level);
println!(
"Finished at : index {} with {}",
index,
node.debug_display()
);
Ok(node)
}
SkillTypes::NewLine => Err(LoadError::EmptyLine),
}
@ -189,6 +217,7 @@ impl ListId {
}
/// Temporary data to preparse the file
#[derive(Debug)]
pub struct LineId {
/// The level of the idem (Section to subskill)
kind: SkillTypes,
@ -233,18 +262,6 @@ mod test {
assert_eq!(list.get_kind(&0), &SkillTypes::Section);
}
#[test]
fn test_parser() {}
#[test]
fn test_skilllist() {}
#[test]
fn test_node() {}
#[test]
fn test_listid() {}
#[derive(Debug)]
struct BareNode {
list: Option<Vec<BareNode>>,
@ -414,4 +431,32 @@ mod test {
}
}
}
#[test]
fn test_parser() {}
#[test]
fn test_skilllist() {}
#[test]
fn test_node() {
// let file_content = "@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"
// .to_string();
let file_content = "@s0\n+ss0\n=sk0\n-sks0".to_string();
let line_ids = ListId::from_string(file_content).unwrap();
// println!("{:?}", line_ids);
let mut index = 0;
let node = line_ids.make_node(&mut index, SkillTypes::Section);
// println!("{:?}", node);
let node = node.unwrap();
println!("Final Nodes: \n{}", node.debug_display());
// println!("{:?}", node);
}
#[test]
fn test_listid() {}
}

View File

@ -67,6 +67,7 @@ impl SkillList {
}
/// A node, with a level instead of a hierarchy
#[derive(Debug)]
pub struct Node {
/// The (optional) list of subnodes
childs: Option<Vec<Node>>,
@ -77,6 +78,7 @@ pub struct Node {
}
/// The data of a cell
#[derive(Debug)]
pub struct DataCell {
/// The name, untouched, as a string
name: String,
@ -186,11 +188,18 @@ impl Renderable for Node {
fn debug_display(&self) -> String {
let mut f = String::new();
write!(f, "{}\n", self.to_string()).unwrap();
write!(f, "{}", self.to_string()).unwrap();
if let Some(childs) = &self.childs {
for child in childs.iter() {
write!(f, "{}\n", child.to_string()).unwrap();
}
write!(
f,
"\n{}",
childs
.iter()
.map(|v| v.debug_display())
.collect::<Vec<_>>()
.join("\n")
)
.unwrap();
}
f
}
@ -224,7 +233,7 @@ impl Renderable for SkillList {
writeln!(s, "[{:?}]{:?}", self.level, self.data.name).unwrap();
if let Some(childs) = &self.childs {
for chld in childs.iter() {
write!(s, "{}", chld.to_string()).unwrap();
write!(s, "{}", chld.debug_display()).unwrap();
}
}
s
@ -428,18 +437,18 @@ mod tests {
#[test]
fn test_display() {
let n1 = Node::new("Name0", 0, 2, None, SkillTypes::Section);
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 [Section] Name0 [0/2]\n".to_owned()
" [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]".to_owned()
"[Root]\"Biglist\"\n [Section] Name1 [0/1]\n [SubSection] Name0 [0/2]".to_owned()
)
}

File diff suppressed because one or more lines are too long