asmarques / adsb

A Rust parser for ADS-B/Mode-S messages.
Apache License 2.0
17 stars 5 forks source link

Improve ergonomics of (*)MessageKind's #6

Open Snowda opened 1 year ago

Snowda commented 1 year ago

Due to how the variants of MessageKind and ADSBMessageKind are built, they are pretty easy to create, but from a user of the API's perspective, due to the individual variants being Enums, they cannot be typed and assigned to new variables.

I'd suggest to convert messageKinds into individual types, but they consumes a common trait that can still be extended using impl on both of these types for creation purposes. This is particularly impactful on the enum variants that have highly differing contents and sub-fields. By implementing this, in theory you could then the following which is not really possible as Enums aren't types:

let packet = parse_avr(&input).unwrap();
let adsb: ADSBMessage = packet.kind;
let postition: Position = adsb.kind.cpr_frame.position;
asmarques commented 1 year ago

Unfortunately rust doesn't support enum variants as types and it looks like this won't be changing anytime soon (rust-lang/lang-team#122).

Another alternative to improve the ergonomics of assigning message types would be to convert the anonymous struct variants into tuple variants containing a named struct:

struct AirbornePosition {
    altitude: u16,
    cpr_frame: CPRFrame,
}

pub enum ADSBMessageKind {
    ...
    AirbornePosition(AirbornePosition),
    ...
}

Despite the more verbose syntax for matching/creating, would this help your use case?