sz_configtool_lib/versioning.rs
1//! Version operations
2//!
3//! Functions for managing configuration version information.
4//! Includes VERSION and COMPATIBILITY_VERSION fields.
5
6use crate::error::{Result, SzConfigError};
7use serde_json::Value;
8
9/// Get the configuration version
10///
11/// # Arguments
12///
13/// * `config_json` - Configuration JSON string
14///
15/// # Returns
16///
17/// Returns the VERSION string on success
18///
19/// # Example
20///
21/// ```
22/// use sz_configtool_lib::versioning;
23///
24/// let config = r#"{"G2_CONFIG": {"CONFIG_BASE_VERSION": {"VERSION": "4.0.0"}}}"#;
25/// let version = versioning::get_version(config).unwrap();
26/// assert_eq!(version, "4.0.0");
27/// ```
28pub fn get_version(config_json: &str) -> Result<String> {
29 let config_data: Value = serde_json::from_str(config_json)?;
30
31 let version = config_data
32 .get("G2_CONFIG")
33 .and_then(|g| g.get("CONFIG_BASE_VERSION"))
34 .and_then(|bv| bv.get("VERSION"))
35 .and_then(|v| v.as_str())
36 .ok_or_else(|| SzConfigError::NotFound("VERSION not found in configuration".to_string()))?;
37
38 Ok(version.to_string())
39}
40
41/// Get the compatibility version
42///
43/// # Arguments
44///
45/// * `config_json` - Configuration JSON string
46///
47/// # Returns
48///
49/// Returns the COMPATIBILITY_VERSION CONFIG_VERSION string on success
50///
51/// # Example
52///
53/// ```
54/// use sz_configtool_lib::versioning;
55///
56/// let config = r#"{"G2_CONFIG": {"CONFIG_BASE_VERSION": {"COMPATIBILITY_VERSION": {"CONFIG_VERSION": "11"}}}}"#;
57/// let version = versioning::get_compatibility_version(config).unwrap();
58/// assert_eq!(version, "11");
59/// ```
60pub fn get_compatibility_version(config_json: &str) -> Result<String> {
61 let config_data: Value = serde_json::from_str(config_json)?;
62
63 let version = config_data
64 .get("G2_CONFIG")
65 .and_then(|g| g.get("CONFIG_BASE_VERSION"))
66 .and_then(|bv| bv.get("COMPATIBILITY_VERSION"))
67 .and_then(|cv| cv.get("CONFIG_VERSION"))
68 .and_then(|v| v.as_str())
69 .ok_or_else(|| {
70 SzConfigError::NotFound("COMPATIBILITY_VERSION not found in configuration".to_string())
71 })?;
72
73 Ok(version.to_string())
74}
75
76/// Update the compatibility version
77///
78/// # Arguments
79///
80/// * `config_json` - Configuration JSON string
81/// * `new_version` - New version string
82///
83/// # Returns
84///
85/// Returns modified configuration JSON on success
86///
87/// # Example
88///
89/// ```
90/// use sz_configtool_lib::versioning;
91///
92/// let config = r#"{"G2_CONFIG": {"CONFIG_BASE_VERSION": {"COMPATIBILITY_VERSION": {"CONFIG_VERSION": "10"}}}}"#;
93/// let modified = versioning::update_compatibility_version(config, "11").unwrap();
94/// ```
95pub fn update_compatibility_version(config_json: &str, new_version: &str) -> Result<String> {
96 let mut config_data: Value = serde_json::from_str(config_json)?;
97
98 if let Some(g2_config) = config_data.get_mut("G2_CONFIG") {
99 if let Some(base_version) = g2_config.get_mut("CONFIG_BASE_VERSION") {
100 if let Some(compat_version) = base_version.get_mut("COMPATIBILITY_VERSION") {
101 if let Some(compat_obj) = compat_version.as_object_mut() {
102 compat_obj.insert("CONFIG_VERSION".to_string(), serde_json::json!(new_version));
103 } else {
104 return Err(SzConfigError::InvalidConfig(
105 "COMPATIBILITY_VERSION is not an object".to_string(),
106 ));
107 }
108 } else {
109 return Err(SzConfigError::NotFound(
110 "COMPATIBILITY_VERSION not found".to_string(),
111 ));
112 }
113 } else {
114 return Err(SzConfigError::NotFound(
115 "CONFIG_BASE_VERSION not found".to_string(),
116 ));
117 }
118 }
119
120 Ok(serde_json::to_string(&config_data)?)
121}
122
123/// Verify the compatibility version matches expected value
124///
125/// # Arguments
126///
127/// * `config_json` - Configuration JSON string
128/// * `expected_version` - Expected version string
129///
130/// # Returns
131///
132/// Returns `(current_version, matches)` tuple on success
133///
134/// # Example
135///
136/// ```
137/// use sz_configtool_lib::versioning;
138///
139/// let config = r#"{"G2_CONFIG": {"CONFIG_BASE_VERSION": {"COMPATIBILITY_VERSION": {"CONFIG_VERSION": "11"}}}}"#;
140/// let (current, matches) = versioning::verify_compatibility_version(config, "11").unwrap();
141/// assert!(matches);
142/// ```
143pub fn verify_compatibility_version(
144 config_json: &str,
145 expected_version: &str,
146) -> Result<(String, bool)> {
147 let config_data: Value = serde_json::from_str(config_json)?;
148
149 let current_version = config_data
150 .get("G2_CONFIG")
151 .and_then(|g| g.get("CONFIG_BASE_VERSION"))
152 .and_then(|bv| bv.get("COMPATIBILITY_VERSION"))
153 .and_then(|cv| cv.get("CONFIG_VERSION"))
154 .and_then(|v| v.as_str())
155 .ok_or_else(|| SzConfigError::NotFound("CONFIG_VERSION not found".to_string()))?;
156
157 let matches = current_version == expected_version;
158
159 Ok((current_version.to_string(), matches))
160}