vatsim-api 0.1.0

Rust types, async client, and mock server for various VATSIM APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! Core types shared across VATSIM APIs.
//!
//! This module contains enums and newtypes that appear in multiple VATSIM
//! endpoints: [`CertificateId`], [`Facility`], [`ControllerRating`],
//! [`PilotRating`], and [`MilitaryRating`]. Endpoint-specific structs live in
//! the [`datafeed`] and [`slurper`] submodules.

pub mod connect;
pub mod datafeed;
pub mod slurper;

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// A VATSIM user's certificate ID (CID), a unique numeric identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct CertificateId(u32);

/// ATC facility type as used in the VATSIM data feed and callsign suffixes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum Facility {
    /// Observer, `OBS`, data feed ID 0. Also the fallback for unrecognized
    /// callsign suffixes.
    #[default]
    Observer,
    /// Ramp control, `RMP`. Has no data feed ID.
    Ramp,
    /// Clearance delivery, `DEL`, data feed ID 2.
    ClearanceDelivery,
    /// Ground, `GND`, data feed ID 3.
    Ground,
    /// Tower, `TWR`, data feed ID 4.
    Tower,
    /// Approach, `APP`, data feed ID 5. The data feed does not distinguish
    /// approach from departure, so an ID of 5 always maps here.
    Approach,
    /// Departure, `DEP`. Has no data feed ID; see [`Facility::Approach`].
    Departure,
    /// Enroute (area control center), `CTR`, data feed ID 6.
    Enroute,
    /// Flight service station, `FSS`, data feed ID 1.
    FlightServiceStation,
    /// Radio, `RDO`. Has no data feed ID.
    Radio,
    /// Traffic flow or traffic management, `FMP`. Has no data feed ID.
    TrafficFlow,
}

/// VATSIM ATC controller rating, from inactive/suspended through administrator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum ControllerRating {
    /// Inactive, `INAC`, ID -1.
    #[default]
    Inactive,
    /// Suspended, `SUS`, ID 0.
    Suspended,
    /// Observer, `OBS`, ID 1.
    Observer,
    /// Tower trainee, `S1`, ID 2.
    TowerTrainee,
    /// Tower controller, `S2`, ID 3.
    TowerController,
    /// Senior student, `S3`, ID 4.
    SeniorStudent,
    /// Enroute controller, `C1`, ID 5.
    EnrouteController,
    /// Controller 2, `C2`, ID 6.
    #[deprecated(note = "not actively in use on VATSIM")]
    Controller2,
    /// Senior controller, `C3`, ID 7.
    SeniorController,
    /// Instructor, `I1`, ID 8.
    Instructor,
    /// Instructor 2, `I2`, ID 9.
    #[deprecated(note = "not actively in use on VATSIM")]
    Instructor2,
    /// Senior instructor, `I3`, ID 10.
    SeniorInstructor,
    /// Supervisor, `SUP`, ID 11.
    Supervisor,
    /// Administrator, `ADM`, ID 12.
    Administrator,
}

/// VATSIM pilot rating, from basic member through flight examiner.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum PilotRating {
    /// Basic member, `NEW`, ID 0.
    #[default]
    BasicMember,
    /// Private pilot license, `PPL`, ID 1.
    PrivatePilotLicense,
    /// Instrument rating, `IR`, ID 3.
    InstrumentRating,
    /// Commercial multi-engine license, `CMEL`, ID 7.
    CommercialMultiEngineLicense,
    /// Airline transport pilot license, `ATPL`, ID 15.
    AirlineTransportPilotLicense,
    /// Flight instructor, `FI`, ID 31.
    FlightInstructor,
    /// Flight examiner, `FE`, ID 63.
    FlightExaminer,
}

/// VATSIM military pilot rating.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum MilitaryRating {
    /// No military rating, `M0`, ID 0.
    #[default]
    NoMilitaryRating,
    /// Military pilot license, `M1`, ID 1.
    MilitaryPilotLicense,
    /// Military instrument rating, `M2`, ID 3.
    MilitaryInstrumentRating,
    /// Military multi-engine rating, `M3`, ID 7.
    MilitaryMultiEngineRating,
    /// Military mission ready pilot, `M4`, ID 15.
    MilitaryMissionReadyPilot,
}

impl CertificateId {
    /// Creates a new [`CertificateId`] from a raw numeric ID.
    #[must_use]
    pub const fn new(id: u32) -> Self {
        Self(id)
    }

