From 49a09226d99939264374d27a0f9ae86c359a923e Mon Sep 17 00:00:00 2001 From: Fred Clausen <43556888+fredclausen@users.noreply.github.com> Date: Sat, 3 Feb 2024 09:06:20 -0700 Subject: [PATCH] clippy pedantic --- src/decoders/json.rs | 2 + src/decoders/raw_types/me.rs | 165 +++++++++++++++++-------- src/helpers/encode_adsb_beast_input.rs | 2 + src/state_machine/state.rs | 7 +- 4 files changed, 121 insertions(+), 55 deletions(-) diff --git a/src/decoders/json.rs b/src/decoders/json.rs index 3818cb3..e64d605 100644 --- a/src/decoders/json.rs +++ b/src/decoders/json.rs @@ -161,6 +161,8 @@ impl JSONMessage { self.pretty_print_with_options() } + // FIXME: Can/should this be refactored in to less lines? + #[allow(clippy::too_many_lines)] fn pretty_print_with_options(&self) -> String { // Go through each field and print it out let mut output: String = String::new(); diff --git a/src/decoders/raw_types/me.rs b/src/decoders/raw_types/me.rs index bf50c7b..e1fa583 100644 --- a/src/decoders/raw_types/me.rs +++ b/src/decoders/raw_types/me.rs @@ -15,6 +15,7 @@ use super::{ altitude::Altitude, autopilot_modes::{AltitudeHold, ApproachMode, AutopilotEngaged, VNAVEngaged, LNAV, TCAS}, capability::Capability, + emergencystate::EmergencyState, heading::SelectedHeadingStatus, icao::ICAO, identification::Identification, @@ -74,6 +75,8 @@ pub enum ME { impl ME { /// `to_string` with DF.id() input + // FIXME: Can/should this be refactored in to less lines? + #[allow(clippy::too_many_lines)] pub(crate) fn to_string( &self, icao: ICAO, @@ -200,89 +203,147 @@ impl ME { squawk, .. }) => { - writeln!( - f, - " Extended Squitter{transponder}Emergency/priority status", + print_aircraft_status( + &mut f, + transponder, + icao, + capability, + address_type, + *emergency_state, + *squawk, )?; - writeln!(f, " Address: {icao} {address_type}")?; - writeln!(f, " Air/Ground: {capability}")?; - writeln!(f, " Squawk: {squawk:02X?}")?; - writeln!(f, " Emergency/priority: {emergency_state}")?; } ME::TargetStateAndStatusInformation(target_info) => { - writeln!( - f, - " Extended Squitter{transponder}Target state and status (V2)", + print_target_state_and_status_information( + &mut f, + transponder, + icao, + capability, + address_type, + target_info, )?; - writeln!(f, " Address: {icao} {address_type}")?; - writeln!(f, " Air/Ground: {capability}")?; - writeln!(f, " Target State and Status:")?; - writeln!(f, " Target altitude: MCP, {} ft", target_info.altitude)?; - writeln!(f, " Altimeter setting: {} millibars", target_info.qnh)?; - if target_info.is_heading == SelectedHeadingStatus::Valid { - writeln!(f, " Target heading: {}", target_info.heading)?; - } - if target_info.tcas == TCAS::Engaged { - write!(f, " ACAS: operational ")?; - if target_info.autopilot == AutopilotEngaged::Engaged { - write!(f, "autopilot ")?; - } - if target_info.vnac == VNAVEngaged::Engaged { - write!(f, "vnav ")?; - } - if target_info.alt_hold == AltitudeHold::Engaged { - write!(f, "altitude-hold ")?; - } - if target_info.approach == ApproachMode::Engaged { - write!(f, " approach")?; - } - if target_info.lnav == LNAV::Engaged { - write!(f, " lnav")?; - } - writeln!(f)?; - } else { - writeln!(f, " ACAS: NOT operational")?; - } - writeln!(f, " NACp: {}", target_info.nacp)?; - writeln!(f, " NICbaro: {}", target_info.nicbaro)?; - writeln!(f, " SIL: {} (per sample)", target_info.sil)?; - writeln!(f, " QNH: {} millibars", target_info.qnh)?; } ME::AircraftOperationalCoordination(_) => { - writeln!( - f, - " Extended Squitter{transponder}Aircraft Operational Coordination", + print_aircraft_operational_coordination_message( + &mut f, + transponder, + icao, + address_type, )?; - writeln!(f, " Address: {icao} {address_type}")?; } ME::AircraftOperationStatus(OperationStatus::Airborne(opstatus_airborne)) => { - let _ = print_operation_status_airborne( + print_operation_status_airborne( &mut f, transponder, icao, capability, address_type, opstatus_airborne, - ); + )?; } ME::AircraftOperationStatus(OperationStatus::Surface(opstatus_surface)) => { - let _ = print_operation_status_surface( + print_operation_status_surface( &mut f, transponder, icao, capability, address_type, opstatus_surface, - ); + )?; } ME::AircraftOperationStatus(OperationStatus::Reserved(..)) => { - let _ = print_operation_status_reserved(&mut f, transponder, icao, address_type); + print_operation_status_reserved(&mut f, transponder, icao, address_type)?; } } Ok(f) } } +fn print_aircraft_status( + f: &mut String, + transponder: &str, + icao: ICAO, + capability: Capability, + address_type: &str, + emergency_state: EmergencyState, + squawk: u32, +) -> Result<(), Error> { + writeln!( + f, + " Extended Squitter{transponder}Emergency/priority status", + )?; + writeln!(f, " Address: {icao} {address_type}")?; + writeln!(f, " Air/Ground: {capability}")?; + writeln!(f, " Squawk: {squawk:02X?}")?; + writeln!(f, " Emergency/priority: {emergency_state}")?; + + Ok(()) +} + +fn print_target_state_and_status_information( + f: &mut String, + transponder: &str, + icao: ICAO, + capability: Capability, + address_type: &str, + target_info: &TargetStateAndStatusInformation, +) -> Result<(), Error> { + writeln!( + f, + " Extended Squitter{transponder}Target state and status (V2)", + )?; + writeln!(f, " Address: {icao} {address_type}")?; + writeln!(f, " Air/Ground: {capability}")?; + writeln!(f, " Target State and Status:")?; + writeln!(f, " Target altitude: MCP, {} ft", target_info.altitude)?; + writeln!(f, " Altimeter setting: {} millibars", target_info.qnh)?; + if target_info.is_heading == SelectedHeadingStatus::Valid { + writeln!(f, " Target heading: {}", target_info.heading)?; + } + if target_info.tcas == TCAS::Engaged { + write!(f, " ACAS: operational ")?; + if target_info.autopilot == AutopilotEngaged::Engaged { + write!(f, "autopilot ")?; + } + if target_info.vnac == VNAVEngaged::Engaged { + write!(f, "vnav ")?; + } + if target_info.alt_hold == AltitudeHold::Engaged { + write!(f, "altitude-hold ")?; + } + if target_info.approach == ApproachMode::Engaged { + write!(f, " approach")?; + } + if target_info.lnav == LNAV::Engaged { + write!(f, " lnav")?; + } + writeln!(f)?; + } else { + writeln!(f, " ACAS: NOT operational")?; + } + writeln!(f, " NACp: {}", target_info.nacp)?; + writeln!(f, " NICbaro: {}", target_info.nicbaro)?; + writeln!(f, " SIL: {} (per sample)", target_info.sil)?; + writeln!(f, " QNH: {} millibars", target_info.qnh)?; + + Ok(()) +} + +fn print_aircraft_operational_coordination_message( + f: &mut String, + transponder: &str, + icao: ICAO, + address_type: &str, +) -> Result<(), Error> { + writeln!( + f, + " Extended Squitter{transponder}Aircraft Operational Coordination", + )?; + writeln!(f, " Address: {icao} {address_type}")?; + + Ok(()) +} + fn print_operation_status_airborne( f: &mut String, transponder: &str, diff --git a/src/helpers/encode_adsb_beast_input.rs b/src/helpers/encode_adsb_beast_input.rs index 7b195dd..8e2e366 100644 --- a/src/helpers/encode_adsb_beast_input.rs +++ b/src/helpers/encode_adsb_beast_input.rs @@ -45,6 +45,8 @@ enum FrameType { /// Returns a vector of bytes, with each element of the array being a frame that can be passed in to the ADSB Beast parser. #[must_use] +// FIXME: Can/should this be refactored in to less lines? +#[allow(clippy::too_many_lines)] pub fn format_adsb_beast_frames_from_bytes(bytes: &[u8]) -> ADSBBeastFrames { let mut formatted_frames: Vec> = Vec::new(); let mut leftbytes: Vec = Vec::new(); diff --git a/src/state_machine/state.rs b/src/state_machine/state.rs index b202c3f..85f3d48 100644 --- a/src/state_machine/state.rs +++ b/src/state_machine/state.rs @@ -241,9 +241,10 @@ impl Machine { } pub async fn print_airplane_by_hex(&self, transponder_hex: &str) { - match self.get_airplane_by_hex(transponder_hex).await { - Some(airplane) => info!("{airplane}"), - None => error!("No airplane found with transponder hex {transponder_hex}"), + if let Some(airplane) = self.get_airplane_by_hex(transponder_hex).await { + info!("{airplane}"); + } else { + error!("No airplane found with transponder hex {transponder_hex}"); } }