capability-skeleton-string 0.1.0

A robust library for managing skill tree skeletons using string-based nodes and bidirectional conversions between string and numeric formats.
Documentation
// ---------------- [ File: capability-skeleton-string/src/dispatch_child_spec.rs ]
crate::ix!();

/// Use this object to configure per-child selection probability within a Dispatch node
///
#[derive(Eq,Default,AiJsonTemplateWithJustification,Builder,SaveLoad,Getters,Debug, Copy,Clone, PartialEq, Serialize, Deserialize, AiJsonTemplate)]
#[builder(pattern="owned", setter(into))]
#[getset(get = "pub")]
pub struct DispatchChildSpec {

    /// Set the `branch_selection_likelihood` field to indicate the likelihood of selecting this child branch, relative to its peers.
    ///
    /// Values may be 0..100 inclusive. Values over this maximum will be clipped.
    ///
    /// We will normalize this value amongst its peers relative to the their sum.
    ///
    #[serde(default)]
    #[serde(deserialize_with = "fuzzy_u8")]
    branch_selection_likelihood: u8,
}

impl FuzzyFromJsonValue for JustifiedDispatchChildSpec {
    fn fuzzy_from_json_value(value: &serde_json::Value)
        -> Result<Self, FuzzyFromJsonValueError>
    {
        trace!("(JustifiedDispatchChildSpec) Entering fuzzy_from_json_value => {}", value);

        let obj = match value.as_object() {
            Some(m) => {
                trace!("(JustifiedDispatchChildSpec) Found object => proceed to parse fields");
                m
            }
            None => {
                error!("(JustifiedDispatchChildSpec) Not an object => fail");
                return Err(FuzzyFromJsonValueError::NotAnObject {
                    target_type: "JustifiedDispatchChildSpec",
                    actual: value.clone(),
                });
            }
        };

        // parse branch_selection_likelihood => fuzzy_u8
        let bsl_val = obj.get("branch_selection_likelihood").ok_or_else(|| {
            error!("(JustifiedDispatchChildSpec) Missing 'branch_selection_likelihood' => fail");
            FuzzyFromJsonValueError::MissingField {
                field_name: "branch_selection_likelihood",
                target_type: "JustifiedDispatchChildSpec",
            }
        })?;
        let bsl_u8 = fuzzy_u8(bsl_val).map_err(|err| {
            error!("(JustifiedDispatchChildSpec) Could not parse branch_selection_likelihood => {:?}", err);
            FuzzyFromJsonValueError::Other {
                target_type: "JustifiedDispatchChildSpec",
                detail: err.to_string(),
            }
        })?;

        // parse optional confidence & justification
        let bsl_conf = obj.get("branch_selection_likelihood_confidence")
            .and_then(|v| v.as_f64())
            .unwrap_or(0.0);
        let bsl_just = obj.get("branch_selection_likelihood_justification")
            .and_then(|v| v.as_str())
            .unwrap_or("no justification given")
            .to_string();

        trace!("(JustifiedDispatchChildSpec) Successfully parsed => branch_selection_likelihood={}", bsl_u8);

        Ok(JustifiedDispatchChildSpec {
            branch_selection_likelihood: bsl_u8,
            branch_selection_likelihood_confidence: bsl_conf,
            branch_selection_likelihood_justification: bsl_just,
        })
    }
}