    /// Returns the underlying numeric ID.
    #[must_use]
    pub const fn get(self) -> u32 {
        self.0
    }
}

impl std::fmt::Display for CertificateId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl std::str::FromStr for CertificateId {
    type Err = std::num::ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(s.parse()?))
    }
}

impl From<u32> for CertificateId {
    fn from(id: u32) -> Self {
        Self(id)
    }
}

impl From<CertificateId> for u32 {
    fn from(id: CertificateId) -> Self {
        id.0
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for CertificateId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Clone, Copy)]
        struct CertificateIdVisitor;
        impl serde::de::Visitor<'_> for CertificateIdVisitor {
            type Value = CertificateId;
            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("a CID as a string or integer")
            }
            fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<CertificateId, E> {
                u32::try_from(v)
                    .map(CertificateId::new)
                    .map_err(|_| E::invalid_value(serde::de::Unexpected::Signed(v), &self))
            }
            fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<CertificateId, E> {
                u32::try_from(v)
                    .map(CertificateId::new)
                    .map_err(|_| E::invalid_value(serde::de::Unexpected::Unsigned(v), &self))
            }
            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<CertificateId, E> {
                v.parse().map_err(E::custom)
            }
        }
        deserializer.deserialize_any(CertificateIdVisitor)
    }
}

impl Facility {
    /// All active facility variants (excludes no deprecated variants).
    pub const ALL: &'static [Self] = &[
        Self::Observer,
        Self::Ramp,
        Self::ClearanceDelivery,
        Self::Ground,
        Self::Tower,
        Self::Approach,
        Self::Departure,
        Self::Enroute,
        Self::FlightServiceStation,
        Self::Radio,
        Self::TrafficFlow,
    ];

    /// Returns the numeric ID the VATSIM data feed uses for this facility.
    ///
    /// The data feed has IDs for only seven facility types, so the four
    /// variants without one map to their closest equivalent:
    /// [`Departure`](Self::Departure) to [`Approach`](Self::Approach),
    /// [`Ramp`](Self::Ramp) to [`Ground`](Self::Ground),
    /// [`Radio`](Self::Radio) to
    /// [`FlightServiceStation`](Self::FlightServiceStation), and
    /// [`TrafficFlow`](Self::TrafficFlow) to [`Observer`](Self::Observer).
    /// Those four therefore do not survive a round trip through an ID.
    ///
    /// [`as_str`](Self::as_str) is lossless and is what this crate's
    /// [`Serialize`] implementation uses.
    #[must_use]
    pub const fn as_id(self) -> i8 {
        match self {
            Facility::Observer | Facility::TrafficFlow => 0,
            Facility::FlightServiceStation | Facility::Radio => 1,
            Facility::ClearanceDelivery => 2,
            Facility::Ground | Facility::Ramp => 3,
            Facility::Tower => 4,
            Facility::Approach | Facility::Departure => 5,
            Facility::Enroute => 6,
        }
    }

    /// Returns the short string code for this facility (e.g. `"TWR"`, `"APP"`).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Facility::Observer => "OBS",
            Facility::Ramp => "RMP",
            Facility::ClearanceDelivery => "DEL",
            Facility::Ground => "GND",
            Facility::Tower => "TWR",
            Facility::Approach => "APP",
            Facility::Departure => "DEP",
            Facility::Enroute => "CTR",
            Facility::FlightServiceStation => "FSS",
            Facility::Radio => "RDO",
            Facility::TrafficFlow => "FMP",
        }
    }

    /// Guesses the [`Facility`] from a callsign suffix (e.g. `"LOWW_TWR"` -> [`Facility::Tower`]).
    ///
    /// Returns [`Facility::Observer`] if the suffix is not recognized.
    #[must_use]
    pub fn from_callsign(callsign: impl AsRef<str>) -> Self {
        let facility_suffix = callsign.as_ref().split('_').next_back().unwrap_or_default();
        Facility::try_from(facility_suffix).unwrap_or_default()
    }
}

