capability_grower_configuration/
configuration_complexity.rs

1// ---------------- [ File: capability-grower-configuration/src/configuration_complexity.rs ]
2crate::ix!();
3
4/// This enum should be set to reflect the overall **complexity** vs. usability level.
5/// - **Simple**: Minimal detail, easiest to navigate.
6/// - **Balanced**: A middle ground that tries to combine enough detail without overwhelming complexity.
7/// - **Complex**: Very detailed, potentially more challenging to navigate but more expressive.
8#[derive(
9    Copy,
10    SaveLoad,
11    Debug, 
12    Clone, 
13    PartialEq, 
14    Eq, 
15    Serialize, 
16    Deserialize, 
17    Default,
18    AiJsonTemplate,
19    AiJsonTemplateWithJustification,
20)]
21pub enum ConfigurationComplexity {
22    /// This variant should be used to indicate minimal detail.
23    Simple,
24    /// This variant is the default, indicating a balanced approach.
25    #[default]
26    Balanced,
27    /// This variant indicates a highly detailed, more complex approach.
28    Complex,
29}
30
31impl FuzzyFromJsonValue for JustifiedConfigurationComplexity {
32    fn fuzzy_from_json_value(value: &serde_json::Value) -> Result<Self, FuzzyFromJsonValueError> {
33        trace!("(JustifiedConfigurationComplexity) Entering fuzzy_from_json_value");
34
35        // 1) If it's a string => parse directly (Simple,Balanced,Complex)
36        if let Some(s) = value.as_str() {
37            trace!("(JustifiedConfigurationComplexity) Found string='{}'", s);
38            return match s {
39                "Simple" => Ok(Self::Simple {
40                    variant_confidence: 0.0,
41                    variant_justification: String::new(),
42                }),
43                "Balanced" => Ok(Self::Balanced {
44                    variant_confidence: 0.0,
45                    variant_justification: String::new(),
46                }),
47                "Complex" => Ok(Self::Complex {
48                    variant_confidence: 0.0,
49                    variant_justification: String::new(),
50                }),
51                other => {
52                    error!("(JustifiedConfigurationComplexity) unknown string variant='{}'", other);
53                    Err(FuzzyFromJsonValueError::Other {
54                        target_type: "JustifiedConfigurationComplexity",
55                        detail: format!("Unknown string variant='{}'", other),
56                    })
57                }
58            };
59        }
60
61        // 2) If null => default Balanced
62        if value.is_null() {
63            debug!("(JustifiedConfigurationComplexity) Null => returning Balanced by default");
64            return Ok(Self::Balanced {
65                variant_confidence: 0.0,
66                variant_justification: String::new(),
67            });
68        }
69
70        // 3) Must be an object => check if single-key: e.g. { "Balanced": {} }
71        let mut obj = match value.as_object() {
72            Some(m) => {
73                trace!("(JustifiedConfigurationComplexity) object => checking single-key form or 'variant_name'");
74                m.clone()
75            }
76            None => {
77                error!("(JustifiedConfigurationComplexity) not an object => fail");
78                return Err(FuzzyFromJsonValueError::NotAnObject {
79                    target_type: "JustifiedConfigurationComplexity",
80                    actual: value.clone(),
81                });
82            }
83        };
84
85        if obj.len() == 1 && !obj.contains_key("variant_name") {
86            // single-key => unify
87            let (maybe_variant, nested) = {
88                let mut iter = obj.into_iter();
89                iter.next().unwrap()
90            };
91            trace!("(JustifiedConfigurationComplexity) single-key='{}'", maybe_variant);
92
93            // flatten nested if object
94            let mut sub = match nested.as_object() {
95                Some(submap) => {
96                    let mut sc = submap.clone();
97                    flatten_all_fields(&mut sc);
98                    sc
99                }
100                None => serde_json::Map::new(),
101            };
102
103            // parse variant_conf,just
104            let v_conf = sub
105                .remove("variant_confidence")
106                .and_then(|x| x.as_f64())
107                .unwrap_or(0.0);
108            let v_just = sub
109                .remove("variant_justification")
110                .and_then(|x| x.as_str().map(|s| s.to_string()))
111                .unwrap_or_default();
112
113            return match maybe_variant.as_str() {
114                "Simple" => Ok(Self::Simple {
115                    variant_confidence: v_conf,
116                    variant_justification: v_just,
117                }),
118                "Balanced" => Ok(Self::Balanced {
119                    variant_confidence: v_conf,
120                    variant_justification: v_just,
121                }),
122                "Complex" => Ok(Self::Complex {
123                    variant_confidence: v_conf,
124                    variant_justification: v_just,
125                }),
126                other => {
127                    error!("(JustifiedConfigurationComplexity) unknown single-key='{}'", other);
128                    Err(FuzzyFromJsonValueError::Other {
129                        target_type: "JustifiedConfigurationComplexity",
130                        detail: format!("Unrecognized single key='{}'", other),
131                    })
132                }
133            };
134        }
135
136        // 4) else expect { "variant_name":"Balanced", "variant_confidence":..., ... }
137        let var_name_val = match obj.remove("variant_name") {
138            Some(val) => val,
139            None => {
140                error!("(JustifiedConfigurationComplexity) Missing 'variant_name'");
141                return Err(FuzzyFromJsonValueError::Other {
142                    target_type: "JustifiedConfigurationComplexity",
143                    detail: "No 'variant_name' found => cannot parse as Simple,Balanced,Complex".to_string(),
144                });
145            }
146        };
147        let var_name_str = match var_name_val.as_str() {
148            Some(s) => s,
149            None => {
150                error!("(JustifiedConfigurationComplexity) 'variant_name' not string => fail");
151                return Err(FuzzyFromJsonValueError::Other {
152                    target_type: "JustifiedConfigurationComplexity",
153                    detail: format!("Expected string for variant_name, got {:?}", var_name_val),
154                });
155            }
156        };
157        let conf = obj
158            .remove("variant_confidence")
159            .and_then(|x| x.as_f64())
160            .unwrap_or(0.0);
161        let just = obj
162            .remove("variant_justification")
163            .and_then(|x| x.as_str().map(|s| s.to_string()))
164            .unwrap_or_default();
165
166        match var_name_str {
167            "Simple" => Ok(Self::Simple {
168                variant_confidence: conf,
169                variant_justification: just,
170            }),
171            "Balanced" => Ok(Self::Balanced {
172                variant_confidence: conf,
173                variant_justification: just,
174            }),
175            "Complex" => Ok(Self::Complex {
176                variant_confidence: conf,
177                variant_justification: just,
178            }),
179            other => {
180                error!("(JustifiedConfigurationComplexity) unknown variant='{}'", other);
181                Err(FuzzyFromJsonValueError::Other {
182                    target_type: "JustifiedConfigurationComplexity",
183                    detail: format!("Unknown variant='{}', expected [Simple,Balanced,Complex]", other),
184                })
185            }
186        }
187    }
188}