use std::process::Command;
pub struct WeatherController;
impl WeatherController {
pub fn get_weather(location: Option<&str>) -> Result<String, String> {
let url = if let Some(loc) = location {
format!("https://wttr.in/{}?format=3&m", loc.replace(' ', "+"))
} else {
"https://wttr.in/?format=3&m".to_string()
};
let output = Command::new("curl")
.arg("-s") .arg(&url)
.output()
.map_err(|e| format!("Failed to execute curl: {}", e))?;
if !output.status.success() {
return Err(format!(
"Failed to fetch weather data: {}",
String::from_utf8_lossy(&output.stderr)
));
}
let weather = String::from_utf8_lossy(&output.stdout).trim().to_string();
if weather.is_empty() {
return Err("No weather data received".to_string());
}
Ok(weather)
}
}