impl std::fmt::Display for Facility {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for Facility {
    type Err = crate::error::ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.to_ascii_uppercase();
        match s.as_str() {
            "OBS" | "OBSERVER" => Ok(Facility::Observer),
            "RMP" | "RAMP" => Ok(Facility::Ramp),
            "DEL" | "DELIVERY" | "CLEARANCE DELIVERY" => Ok(Facility::ClearanceDelivery),
            "GND" | "GROUND" => Ok(Facility::Ground),
            "TWR" | "TOWER" => Ok(Facility::Tower),
            "APP" | "APPROACH" | "APPROACH/DEPARTURE" => Ok(Facility::Approach),
            "DEP" | "DEPARTURE" => Ok(Facility::Departure),
            "CTR" | "CENTER" | "CENTRE" | "ENROUTE" => Ok(Facility::Enroute),
            "FSS" | "FLIGHTSERVICESTATION" | "FLIGHT SERVICE STATION" => {
                Ok(Facility::FlightServiceStation)
            }
            "RDO" | "RADIO" => Ok(Facility::Radio),
            "TMU"
            | "TRAFFICMANAGEMENTUNIT"
            | "TRAFFIC MANAGEMENT UNIT"
            | "FMP"
            | "FLOWMANAGEMENTPOSITION"
            | "FLOW MANAGEMENT POSITION"
            | "TRAFFICFLOW"
            | "TRAFFIC FLOW" => Ok(Facility::TrafficFlow),
            other => Err(Self::Err::UnknownValue {
                kind: "facility",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<&str> for Facility {
    type Error = crate::error::ParseError;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

impl TryFrom<String> for Facility {
    type Error = crate::error::ParseError;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        value.as_str().parse()
    }
}

impl TryFrom<i8> for Facility {
    type Error = crate::error::ParseError;
    fn try_from(value: i8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Facility::Observer),
            1 => Ok(Facility::FlightServiceStation),
            2 => Ok(Facility::ClearanceDelivery),
            3 => Ok(Facility::Ground),
            4 => Ok(Facility::Tower),
            5 => Ok(Facility::Approach),
            6 => Ok(Facility::Enroute),
            other => Err(Self::Error::UnknownValue {
                kind: "facility",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<datafeed::FacilityInfo> for Facility {
    type Error = crate::error::ParseError;
    fn try_from(value: datafeed::FacilityInfo) -> Result<Self, Self::Error> {
        value.id.try_into()
    }
}

#[cfg(feature = "serde")]
impl Serialize for Facility {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Facility {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Clone, Copy)]
        struct FacilityVisitor;
        impl serde::de::Visitor<'_> for FacilityVisitor {
            type Value = Facility;
            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("a facility name string or integer ID")
            }
            fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<Facility, E> {
                i8::try_from(v)
                    .map_err(|_| E::invalid_value(serde::de::Unexpected::Signed(v), &self))
                    .and_then(|id| Facility::try_from(id).map_err(|e| E::custom(e)))
            }
            fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<Facility, E> {
                self.visit_i64(
                    i64::try_from(v)
                        .map_err(|_| E::invalid_value(serde::de::Unexpected::Unsigned(v), &self))?,
                )
            }
            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Facility, E> {
                v.parse().map_err(E::custom)
            }
        }
        deserializer.deserialize_any(FacilityVisitor)
    }
}

impl ControllerRating {
    /// All active controller rating variants (excludes deprecated ones).
    pub const ALL: &'static [Self] = &[
        Self::Inactive,
        Self::Suspended,
        Self::Observer,
        Self::TowerTrainee,
        Self::TowerController,
        Self::SeniorStudent,
        Self::EnrouteController,
        Self::SeniorController,
        Self::Instructor,
        Self::SeniorInstructor,
        Self::Supervisor,
        Self::Administrator,
    ];

    /// Returns the short string code for this rating (e.g. `"S1"`, `"C1"`).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            ControllerRating::Inactive => "INAC",
            ControllerRating::Suspended => "SUS",
            ControllerRating::Observer => "OBS",
            ControllerRating::TowerTrainee => "S1",
            ControllerRating::TowerController => "S2",
            ControllerRating::SeniorStudent => "S3",
            ControllerRating::EnrouteController => "C1",
            #[allow(deprecated)]
            ControllerRating::Controller2 => "C2",
            ControllerRating::SeniorController => "C3",
            ControllerRating::Instructor => "I1",
            #[allow(deprecated)]
            ControllerRating::Instructor2 => "I2",
            ControllerRating::SeniorInstructor => "I3",
            ControllerRating::Supervisor => "SUP",
            ControllerRating::Administrator => "ADM",
        }
    }

