Skip to content

Commit

Permalink
Fixed several clippy hints.
Browse files Browse the repository at this point in the history
  • Loading branch information
RRArny committed Sep 12, 2024
1 parent 5c20086 commit 1654942
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 72 deletions.
13 changes: 3 additions & 10 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn request_wx(config: &Config, secrets: &Secrets) -> Option<Value> {
}
}

/// Given a properly formattet position string and Secrets, requests METAR from AvWx and wraps the Response in a Result.
/// Given a properly formattet position string and Secrets, requests METAR from `AvWx` and wraps the Response in a Result.
async fn send_api_call(position: String, secrets: &Secrets) -> Result<Response, Error> {
let uri = format!("https://avwx.rest/api/metar/{position}?onfail=nearest&options=info");
let resp: Response = Client::new()
Expand Down Expand Up @@ -52,29 +52,22 @@ async fn get_nearest_station(config: &Config, secrets: &Secrets) -> Option<Strin
let lat = body.get("latitude")?.as_f64()?;
let lon = body.get("longitude")?.as_f64()?;

println!("{}/{}", lat, lon);

let uri = format!(
"https://avwx.rest/api/station/near/{},{}?n=1&reporting=true",
lat, lon
);
let uri = format!("https://avwx.rest/api/station/near/{lat},{lon}?n=1&reporting=true");
let resp = client
.get(uri)
.header("Authorization", format!("BEARER {}", secrets.avwx_api_key))
.send()
.await
.ok()?;
println!("{:?}", resp);
let value = &resp.json::<Value>().await.ok()?;
println!("val: {value}");
let station = value.get(0)?.get("station")?.get("icao")?.as_str()?;

Some(station.to_string())
}

