use serde_json::Value; pub struct CurrentWeather { current_weather: serde_json::Value, clean: bool, } impl CurrentWeather { pub fn new(current_weather: serde_json::Value, clean: bool) -> CurrentWeather { CurrentWeather { current_weather, clean, } } pub fn extract_simple_data( &self, key: &str, description: &str, end_text: Option<&str>, ) -> String { let data = self.extract_raw(key); self.parse_simple_data(data, description, end_text) } pub fn extract_raw(&self, key: &str) -> &Value { &self.current_weather[key] } fn parse_simple_data(&self, data: &Value, descriptor: &str, end_text: Option<&str>) -> String { if self.clean { format!("{data}") } else { let end_text = end_text.unwrap_or(""); format!("{descriptor}: {data}{end_text}") } } pub fn parse_custom_data(&self, data: &Value, custom: &str) -> String { if self.clean { format!("{data}") } else { format!("{custom}") } } } #[cfg(test)] mod tests { use super::*; #[test] fn test_extract_data_clean() { let current_weather = serde_json::json!({ "temperature": 25, }); let weather = CurrentWeather::new(current_weather, true); assert_eq!(weather.extract_simple_data("temperature", "", None), "25"); } #[test] fn test_extract_data_not_clean() { let current_weather = serde_json::json!({ "temperature": 25, }); let weather = CurrentWeather::new(current_weather, false); assert_eq!( weather.extract_simple_data("temperature", "Temperature", Some("°C")), "Temperature: 25°C" ); } }