    /// Returns the numeric ID used in the VATSIM data feed for this rating.
    #[must_use]
    pub const fn as_id(self) -> i8 {
        match self {
            ControllerRating::Inactive => -1,
            ControllerRating::Suspended => 0,
            ControllerRating::Observer => 1,
            ControllerRating::TowerTrainee => 2,
            ControllerRating::TowerController => 3,
            ControllerRating::SeniorStudent => 4,
            ControllerRating::EnrouteController => 5,
            #[allow(deprecated)]
            ControllerRating::Controller2 => 6,
            ControllerRating::SeniorController => 7,
            ControllerRating::Instructor => 8,
            #[allow(deprecated)]
            ControllerRating::Instructor2 => 9,
            ControllerRating::SeniorInstructor => 10,
            ControllerRating::Supervisor => 11,
            ControllerRating::Administrator => 12,
        }
    }
}

impl std::str::FromStr for ControllerRating {
    type Err = crate::error::ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.to_ascii_uppercase();
        match s.as_str() {
            "INAC" | "INACTIVE" => Ok(ControllerRating::Inactive),
            "SUS" | "SUSPENDED" => Ok(ControllerRating::Suspended),
            "OBS" | "OBSERVER" => Ok(ControllerRating::Observer),
            "S1" | "TOWER TRAINEE" => Ok(ControllerRating::TowerTrainee),
            "S2" | "TOWER CONTROLLER" => Ok(ControllerRating::TowerController),
            "S3" | "SENIOR STUDENT" => Ok(ControllerRating::SeniorStudent),
            "C1" | "ENROUTE CONTROLLER" => Ok(ControllerRating::EnrouteController),
            #[allow(deprecated)]
            "C2" | "CONTROLLER 2 (NOT IN USE)" => Ok(ControllerRating::Controller2),
            "C3" | "SENIOR CONTROLLER" => Ok(ControllerRating::SeniorController),
            "I1" | "INSTRUCTOR" => Ok(ControllerRating::Instructor),
            #[allow(deprecated)]
            "I2" | "INSTRUCTOR 2 (NOT IN USE)" => Ok(ControllerRating::Instructor2),
            "I3" | "SENIOR INSTRUCTOR" => Ok(ControllerRating::SeniorInstructor),
            "SUP" | "SUPERVISOR" => Ok(ControllerRating::Supervisor),
            "ADM" | "ADMINISTRATOR" => Ok(ControllerRating::Administrator),
            other => Err(Self::Err::UnknownValue {
                kind: "controller rating",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<&str> for ControllerRating {
    type Error = crate::error::ParseError;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

impl TryFrom<String> for ControllerRating {
    type Error = crate::error::ParseError;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        value.as_str().parse()
    }
}

impl TryFrom<i8> for ControllerRating {
    type Error = crate::error::ParseError;
    fn try_from(value: i8) -> Result<Self, Self::Error> {
        match value {
            -1 => Ok(ControllerRating::Inactive),
            0 => Ok(ControllerRating::Suspended),
            1 => Ok(ControllerRating::Observer),
            2 => Ok(ControllerRating::TowerTrainee),
            3 => Ok(ControllerRating::TowerController),
            4 => Ok(ControllerRating::SeniorStudent),
            5 => Ok(ControllerRating::EnrouteController),
            #[allow(deprecated)]
            6 => Ok(ControllerRating::Controller2),
            7 => Ok(ControllerRating::SeniorController),
            8 => Ok(ControllerRating::Instructor),
            #[allow(deprecated)]
            9 => Ok(ControllerRating::Instructor2),
            10 => Ok(ControllerRating::SeniorInstructor),
            11 => Ok(ControllerRating::Supervisor),
            12 => Ok(ControllerRating::Administrator),
            other => Err(Self::Error::UnknownValue {
                kind: "controller rating",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<datafeed::RatingInfo> for ControllerRating {
    type Error = crate::error::ParseError;
    fn try_from(value: datafeed::RatingInfo) -> Result<Self, Self::Error> {
        value.id.try_into()
    }
}

#[cfg(feature = "serde")]
impl Serialize for ControllerRating {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for ControllerRating {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Clone, Copy)]
        struct RatingVisitor;
        impl serde::de::Visitor<'_> for RatingVisitor {
            type Value = ControllerRating;
            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("a controller rating string or integer ID")
            }
            fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<ControllerRating, E> {
                i8::try_from(v)
                    .map_err(|_| E::invalid_value(serde::de::Unexpected::Signed(v), &self))
                    .and_then(|id| ControllerRating::try_from(id).map_err(|e| E::custom(e)))
            }
            fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<ControllerRating, E> {
                self.visit_i64(
                    i64::try_from(v)
                        .map_err(|_| E::invalid_value(serde::de::Unexpected::Unsigned(v), &self))?,
                )
            }
            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<ControllerRating, E> {
                v.parse().map_err(E::custom)
            }
        }
        deserializer.deserialize_any(RatingVisitor)
    }
}

impl PilotRating {
    /// All pilot rating variants.
    pub const ALL: &'static [Self] = &[
        Self::BasicMember,
        Self::PrivatePilotLicense,
        Self::InstrumentRating,
        Self::CommercialMultiEngineLicense,
        Self::AirlineTransportPilotLicense,
        Self::FlightInstructor,
        Self::FlightExaminer,
    ];

    /// Returns the numeric ID the VATSIM data feed uses for this rating.
    ///
    /// Pilot rating IDs are a bit field, so they run 0, 1, 3, 7, 15, 31, 63
    /// rather than consecutively.
    #[must_use]
    pub const fn as_id(self) -> i8 {
        match self {
            PilotRating::BasicMember => 0,
            PilotRating::PrivatePilotLicense => 1,
            PilotRating::InstrumentRating => 3,
            PilotRating::CommercialMultiEngineLicense => 7,
            PilotRating::AirlineTransportPilotLicense => 15,
            PilotRating::FlightInstructor => 31,
            PilotRating::FlightExaminer => 63,
        }
    }

    /// Returns the short string code for this rating (e.g. `"PPL"`, `"ATPL"`).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            PilotRating::BasicMember => "NEW",
            PilotRating::PrivatePilotLicense => "PPL",
            PilotRating::InstrumentRating => "IR",
            PilotRating::CommercialMultiEngineLicense => "CMEL",
            PilotRating::AirlineTransportPilotLicense => "ATPL",
            PilotRating::FlightInstructor => "FI",
            PilotRating::FlightExaminer => "FE",
        }
    }
}

impl std::str::FromStr for PilotRating {
    type Err = crate::error::ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.to_ascii_uppercase();
        match s.as_str() {
            "NEW" | "BASIC MEMBER" => Ok(PilotRating::BasicMember),
            "PPL" | "PRIVATE PILOT LICENSE" => Ok(PilotRating::PrivatePilotLicense),
            "IR" | "INSTRUMENT RATING" => Ok(PilotRating::InstrumentRating),
            "CMEL" | "COMMERCIAL MULTI-ENGINE LICENSE" => {
                Ok(PilotRating::CommercialMultiEngineLicense)
            }
            "ATPL" | "AIRLINE TRANSPORT PILOT LICENSE" => {
                Ok(PilotRating::AirlineTransportPilotLicense)
            }
            "FI" | "FLIGHT INSTRUCTOR" => Ok(PilotRating::FlightInstructor),
            "FE" | "FLIGHT EXAMINER" => Ok(PilotRating::FlightExaminer),
            other => Err(Self::Err::UnknownValue {
                kind: "pilot rating",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<&str> for PilotRating {
    type Error = crate::error::ParseError;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

impl TryFrom<String> for PilotRating {
    type Error = crate::error::ParseError;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        value.as_str().parse()
    }
}

impl TryFrom<i8> for PilotRating {
    type Error = crate::error::ParseError;
    fn try_from(value: i8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(PilotRating::BasicMember),
            1 => Ok(PilotRating::PrivatePilotLicense),
            3 => Ok(PilotRating::InstrumentRating),
            7 => Ok(PilotRating::CommercialMultiEngineLicense),
            15 => Ok(PilotRating::AirlineTransportPilotLicense),
            31 => Ok(PilotRating::FlightInstructor),
            63 => Ok(PilotRating::FlightExaminer),
            other => Err(Self::Error::UnknownValue {
                kind: "pilot rating",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<datafeed::PilotRatingInfo> for PilotRating {
    type Error = crate::error::ParseError;
    fn try_from(value: datafeed::PilotRatingInfo) -> Result<Self, Self::Error> {
        value.id.try_into()
    }
}

#[cfg(feature = "serde")]
impl Serialize for PilotRating {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for PilotRating {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Clone, Copy)]
        struct RatingVisitor;
        impl serde::de::Visitor<'_> for RatingVisitor {
            type Value = PilotRating;
            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("a pilot rating string or integer ID")
            }
            fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<PilotRating, E> {
                i8::try_from(v)
                    .map_err(|_| E::invalid_value(serde::de::Unexpected::Signed(v), &self))
                    .and_then(|id| PilotRating::try_from(id).map_err(|e| E::custom(e)))
            }
            fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<PilotRating, E> {
                self.visit_i64(
                    i64::try_from(v)
                        .map_err(|_| E::invalid_value(serde::de::Unexpected::Unsigned(v), &self))?,
                )
            }
            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<PilotRating, E> {
                v.parse().map_err(E::custom)
            }
        }
        deserializer.deserialize_any(RatingVisitor)
    }
}

impl MilitaryRating {
    /// All military rating variants.
    pub const ALL: &'static [Self] = &[
        Self::NoMilitaryRating,
        Self::MilitaryPilotLicense,
        Self::MilitaryInstrumentRating,
        Self::MilitaryMultiEngineRating,
        Self::MilitaryMissionReadyPilot,
    ];

    /// Returns the numeric ID the VATSIM data feed uses for this rating.
    ///
    /// Military rating IDs are a bit field, so they run 0, 1, 3, 7, 15 rather
    /// than consecutively.
    #[must_use]
    pub const fn as_id(self) -> i8 {
        match self {
            MilitaryRating::NoMilitaryRating => 0,
            MilitaryRating::MilitaryPilotLicense => 1,
            MilitaryRating::MilitaryInstrumentRating => 3,
            MilitaryRating::MilitaryMultiEngineRating => 7,
            MilitaryRating::MilitaryMissionReadyPilot => 15,
        }
    }

    /// Returns the short string code for this rating (e.g. `"M0"`, `"M4"`).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            MilitaryRating::NoMilitaryRating => "M0",
            MilitaryRating::MilitaryPilotLicense => "M1",
            MilitaryRating::MilitaryInstrumentRating => "M2",
            MilitaryRating::MilitaryMultiEngineRating => "M3",
            MilitaryRating::MilitaryMissionReadyPilot => "M4",
        }
    }
}

impl std::str::FromStr for MilitaryRating {
    type Err = crate::error::ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.to_ascii_uppercase();
        match s.as_str() {
            "M0" | "NO MILITARY RATING" => Ok(MilitaryRating::NoMilitaryRating),
            "M1" | "MILITARY PILOT LICENSE" => Ok(MilitaryRating::MilitaryPilotLicense),
            "M2" | "MILITARY INSTRUMENT RATING" => Ok(MilitaryRating::MilitaryInstrumentRating),
            "M3" | "MILITARY MULTI-ENGINE RATING" => Ok(MilitaryRating::MilitaryMultiEngineRating),
            "M4" | "MILITARY MISSION READY PILOT" => Ok(MilitaryRating::MilitaryMissionReadyPilot),
            other => Err(Self::Err::UnknownValue {
                kind: "military rating",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<&str> for MilitaryRating {
    type Error = crate::error::ParseError;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

impl TryFrom<String> for MilitaryRating {
    type Error = crate::error::ParseError;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        value.as_str().parse()
    }
}

impl TryFrom<i8> for MilitaryRating {
    type Error = crate::error::ParseError;
    fn try_from(value: i8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(MilitaryRating::NoMilitaryRating),
            1 => Ok(MilitaryRating::MilitaryPilotLicense),
            3 => Ok(MilitaryRating::MilitaryInstrumentRating),
            7 => Ok(MilitaryRating::MilitaryMultiEngineRating),
            15 => Ok(MilitaryRating::MilitaryMissionReadyPilot),
            other => Err(Self::Error::UnknownValue {
                kind: "military rating",
                value: other.to_string(),
            }),
        }
    }
}

impl TryFrom<datafeed::MilitaryRatingInfo> for MilitaryRating {
    type Error = crate::error::ParseError;
    fn try_from(value: datafeed::MilitaryRatingInfo) -> Result<Self, Self::Error> {
        value.id.try_into()
    }
}

#[cfg(feature = "serde")]
impl Serialize for MilitaryRating {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for MilitaryRating {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Clone, Copy)]
        struct RatingVisitor;
        impl serde::de::Visitor<'_> for RatingVisitor {
            type Value = MilitaryRating;
            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("a military rating string or integer ID")
            }
            fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<MilitaryRating, E> {
                i8::try_from(v)
                    .map_err(|_| E::invalid_value(serde::de::Unexpected::Signed(v), &self))
                    .and_then(|id| MilitaryRating::try_from(id).map_err(|e| E::custom(e)))
            }
            fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<MilitaryRating, E> {
                self.visit_i64(
                    i64::try_from(v)
                        .map_err(|_| E::invalid_value(serde::de::Unexpected::Unsigned(v), &self))?,
                )
            }
            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<MilitaryRating, E> {
                v.parse().map_err(E::custom)
            }
        }
        deserializer.deserialize_any(RatingVisitor)
    }
}