/// For a given ICAO code as String and the necessary Secrets makes request to AvWx to check if the code is valid.
pub async fn check_icao_code(icao: &String, secrets: &Secrets) -> bool {
let uri = format!("https://avwx.rest/api/station/{}", icao);
let uri = format!("https://avwx.rest/api/station/{icao}");

let resp = Client::new()
.get(uri)
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn get_config(secrets: &Secrets) -> Config {
position: Position::Airfield(icao),
};
}
println!("Invalid airfield {}. Defaulting to geoip...", icao);
println!("Invalid airfield {icao}. Defaulting to geoip...");
} else if let Some(lat) = args.latitude {
if let Some(long) = args.longitude {
return Config {
Expand All @@ -78,7 +78,7 @@ async fn get_weather(config: &Config, secrets: &Secrets) -> ColoredString {
let json = request_wx(config, secrets)
.await
.expect("Weather request failed.");
let metar = Metar::from_json(json, config).expect("Invalid weather data received...");
let metar = Metar::from_json(&json, config).expect("Invalid weather data received...");
metar.colorise()
}

Expand All @@ -88,5 +88,5 @@ async fn main() {
let config = get_config(&secrets).await;
let wx_string = get_weather(&config, &secrets).await;

println!("{}", wx_string);
println!("{wx_string}");
}
111 changes: 52 additions & 59 deletions src/metar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,46 @@ pub enum MetarField {
impl MetarField {
pub fn colourise(&self) -> ColoredString {
match self {
MetarField::Visibility(vis) => colourise_visibility(vis),
MetarField::Visibility(vis) => colourise_visibility(*vis),
MetarField::TimeStamp(datetime) => colourize_timestamp(datetime),
MetarField::Wind {
direction,
strength,
gusts,
unit,
} => colourise_wind(direction, strength, gusts, unit),
MetarField::WindVariability { low_dir, hi_dir } => colourise_wind_var(low_dir, hi_dir),
} => colourise_wind(*direction, *strength, *gusts, *unit),
MetarField::WindVariability { low_dir, hi_dir } => {
colourise_wind_var(*low_dir, *hi_dir)
}
MetarField::Temperature {
temp,
dewpoint,
unit,
} => colourise_temperature(temp, dewpoint, unit),
MetarField::Qnh(qnh, unit) => colourise_qnh(qnh, unit),
} => colourise_temperature(*temp, *dewpoint, *unit),
MetarField::Qnh(qnh, unit) => colourise_qnh(*qnh, *unit),
MetarField::WxCode(code, intensity, proximity, descriptor) => {
colourise_wx_code(code, intensity, proximity, descriptor)
}
MetarField::Remarks(str) => str.black().on_white(),
MetarField::Clouds(cloud, alt) => colourise_clouds(cloud, alt),
MetarField::Clouds(cloud, alt) => colourise_clouds(cloud, *alt),
}
}
}

fn colourise_clouds(cloud: &Clouds, alt: &i64) -> ColoredString {
let res: ColoredString = format!("{}", cloud).color(match cloud {
fn colourise_clouds(cloud: &Clouds, alt: i64) -> ColoredString {
let res: ColoredString = format!("{cloud}").color(match cloud {
Clouds::Ovc => Color::Red,
Clouds::Brk => Color::Yellow,
_ => Color::Green,
});
let altstr: ColoredString = format!("{}", alt).color(if *alt <= 10 {
let altstr: ColoredString = format!("{alt}").color(if alt <= 10 {
Color::Red
} else if *alt <= 30 {
} else if alt <= 30 {
Color::Yellow
} else {
Color::Green
});
format!("{}{}", res, altstr).into()
format!("{res}{altstr}").into()
}

fn colourise_wx_code(
Expand All @@ -112,10 +114,8 @@ fn colourise_wx_code(
) -> ColoredString {
let codestr = format!("{code}").color(match code {
WxCode::Ra => Color::BrightYellow,
WxCode::Gr => Color::Red,
WxCode::Gr | WxCode::Sn | WxCode::Up => Color::Red,
WxCode::Gs => Color::Yellow,
WxCode::Sn => Color::Red,
WxCode::Up => Color::Red,
WxCode::Po => Color::BrightRed,
_ => Color::White,
});
Expand All @@ -136,69 +136,64 @@ fn colourise_wx_code(
format!("{intensitystr}{descrstr}{codestr}{proximity}").magenta()
}

fn colourise_qnh(qnh: &i64, unit: &PressureUnit) -> ColoredString {
fn colourise_qnh(qnh: i64, unit: PressureUnit) -> ColoredString {
match unit {
PressureUnit::Hpa => format!("Q{}", qnh).color(if *qnh >= 1013 {
PressureUnit::Hpa => format!("Q{qnh}").color(if qnh >= 1013 {
Color::Green
} else {
Color::Yellow
}),
PressureUnit::Inhg => format!("A{}", qnh / 100).color(if *qnh >= 2992 {
PressureUnit::Inhg => format!("A{}", qnh / 100).color(if qnh >= 2992 {
Color::Green
} else {
Color::Yellow
}),
}
}

fn colourise_temperature(temp: &i64, dewpoint: &i64, _unit: &TemperatureUnit) -> ColoredString {
let temp_str = temp.to_string().color(if *temp > 0 {
fn colourise_temperature(temp: i64, dewpoint: i64, _unit: TemperatureUnit) -> ColoredString {
let temp_str = temp.to_string().color(if temp > 0 {
Color::BrightGreen
} else {
Color::BrightRed
});
let dew_str = dewpoint.to_string().color(if *temp - *dewpoint > 3 {
let dew_str = dewpoint.to_string().color(if temp - dewpoint > 3 {
Color::Green
} else {
Color::Red
});
format!("{}/{}", temp_str, dew_str).into()
format!("{temp_str}/{dew_str}").into()
}

fn colourise_wind_var(low_dir: &i64, hi_dir: &i64) -> ColoredString {
format!("{}V{}", low_dir, hi_dir).color(if hi_dir - low_dir < 45 {
fn colourise_wind_var(low_dir: i64, hi_dir: i64) -> ColoredString {
format!("{low_dir}V{hi_dir}").color(if hi_dir - low_dir < 45 {
Color::Green
} else {
Color::Yellow
})
}

fn colourise_wind(
direction: &i64,
strength: &i64,
gusts: &i64,
_unit: &SpeedUnit,
) -> ColoredString {
let dir_str = format!("{:03}", direction).to_string();
let strength_str = format!("{:02}", strength)
fn colourise_wind(direction: i64, strength: i64, gusts: i64, _unit: SpeedUnit) -> ColoredString {
let dir_str = format!("{direction:03}").to_string();
let strength_str = format!("{strength:02}")
.to_string()
.color(if *strength > 15 {
.color(if strength > 15 {
Color::Red
} else {
Color::Green
});
let mut output: ColoredString = format!("{}{}", dir_str, strength_str).into();
if *gusts > 0 {
let gust_str = format!("{:02}", gusts)
let mut output: ColoredString = format!("{dir_str}{strength_str}").into();
if gusts > 0 {
let gust_str = format!("{gusts:02}")
.to_string()
.color(if gusts - strength > 5 {
Color::BrightRed
} else {
Color::Green
});
output = format!("{}G{}", output, gust_str).into();
output = format!("{output}G{gust_str}").into();
}
output = format!("{}KT", output).into();
output = format!("{output}KT").into();
output
}

Expand All @@ -216,58 +211,56 @@ fn colourize_timestamp(datetime: &DateTime<FixedOffset>) -> ColoredString {
})
}

fn colourise_visibility(vis: &i64) -> ColoredString {
if *vis >= 6000 {
fn colourise_visibility(vis: i64) -> ColoredString {
if vis >= 6000 {
vis.to_string().green()
} else if *vis > 1500 {
} else if vis > 1500 {
vis.to_string().yellow()
} else {
vis.to_string().red()
}
}

impl Metar {
pub fn from_json(json: Value, config: &Config) -> Option<Self> {
// println!("{:?}", json);

pub fn from_json(json: &Value, config: &Config) -> Option<Self> {
let mut station = String::new();
if let Some(icao) = json.get("station") {
station = icao.as_str()?.to_string();
}

let units: Units = Units::from_json(&json);
let units: Units = Units::from_json(json);

let mut fields: Vec<MetarField> = Vec::new();

if let Some(time) = get_timestamp(&json) {
if let Some(time) = get_timestamp(json) {
fields.push(time);
}

if let Some(wind) = get_winds(&json, &units) {
if let Some(wind) = get_winds(json, units) {
fields.push(wind);
}

if let Some(wind_var) = get_wind_var(&json) {
if let Some(wind_var) = get_wind_var(json) {
fields.push(wind_var);
}

if let Some(vis) = get_visibility(&json, &units) {
if let Some(vis) = get_visibility(json, units) {
fields.push(vis);
}

if let Some(temp) = get_temp(&json, &units) {
if let Some(temp) = get_temp(json, units) {
fields.push(temp);
}

if let Some(qnh) = get_qnh(&json, &units) {
if let Some(qnh) = get_qnh(json, units) {
fields.push(qnh);
}

fields.append(&mut get_wxcodes_from_json(&json));
fields.append(&mut get_wxcodes_from_json(json));

fields.append(&mut get_clouds_from_json(&json));
fields.append(&mut get_clouds_from_json(json));

if let Some(rmks) = get_remarks(&json) {
if let Some(rmks) = get_remarks(json) {
fields.push(rmks);
}

Expand Down Expand Up @@ -306,7 +299,7 @@ fn get_timestamp(json: &Value) -> Option<MetarField> {
Some(MetarField::TimeStamp(datetime))
}

fn get_qnh(json: &Value, units: &Units) -> Option<MetarField> {
fn get_qnh(json: &Value, units: Units) -> Option<MetarField> {
let qnh_val: &Value = json.get("altimeter")?.get("value")?;
let qnh: i64 = if qnh_val.is_f64() {
qnh_val.as_f64()?.mul(100.).round() as i64
Expand All @@ -317,7 +310,7 @@ fn get_qnh(json: &Value, units: &Units) -> Option<MetarField> {
Some(MetarField::Qnh(qnh, units.pressure))
}

fn get_temp(json: &Value, units: &Units) -> Option<MetarField> {
fn get_temp(json: &Value, units: Units) -> Option<MetarField> {
let temp = json.get("temperature")?.get("value")?.as_i64()?;
let dewpoint = json.get("dewpoint")?.get("value")?.as_i64()?;
Some(MetarField::Temperature {
Expand All @@ -333,7 +326,7 @@ fn get_wind_var(json: &Value) -> Option<MetarField> {
for dir in wind_dirs {
dirs.push(dir.get("value")?.as_i64()?);
}
dirs.sort();
dirs.sort_unstable();
let low_dir = dirs.first()?;
let hi_dir = dirs.last()?;
Some(MetarField::WindVariability {
Expand All @@ -342,13 +335,13 @@ fn get_wind_var(json: &Value) -> Option<MetarField> {
})
}

fn get_winds(json: &Value, units: &Units) -> Option<MetarField> {
fn get_winds(json: &Value, units: Units) -> Option<MetarField> {
let direction = json.get("wind_direction")?.get("value")?.as_i64()?;
let strength = json.get("wind_speed")?.get("value")?.as_i64()?;
let gusts = json
.get("wind_gust")
.and_then(|g| g.get("value"))
.and_then(|g| g.as_i64())
.and_then(serde_json::Value::as_i64)
.unwrap_or(0);

Some(MetarField::Wind {
Expand All @@ -359,7 +352,7 @@ fn get_winds(json: &Value, units: &Units) -> Option<MetarField> {
})
}

fn get_visibility(json: &Value, _units: &Units) -> Option<MetarField> {
fn get_visibility(json: &Value, _units: Units) -> Option<MetarField> {
let vis = json.get("visibility")?.get("value")?.as_i64()?;
Some(MetarField::Visibility(vis))
}
Expand Down

0 comments on commit 1654942

Please sign in to comment.