squitter-cli 0.1.1

Command-line decoder for 1090ES/DF17 (ADS-B extended squitter) hex frames, built on squitter
//! Human-readable printing of decoded ADS-B messages.

use squitter::message::Altitude;
use squitter::{AdsbMessage, Frame};

/// Formats an optional float to one decimal place, or `n/a`.
fn opt_f64(v: Option<f64>) -> String {
    v.map_or_else(|| "n/a".to_string(), |v| format!("{v:.1}"))
}

pub fn print_message(frame: &Frame, message: &AdsbMessage) {
    let icao = frame.icao_address();
    match message {
        AdsbMessage::Identification(m) => {
            println!(
                "icao={icao:06X} type=identification tc={} category={} callsign={}",
                m.type_code, m.category, m.callsign
            );
        }
        AdsbMessage::SurfacePosition(m) => {
            println!(
                "icao={icao:06X} type=surface_position tc={} speed={}kn track={}deg lat_cpr={} lon_cpr={} fmt={:?}",
                m.type_code,
                opt_f64(m.ground_speed_knots()),
                opt_f64(m.track_degrees()),
                m.lat_cpr,
                m.lon_cpr,
                m.cpr_format,
            );
        }
        AdsbMessage::AirbornePosition(m) => {
            let altitude = match m.altitude {
                Altitude::BarometricFeet(ft) => format!("{ft}ft (baro)"),
                Altitude::GnssMeters(meters) => format!("{meters}m (gnss)"),
                Altitude::Gillham(raw) => format!("gillham({raw})"),
                Altitude::NotAvailable => "n/a".to_string(),
            };
            println!(
                "icao={icao:06X} type=airborne_position tc={} altitude={altitude} lat_cpr={} lon_cpr={} fmt={:?}",
                m.type_code, m.lat_cpr, m.lon_cpr, m.cpr_format,
            );
        }
        AdsbMessage::AirborneVelocity(m) => {
            println!(
                "icao={icao:06X} type=airborne_velocity subtype={} groundspeed={}kn vrate={:?}fpm",
                m.subtype,
                opt_f64(m.ground_speed_knots()),
                m.vertical_rate_fpm,
            );
        }
        other => println!("icao={icao:06X}: printing not yet implemented for {other:?}"),
    }
}