Miscellaneous cleanup

This commit is contained in:
Mahesh Asolkar 2024-03-09 14:12:08 -08:00
parent c3efab7b2a
commit 9954dd0b62

View File

@ -61,35 +61,35 @@ impl BarWeather {
fn get_weather(self) { fn get_weather(self) {
// Print a web page onto stdout // Print a web page onto stdout
let mut easy = Easy::new(); let mut curl = Easy::new();
let app = self;
let url_string_1 :String = "https://api.weather.gov/stations/".to_owned(); let url_string_1 :String = "https://api.weather.gov/stations/".to_owned();
let url_string_2 :&str = app.opts.station.as_str(); let url_string_2 :&str = self.opts.station.as_str();
let url_string_3 :String = "/observations?limit=1".to_owned(); let url_string_3 :String = "/observations?limit=1".to_owned();
let url_string = url_string_1 + url_string_2 + &url_string_3; let url_string = url_string_1 + url_string_2 + &url_string_3;
easy.url(url_string.as_str()).unwrap();
curl.url(url_string.as_str()).unwrap();
let mut list = List::new(); let mut list = List::new();
list.append("User-Agent: Bar Weather (mahesh@heshapps.com)").unwrap(); list.append("User-Agent: Bar Weather (mahesh@heshapps.com)").unwrap();
easy.http_headers(list).unwrap(); curl.http_headers(list).unwrap();
easy.write_function(move |data| { curl.write_function(move |data| {
// To debug returned JSON // To debug returned JSON
// use std::io::{stdout, Write}; // use std::io::{stdout, Write};
// stdout().write_all(data).unwrap(); // stdout().write_all(data).unwrap();
let v: Value = serde_json::from_str(str::from_utf8(data).unwrap()).unwrap(); let v: Value = serde_json::from_str(str::from_utf8(data).unwrap()).unwrap();
let temperature_value :f32 = app.get_current_temperature(v); let temperature_value :f32 = self.get_current_temperature(v);
let temperature_unit :String = app.get_temperature_unit(); let temperature_unit :String = self.get_temperature_unit();
println!("{}{}", temperature_value, temperature_unit);
println!("{:.2}{}", temperature_value, temperature_unit); println!("{:.2}{}", temperature_value, temperature_unit);
Ok(data.len()) Ok(data.len())
} }
).unwrap(); ).unwrap();
easy.perform().unwrap(); curl.perform().unwrap();
} }
fn check_options(&self) -> bool { fn check_options(&self) -> bool {
@ -123,15 +123,11 @@ impl BarWeather {
fn main() { fn main() {
let cmd_args = CommandlineArgs::parse(); let cmd_args = CommandlineArgs::parse();
let app = BarWeather { let app = BarWeather {
//acts: BarWeatherActions {
opts: AppOptions { opts: AppOptions {
// units: TemperatureUnits::Imperial,
// units: TemperatureUnits::Metric,
units: if cmd_args.metric { TemperatureUnits::Metric } units: if cmd_args.metric { TemperatureUnits::Metric }
else { TemperatureUnits::Imperial }, else { TemperatureUnits::Imperial },
station: cmd_args.station, station: cmd_args.station,
}, },
//},
}; };
app.run(); app.run();