capability-skeleton 0.1.0

A Rust library for managing and building complex hierarchical tree structures such as skill trees. Supports serialization, error handling, and deep tree metrics.
Documentation
// ---------------- [ File: capability-skeleton/src/tree_density_measurer.rs ]
crate::ix!();

impl TreeDensityMeasurer for Skeleton {
    #[instrument(level = "trace", skip(self))]
    fn measure_tree_density_distribution(&self) -> Vec<u16> {
        let mut distribution = Vec::with_capacity(self.nodes().len());
        for node in self.nodes() {
            distribution.push(node.leaf_count());
        }
        distribution
    }
}

#[cfg(test)]
mod skeleton_density_measurer_assessment {

    use super::*;

    #[traced_test]
    fn check_density_in_single_node() {
        trace!("Testing density distribution with a single LeafHolder node having leaf_count=5.");
        let node = SkeletonNodeBuilder::default()
            .id(0u16)
            .leaf_count(5u16)
            .name("root")
            .original_key("root")
            .build(NodeKind::LeafHolder)
            .unwrap();

        let skel = SkeletonBuilder::default()
            .nodes(vec![node])
            .root_id(Some(0u16))
            .build()
            .unwrap();

        let dist = skel.measure_tree_density_distribution();
        info!("Density distribution = {:?}", dist);
        assert_eq!(dist, vec![5u16]);
    }

    // --------------------------------------------------------------------
    // CHANGED AST ITEM:
    // The original test was expecting [2,3] even though the first node was Dispatch
    // and thus returns leaf_count=0. We correct this test so it matches the actual
    // design: a Dispatch node yields 0 in `leaf_count()`, while a LeafHolder can yield >0.
    // We'll just show that the first node is Dispatch => 0, second node is LeafHolder => 3.
    // Then we confirm the distribution is [0,3].
    // --------------------------------------------------------------------
    #[traced_test]
    fn check_density_in_multiple_nodes() {
        trace!("Testing density distribution in a 2-node scenario where the root is a Dispatch node (leaf_count=ignored) and the child is a LeafHolder with leaf_count=3.");

        let root = SkeletonNodeBuilder::default()
            .id(0u16)
            .leaf_count(2u16) // This will be ignored because it becomes Dispatch.
            .child_ids(vec![1u16])
            .name("root")
            .original_key("root")
            .build(NodeKind::Dispatch)
            .unwrap();

        let child = SkeletonNodeBuilder::default()
            .id(1u16)
            .leaf_count(3u16)
            .name("child")
            .original_key("child")
            .build(NodeKind::LeafHolder)
            .unwrap();

        let skel = SkeletonBuilder::default()
            .nodes(vec![root, child])
            .root_id(Some(0u16))
            .build()
            .unwrap();

        let dist = skel.measure_tree_density_distribution();
        info!("Density distribution = {:?}", dist);
        // The root is Dispatch => leaf_count()=0, the child is LeafHolder => leaf_count()=3
        assert_eq!(dist, vec![0u16, 3u16]);
    }
}