Skip to main content

capability_stripped_string_skeleton/
display.rs

1// ---------------- [ File: capability-stripped-string-skeleton/src/display.rs ]
2crate::ix!();
3
4impl fmt::Display for StrippedStringSkeleton {
5    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6        // We simply iterate over each node in the order they appear in `items()`
7        // and print a block for that node.
8
9        let mut items = self.items().clone();
10        items.sort_by(|a,b| a.kind().cmp(b.kind()));
11
12        for node in items {
13            // 1) Print:  <Name> => <Kind> node with ...
14            write!(f, "{} => ", node.name())?;
15
16            match node.kind() {
17                SkillTreeNodeKind::Dispatch => {
18                    // e.g. "SanskritYoga => Dispatch node with branches: {"
19                    writeln!(f, "Dispatch node with branches: {{")?;
20
21                    // Then each child is:
22                    //   // branch_selection_probability = X%
23                    //   ChildName,
24                    if let Some(children) = node.children() {
25                        for child_spec in children {
26                            // comment line
27                            writeln!(
28                                f,
29                                "    // branch_selection_probability = {}%",
30                                child_spec.probability()
31                            )?;
32                            // the child itself
33                            writeln!(f, "    {},", child_spec.name())?;
34                        }
35                    }
36
37                    writeln!(f, "}}")?;
38                }
39
40                SkillTreeNodeKind::Aggregate => {
41                    // e.g. "PronunciationAndPhonetics => Aggregate node with components: ["
42                    writeln!(f, "Aggregate node with components: [")?;
43
44                    // Each child line:
45                    //   ChildName, // mandatory
46                    // or
47                    //   ChildName, // optional, psome=XX%
48                    if let Some(children) = node.children() {
49                        for child_spec in children {
50                            let nm = child_spec.name();
51                            if *child_spec.optional() {
52                                // optional => show probability
53                                writeln!(
54                                    f,
55                                    "    {}, // optional, psome={}%",
56                                    nm,
57                                    child_spec.probability()
58                                )?;
59                            } else {
60                                // mandatory
61                                writeln!(f, "    {}, // mandatory", nm)?;
62                            }
63                        }
64                    }
65
66                    writeln!(f, "]")?;
67                }
68
69                SkillTreeNodeKind::LeafHolder => {
70                    // e.g. "MantraConvergence => LeafHolder node with 10 leaves [capstone]"
71                    write!(f, "LeafHolder node")?;
72                    if let Some(n) = node.n_leaves() {
73                        write!(f, " with {} leaves", n)?;
74                    }
75                    if let Some(true) = node.capstone() {
76                        write!(f, " [capstone]")?;
77                    }
78                    writeln!(f)?;
79                }
80            }
81
82            // Add a blank line between nodes for readability
83            writeln!(f)?;
84        }
85
86        Ok(())
87    }
88}