Skip to main content

sz_configtool_lib/
system_params.rs

1//! System Parameter operations
2//!
3//! Functions for managing system parameters in the configuration.
4//! Currently supports the relationshipsBreakMatches parameter (BREAK_RES in RCLASS_ID=2).
5
6use crate::error::{Result, SzConfigError};
7use serde_json::Value;
8use std::collections::HashMap;
9
10/// List all system parameters
11///
12/// # Arguments
13///
14/// * `config_json` - Configuration JSON string
15///
16/// # Returns
17///
18/// Returns a HashMap of parameter names to values
19///
20/// # Example
21///
22/// ```
23/// use sz_configtool_lib::system_params;
24///
25/// let config = r#"{"G2_CONFIG": {"CFG_RTYPE": [{"RCLASS_ID": 2, "BREAK_RES": 1}]}}"#;
26/// let params = system_params::list_system_parameters(config).unwrap();
27/// assert!(params.contains_key("relationshipsBreakMatches"));
28/// ```
29pub fn list_system_parameters(config_json: &str) -> Result<HashMap<String, String>> {
30    let config_data: Value = serde_json::from_str(config_json)?;
31    let mut params = HashMap::new();
32
33    // Find RCLASS_ID == 2 in CFG_RTYPE to get BREAK_RES value
34    if let Some(g2_config) = config_data.get("G2_CONFIG") {
35        if let Some(rtype_array) = g2_config.get("CFG_RTYPE").and_then(|v| v.as_array()) {
36            for rtype in rtype_array {
37                if let Some(rclass_id) = rtype.get("RCLASS_ID").and_then(|v| v.as_i64()) {
38                    if rclass_id == 2 {
39                        if let Some(break_res) = rtype.get("BREAK_RES").and_then(|v| v.as_i64()) {
40                            params.insert(
41                                "relationshipsBreakMatches".to_string(),
42                                break_res.to_string(),
43                            );
44                        }
45                        break;
46                    }
47                }
48            }
49        }
50    }
51
52    Ok(params)
53}
54
55/// Set a system parameter
56///
57/// # Arguments
58///
59/// * `config_json` - Configuration JSON string
60/// * `parameter_name` - Parameter name (e.g., "relationshipsBreakMatches")
61/// * `parameter_value` - Parameter value
62///
63/// # Returns
64///
65/// Returns modified configuration JSON on success
66///
67/// # Example
68///
69/// ```
70/// use sz_configtool_lib::system_params;
71/// use serde_json::json;
72///
73/// let config = r#"{"G2_CONFIG": {"CFG_RTYPE": [{"RCLASS_ID": 2, "BREAK_RES": 0}]}}"#;
74/// let modified = system_params::set_system_parameter(config, "relationshipsBreakMatches", &json!(1)).unwrap();
75/// ```
76pub fn set_system_parameter(
77    config_json: &str,
78    parameter_name: &str,
79    parameter_value: &Value,
80) -> Result<String> {
81    let parameter_name = parameter_name.to_uppercase();
82    let mut config_data: Value = serde_json::from_str(config_json)?;
83    let mut found = false;
84
85    // Currently, the main system parameter is "relationshipsBreakMatches" which maps to BREAK_RES in RCLASS_ID=2
86    if parameter_name == "RELATIONSHIPSBREAKMATCHES"
87        || parameter_name == "RELATIONSHIPS_BREAK_MATCHES"
88    {
89        if let Some(g2_config) = config_data.get_mut("G2_CONFIG") {
90            if let Some(rtype_array) = g2_config
91                .get_mut("CFG_RTYPE")
92                .and_then(|v| v.as_array_mut())
93            {
94                for rtype in rtype_array.iter_mut() {
95                    if let Some(rclass_id) = rtype.get("RCLASS_ID").and_then(|v| v.as_i64()) {
96                        if rclass_id == 2 {
97                            if let Some(rtype_obj) = rtype.as_object_mut() {
98                                rtype_obj.insert("BREAK_RES".to_string(), parameter_value.clone());
99                                found = true;
100                            }
101                            break;
102                        }
103                    }
104                }
105            }
106        }
107    } else {
108        return Err(SzConfigError::InvalidConfig(format!(
109            "Unknown system parameter: {parameter_name}"
110        )));
111    }
112
113    if !found {
114        return Err(SzConfigError::NotFound(format!(
115            "Failed to set system parameter: {parameter_name}"
116        )));
117    }
118
119    Ok(serde_json::to_string(&config_data)?)
120}