capability-stripped-string-skeleton 0.1.0

Provides a streamlined representation of a skill tree structure, focusing on a lightweight, stripped-down version that retains essential hierarchical relationships without the full complexity of the original structure.
Documentation
// ---------------- [ File: capability-stripped-string-skeleton/src/display.rs ]
crate::ix!();

impl fmt::Display for StrippedStringSkeleton {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // We simply iterate over each node in the order they appear in `items()`
        // and print a block for that node.

        let mut items = self.items().clone();
        items.sort_by(|a,b| a.kind().cmp(b.kind()));

        for node in items {
            // 1) Print:  <Name> => <Kind> node with ...
            write!(f, "{} => ", node.name())?;

            match node.kind() {
                SkillTreeNodeKind::Dispatch => {
                    // e.g. "SanskritYoga => Dispatch node with branches: {"
                    writeln!(f, "Dispatch node with branches: {{")?;

                    // Then each child is:
                    //   // branch_selection_probability = X%
                    //   ChildName,
                    if let Some(children) = node.children() {
                        for child_spec in children {
                            // comment line
                            writeln!(
                                f,
                                "    // branch_selection_probability = {}%",
                                child_spec.probability()
                            )?;
                            // the child itself
                            writeln!(f, "    {},", child_spec.name())?;
                        }
                    }

                    writeln!(f, "}}")?;
                }

                SkillTreeNodeKind::Aggregate => {
                    // e.g. "PronunciationAndPhonetics => Aggregate node with components: ["
                    writeln!(f, "Aggregate node with components: [")?;

                    // Each child line:
                    //   ChildName, // mandatory
                    // or
                    //   ChildName, // optional, psome=XX%
                    if let Some(children) = node.children() {
                        for child_spec in children {
                            let nm = child_spec.name();
                            if *child_spec.optional() {
                                // optional => show probability
                                writeln!(
                                    f,
                                    "    {}, // optional, psome={}%",
                                    nm,
                                    child_spec.probability()
                                )?;
                            } else {
                                // mandatory
                                writeln!(f, "    {}, // mandatory", nm)?;
                            }
                        }
                    }

                    writeln!(f, "]")?;
                }

                SkillTreeNodeKind::LeafHolder => {
                    // e.g. "MantraConvergence => LeafHolder node with 10 leaves [capstone]"
                    write!(f, "LeafHolder node")?;
                    if let Some(n) = node.n_leaves() {
                        write!(f, " with {} leaves", n)?;
                    }
                    if let Some(true) = node.capstone() {
                        write!(f, " [capstone]")?;
                    }
                    writeln!(f)?;
                }
            }

            // Add a blank line between nodes for readability
            writeln!(f)?;
        }

        Ok(())
    }
}