Skip to main content

capability_stripped_string_skeleton/
from_string_skeleton.rs

1// ---------------- [ File: capability-stripped-string-skeleton/src/from_string_skeleton.rs ]
2crate::ix!();
3
4impl From<JustifiedStringSkeleton> for StrippedStringSkeleton {
5    fn from(x: JustifiedStringSkeleton) -> Self {
6        let skel = StringSkeleton::from(x);
7        (&skel).into()
8    }
9}
10
11/// Below is our updated `From<&StringSkeleton> for StrippedStringSkeleton`
12/// implementation. We simply call `promote_missing_child_nodes_with_defaults`
13/// at the start, so that none of the aggregator/dispatch children get dropped.
14/// After that, the existing logic remains unchanged.
15///
16/// Make sure to import the `promote_missing_child_nodes_with_defaults` function
17/// in whatever module houses this `impl`.
18impl From<&StringSkeleton> for StrippedStringSkeleton {
19    fn from(x: &StringSkeleton) -> Self {
20        use tracing::{trace, debug};
21
22        trace!("Starting conversion from &StringSkeleton to StrippedStringSkeleton");
23
24        // -- NEW STEP: clone the input so we can fix missing aggregator/dispatch children
25        let mut temp_skel = x.clone();
26        trace!("Promoting any missing aggregator/dispatch children with default LeafHolders...");
27        promote_missing_child_nodes_with_defaults(&mut temp_skel);
28
29        debug!("After promoting missing nodes, map size = {}", temp_skel.map().len());
30
31        // Then we proceed as before, but using `temp_skel` instead of `x`.
32        let items = temp_skel.map().iter().map(|(_key, node)| {
33            match node {
34                StringSkeletonNode::Dispatch { name, ordering: _ordering, children } => {
35                    debug!("Converting a Dispatch node with name={}", name);
36                    let child_vec: Vec<NamedChildSpec> = children.iter().map(|(child_name, cspec)| {
37                        NamedChildSpecBuilder::default()
38                            .name(child_name.clone())
39                            .optional(false)
40                            .probability((*cspec.branch_selection_likelihood()).into())
41                            .build()
42                            .expect("Failed building NamedChildSpec for Dispatch")
43                    }).collect();
44                    StrippedNodeDataBuilder::default()
45                        .name(name.clone())
46                        .kind(SkillTreeNodeKind::Dispatch)
47                        .n_leaves(None)
48                        .capstone(None)
49                        .children(Some(child_vec))
50                        .build()
51                        .expect("Failed building StrippedNodeData for Dispatch")
52                }
53                StringSkeletonNode::LeafHolder { name, ordering: _ordering, n_leaves, capstone } => {
54                    debug!("Converting a LeafHolder node with name={}", name);
55                    StrippedNodeDataBuilder::default()
56                        .name(name.clone())
57                        .kind(SkillTreeNodeKind::LeafHolder)
58                        .n_leaves(Some(*n_leaves as usize))
59                        .capstone(Some(*capstone))
60                        .children(None)
61                        .build()
62                        .expect("Failed building StrippedNodeData for LeafHolder")
63                }
64                StringSkeletonNode::Aggregate { name, ordering: _ordering, children } => {
65                    debug!("Converting an Aggregate node with name={}", name);
66                    let child_vec: Vec<NamedChildSpec> = children.iter().map(|(child_name, cspec)| {
67                        NamedChildSpecBuilder::default()
68                            .name(child_name.clone())
69                            .optional(*cspec.optional())
70                            .probability((*cspec.psome_likelihood()).into())
71                            .build()
72                            .expect("Failed building NamedChildSpec for Aggregate")
73                    }).collect();
74                    StrippedNodeDataBuilder::default()
75                        .name(name.clone())
76                        .kind(SkillTreeNodeKind::Aggregate)
77                        .n_leaves(None)
78                        .capstone(None)
79                        .children(Some(child_vec))
80                        .build()
81                        .expect("Failed building StrippedNodeData for Aggregate")
82                }
83            }
84        }).collect();
85
86        trace!("Completed conversion from &StringSkeleton to StrippedStringSkeleton");
87
88        StrippedStringSkeletonBuilder::default()
89            .target_name(x.target_name().to_string())
90            .items(items)
91            .build()
92            .expect("Failed building StrippedStringSkeleton")
93    }
94}