capability_skeleton_string/
dispatch_child_spec.rs

1// ---------------- [ File: capability-skeleton-string/src/dispatch_child_spec.rs ]
2crate::ix!();
3
4/// Use this object to configure per-child selection probability within a Dispatch node
5///
6#[derive(Eq,Default,AiJsonTemplateWithJustification,Builder,SaveLoad,Getters,Debug, Copy,Clone, PartialEq, Serialize, Deserialize, AiJsonTemplate)]
7#[builder(pattern="owned", setter(into))]
8#[getset(get = "pub")]
9pub struct DispatchChildSpec {
10
11    /// Set the `branch_selection_likelihood` field to indicate the likelihood of selecting this child branch, relative to its peers.
12    ///
13    /// Values may be 0..100 inclusive. Values over this maximum will be clipped.
14    ///
15    /// We will normalize this value amongst its peers relative to the their sum.
16    ///
17    #[serde(default)]
18    #[serde(deserialize_with = "fuzzy_u8")]
19    branch_selection_likelihood: u8,
20}
21
22impl FuzzyFromJsonValue for JustifiedDispatchChildSpec {
23    fn fuzzy_from_json_value(value: &serde_json::Value)
24        -> Result<Self, FuzzyFromJsonValueError>
25    {
26        trace!("(JustifiedDispatchChildSpec) Entering fuzzy_from_json_value => {}", value);
27
28        let obj = match value.as_object() {
29            Some(m) => {
30                trace!("(JustifiedDispatchChildSpec) Found object => proceed to parse fields");
31                m
32            }
33            None => {
34                error!("(JustifiedDispatchChildSpec) Not an object => fail");
35                return Err(FuzzyFromJsonValueError::NotAnObject {
36                    target_type: "JustifiedDispatchChildSpec",
37                    actual: value.clone(),
38                });
39            }
40        };
41
42        // parse branch_selection_likelihood => fuzzy_u8
43        let bsl_val = obj.get("branch_selection_likelihood").ok_or_else(|| {
44            error!("(JustifiedDispatchChildSpec) Missing 'branch_selection_likelihood' => fail");
45            FuzzyFromJsonValueError::MissingField {
46                field_name: "branch_selection_likelihood",
47                target_type: "JustifiedDispatchChildSpec",
48            }
49        })?;
50        let bsl_u8 = fuzzy_u8(bsl_val).map_err(|err| {
51            error!("(JustifiedDispatchChildSpec) Could not parse branch_selection_likelihood => {:?}", err);
52            FuzzyFromJsonValueError::Other {
53                target_type: "JustifiedDispatchChildSpec",
54                detail: err.to_string(),
55            }
56        })?;
57
58        // parse optional confidence & justification
59        let bsl_conf = obj.get("branch_selection_likelihood_confidence")
60            .and_then(|v| v.as_f64())
61            .unwrap_or(0.0);
62        let bsl_just = obj.get("branch_selection_likelihood_justification")
63            .and_then(|v| v.as_str())
64            .unwrap_or("no justification given")
65            .to_string();
66
67        trace!("(JustifiedDispatchChildSpec) Successfully parsed => branch_selection_likelihood={}", bsl_u8);
68
69        Ok(JustifiedDispatchChildSpec {
70            branch_selection_likelihood: bsl_u8,
71            branch_selection_likelihood_confidence: bsl_conf,
72            branch_selection_likelihood_justification: bsl_just,
73        })
74    }
75}