vin-decode 0.3.1

Auto-updating VIN decoder backed by NHTSA vPIC, refreshed monthly via CI
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
use std::fmt;

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

/// A validated 17-character VIN.
///
/// Construction enforces length, ASCII-alphanumeric chars, and the I/O/Q ban.
/// Check-digit validation is separate (see [`crate::Decoder::decode`]).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Vin(String);

impl Vin {
    /// Parse a raw VIN string. Uppercases ASCII; rejects bad length/chars.
    pub fn new(raw: impl Into<String>) -> crate::Result<Self> {
        let s = raw.into().to_ascii_uppercase();
        crate::wmi::validate_chars(&s)?;
        Ok(Vin(s))
    }

    /// Borrow as canonical (uppercase) string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// World Manufacturer Identifier — first 3 chars.
    pub fn wmi(&self) -> &str {
        &self.0[..3]
    }

    /// Vehicle Descriptor Section — chars 4-9.
    pub fn vds(&self) -> &str {
        &self.0[3..9]
    }

    /// Vehicle Identifier Section — chars 10-17.
    pub fn vis(&self) -> &str {
        &self.0[9..]
    }

    /// Check digit at position 9.
    pub fn check_digit(&self) -> char {
        self.0.as_bytes()[8] as char
    }

    /// Model-year code at position 10.
    pub fn year_code(&self) -> char {
        self.0.as_bytes()[9] as char
    }

    /// Plant code at position 11.
    pub fn plant_code(&self) -> char {
        self.0.as_bytes()[10] as char
    }

    /// Region code — first character (ISO 3779 region bucket).
    pub fn region_code(&self) -> char {
        self.0.as_bytes()[0] as char
    }

    /// Country code — first two characters (ISO 3779 country range).
    pub fn country_code(&self) -> &str {
        &self.0[..2]
    }

    /// Squish-VIN — the 10-char fingerprint used by some lookup tools:
    /// chars 1-8 + chars 10-11 (skipping the check digit at position 9).
    pub fn squish_vin(&self) -> String {
        let s = &self.0;
        let mut out = String::with_capacity(10);
        out.push_str(&s[..8]);
        out.push_str(&s[9..11]);
        out
    }

    /// Both possible model-year candidates from the VIN's pos-10 year code.
    ///
    /// SAE-J853 reuses each letter twice (30-year cycle), so a code like
    /// `'F'` maps to both 1985 and 2015. This returns both. Numeric codes
    /// `1`-`9` only ever map to a single year (2001-2009) since the second
    /// digit cycle (2031-2039) hasn't started. Returns an empty vec for
    /// unreadable codes (`I`/`O`/`Q`/`U`/`Z`/`0`).
    ///
    /// Note: many manufacturers don't follow the SAE-J853 pos-10 convention
    /// (modern Mercedes encodes year in the chassis serial; some Renault /
    /// Dacia families use other positions; Ford EU uses pos-11). For those
    /// VINs this method may return candidates that don't include the actual
    /// model year. The decoder no longer auto-picks one — consumers can
    /// inspect the candidates and make their own call.
    pub fn year_candidates(&self) -> Vec<u32> {
        let code = self.year_code();
        let Some(base) = crate::year::year_for_code(code) else {
            return Vec::new();
        };
        if code.is_ascii_digit() {
            vec![base]
        } else {
            vec![base + 30, base]
        }
    }
}

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

/// Fully decoded vehicle attributes derived from a single VIN.
///
/// Every field is `Option<_>` — vPIC coverage is uneven, especially for
/// non-US-market vehicles. Always check what you got before unwrapping.
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Vehicle {
    /// Original VIN string (uppercase).
    pub vin: String,
    /// World Manufacturer Identifier (first 3 chars of the VIN).
    pub wmi: String,
    /// Make name (e.g. `"Honda"`).
    pub make: Option<String>,
    /// Model name (e.g. `"Civic"`).
    pub model: Option<String>,
    /// Series identifier (sometimes used as a finer model variant).
    pub series: Option<String>,
    /// Trim level / package.
    pub trim: Option<String>,
    /// Model year (1980-2039, decoded from year code + position-7 disambiguator).
    pub model_year: Option<u32>,
    /// Bodywork type, per the EU type-approval standard (see [`BodyType`]).
    pub body_type: Option<BodyType>,
    /// Primary fuel type.
    pub fuel_primary: Option<FuelType>,
    /// Secondary fuel type (set on hybrids, dual-fuel).
    pub fuel_secondary: Option<FuelType>,
    /// Door count.
    pub doors: Option<u8>,
    /// Engine cylinder count.
    pub engine_cylinders: Option<u8>,
    /// Engine model designation.
    pub engine_model: Option<String>,
    /// Engine configuration (e.g. `"V"`, `"In-Line"`).
    pub engine_configuration: Option<String>,
    /// Engine manufacturer.
    pub engine_manufacturer: Option<String>,
    /// Displacement in liters.
    pub displacement_l: Option<f32>,
    /// Whether the engine is turbocharged.
    pub turbo: Option<bool>,
    /// Drive type (e.g. `"FWD"`, `"AWD"`).
    pub drive_type: Option<String>,
    /// Transmission style.
    pub transmission: Option<String>,
    /// Battery type (EV / hybrid).
    pub battery_type: Option<String>,
    /// On-board charger level (EV).
    pub charger_level: Option<String>,
    /// EV drive unit configuration.
    pub ev_drive_unit: Option<String>,
    /// Brake system type.
    pub brake_system: Option<String>,
    /// Gross vehicle weight rating.
    pub gvwr: Option<String>,
    /// Plant country.
    pub plant_country: Option<String>,
    /// Plant city.
    pub plant_city: Option<String>,
    /// Plant state/province.
    pub plant_state: Option<String>,
    /// Manufacturer name (often differs from make for OEM/coachbuilders).
    pub manufacturer: Option<String>,
    /// Continental region derived from the first VIN character (Africa/Asia/Europe/etc).
    pub region: Option<String>,
}

/// Vehicle bodywork type, per the EU type-approval standard — the two-letter
/// codes from Commission Regulation (EU) 678/2011 (consolidated into the
/// framework Reg (EU) 2018/858), the same codes printed on the Certificate of
/// Conformity and recorded by EU national vehicle registers.
///
/// This is a *bodywork* classification, not a market segment. There is
/// deliberately **no `SUV` / `Crossover`** variant: the standard has none — an
/// SUV is type-approved as a [`BodyType::StationWagon`] (`AC`) or
/// [`BodyType::MultiPurpose`] (`AF`). Segment labelling (SUV, city car, …) is
/// an application-layer concern and is intentionally out of scope.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum BodyType {
    // Category M1 — passenger cars
    Saloon,            // AA
    Hatchback,         // AB
    StationWagon,      // AC
    Coupe,             // AD
    Convertible,       // AE
    MultiPurpose,      // AF
    TruckStationWagon, // AG
    // Category N — goods vehicles
    Lorry,       // BA
    Van,         // BB
    TractorUnit, // BC
    RoadTractor, // BD
    PickUp,      // BE
    ChassisCab,  // BX
    // Category M2/M3 — buses & coaches
    SingleDeckBus,                    // CA
    DoubleDeckBus,                    // CB
    ArticulatedSingleDeckBus,         // CC
    ArticulatedDoubleDeckBus,         // CD
    LowFloorSingleDeckBus,            // CE
    LowFloorDoubleDeckBus,            // CF
    ArticulatedLowFloorSingleDeckBus, // CG
    ArticulatedLowFloorDoubleDeckBus, // CH
    OpenTopSingleDeckBus,             // CI
    OpenTopDoubleDeckBus,             // CJ
    BusChassis,                       // CX
    // Category O — trailers
    SemiTrailer,         // DA
    DrawbarTrailer,      // DB
    CentreAxleTrailer,   // DC
    RigidDrawbarTrailer, // DE
    // Special-purpose vehicles
    MotorCaravan,           // SA
    ArmouredVehicle,        // SB
    Ambulance,              // SC
    Hearse,                 // SD
    TrailerCaravan,         // SE
    MobileCrane,            // SF
    SpecialGroup,           // SG
    WheelchairAccessible,   // SH
    ConverterDolly,         // SJ
    ExceptionalLoadTrailer, // SK
    /// Bodywork that maps to no EU type-approval code — e.g. a category L
    /// powered two-wheeler, or an unrecognised source string.
    Unknown,
}

/// Every standard (code-bearing) variant, in EU-code order. Excludes
/// [`BodyType::Unknown`], which is the absence of a code rather than a type.
pub(crate) const BODY_TYPES: &[BodyType] = &[
    BodyType::Saloon,
    BodyType::Hatchback,
    BodyType::StationWagon,
    BodyType::Coupe,
    BodyType::Convertible,
    BodyType::MultiPurpose,
    BodyType::TruckStationWagon,
    BodyType::Lorry,
    BodyType::Van,
    BodyType::TractorUnit,
    BodyType::RoadTractor,
    BodyType::PickUp,
    BodyType::ChassisCab,
    BodyType::SingleDeckBus,
    BodyType::DoubleDeckBus,
    BodyType::ArticulatedSingleDeckBus,
    BodyType::ArticulatedDoubleDeckBus,
    BodyType::LowFloorSingleDeckBus,
    BodyType::LowFloorDoubleDeckBus,
    BodyType::ArticulatedLowFloorSingleDeckBus,
    BodyType::ArticulatedLowFloorDoubleDeckBus,
    BodyType::OpenTopSingleDeckBus,
    BodyType::OpenTopDoubleDeckBus,
    BodyType::BusChassis,
    BodyType::SemiTrailer,
    BodyType::DrawbarTrailer,
    BodyType::CentreAxleTrailer,
    BodyType::RigidDrawbarTrailer,
    BodyType::MotorCaravan,
    BodyType::ArmouredVehicle,
    BodyType::Ambulance,
    BodyType::Hearse,
    BodyType::TrailerCaravan,
    BodyType::MobileCrane,
    BodyType::SpecialGroup,
    BodyType::WheelchairAccessible,
    BodyType::ConverterDolly,
    BodyType::ExceptionalLoadTrailer,
];

impl BodyType {
    /// The canonical two-letter EU type-approval bodywork code (`"AA"`, `"BB"`,
    /// …). [`BodyType::Unknown`] has no code and returns `""`.
    pub fn code(self) -> &'static str {
        match self {
            BodyType::Saloon => "AA",
            BodyType::Hatchback => "AB",
            BodyType::StationWagon => "AC",
            BodyType::Coupe => "AD",
            BodyType::Convertible => "AE",
            BodyType::MultiPurpose => "AF",
            BodyType::TruckStationWagon => "AG",
            BodyType::Lorry => "BA",
            BodyType::Van => "BB",
            BodyType::TractorUnit => "BC",
            BodyType::RoadTractor => "BD",
            BodyType::PickUp => "BE",
            BodyType::ChassisCab => "BX",
            BodyType::SingleDeckBus => "CA",
            BodyType::DoubleDeckBus => "CB",
            BodyType::ArticulatedSingleDeckBus => "CC",
            BodyType::ArticulatedDoubleDeckBus => "CD",
            BodyType::LowFloorSingleDeckBus => "CE",
            BodyType::LowFloorDoubleDeckBus => "CF",
            BodyType::ArticulatedLowFloorSingleDeckBus => "CG",
            BodyType::ArticulatedLowFloorDoubleDeckBus => "CH",
            BodyType::OpenTopSingleDeckBus => "CI",
            BodyType::OpenTopDoubleDeckBus => "CJ",
            BodyType::BusChassis => "CX",
            BodyType::SemiTrailer => "DA",
            BodyType::DrawbarTrailer => "DB",
            BodyType::CentreAxleTrailer => "DC",
            BodyType::RigidDrawbarTrailer => "DE",
            BodyType::MotorCaravan => "SA",
            BodyType::ArmouredVehicle => "SB",
            BodyType::Ambulance => "SC",
            BodyType::Hearse => "SD",
            BodyType::TrailerCaravan => "SE",
            BodyType::MobileCrane => "SF",
            BodyType::SpecialGroup => "SG",
            BodyType::WheelchairAccessible => "SH",
            BodyType::ConverterDolly => "SJ",
            BodyType::ExceptionalLoadTrailer => "SK",
            BodyType::Unknown => "",
        }
    }

    /// Map a two-letter EU type-approval code (case-insensitive) onto a variant.
    /// Returns `None` for an unrecognised code. This is the exact path for
    /// registry data that already carries the standard code (e.g. RDW
    /// `carrosserietype`).
    pub fn from_code(code: &str) -> Option<Self> {
        Some(match code.trim().to_ascii_uppercase().as_str() {
            "AA" => BodyType::Saloon,
            "AB" => BodyType::Hatchback,
            "AC" => BodyType::StationWagon,
            "AD" => BodyType::Coupe,
            "AE" => BodyType::Convertible,
            "AF" => BodyType::MultiPurpose,
            "AG" => BodyType::TruckStationWagon,
            "BA" => BodyType::Lorry,
            "BB" => BodyType::Van,
            "BC" => BodyType::TractorUnit,
            "BD" => BodyType::RoadTractor,
            "BE" => BodyType::PickUp,
            "BX" => BodyType::ChassisCab,
            "CA" => BodyType::SingleDeckBus,
            "CB" => BodyType::DoubleDeckBus,
            "CC" => BodyType::ArticulatedSingleDeckBus,
            "CD" => BodyType::ArticulatedDoubleDeckBus,
            "CE" => BodyType::LowFloorSingleDeckBus,
            "CF" => BodyType::LowFloorDoubleDeckBus,
            "CG" => BodyType::ArticulatedLowFloorSingleDeckBus,
            "CH" => BodyType::ArticulatedLowFloorDoubleDeckBus,
            "CI" => BodyType::OpenTopSingleDeckBus,
            "CJ" => BodyType::OpenTopDoubleDeckBus,
            "CX" => BodyType::BusChassis,
            "DA" => BodyType::SemiTrailer,
            "DB" => BodyType::DrawbarTrailer,
            "DC" => BodyType::CentreAxleTrailer,
            "DE" => BodyType::RigidDrawbarTrailer,
            "SA" => BodyType::MotorCaravan,
            "SB" => BodyType::ArmouredVehicle,
            "SC" => BodyType::Ambulance,
            "SD" => BodyType::Hearse,
            "SE" => BodyType::TrailerCaravan,
            "SF" => BodyType::MobileCrane,
            "SG" => BodyType::SpecialGroup,
            "SH" => BodyType::WheelchairAccessible,
            "SJ" => BodyType::ConverterDolly,
            "SK" => BodyType::ExceptionalLoadTrailer,
            _ => return None,
        })
    }

    /// Best-effort map of a free-text body-style string — vPIC English, RDW
    /// Dutch `inrichting`, autoevolution/DBpedia prose — onto the standard
    /// variant. Order matters: more specific substrings win first.
    ///
    /// Market-segment words with no bodywork code (`"SUV"`, `"crossover"`,
    /// `"CUV"`) collapse to [`BodyType::MultiPurpose`] (`AF`), the standard's
    /// closest catch-all for a tall multi-use passenger car. Genuinely
    /// unrecognised input yields [`BodyType::Unknown`].
    pub fn parse(s: &str) -> Self {
        let lc = s.to_ascii_lowercase();
        let has = |needle: &str| lc.contains(needle);
        // Special-purpose (most specific first).
        if has("ambulance") {
            return BodyType::Ambulance;
        }
        if has("hearse") || has("lijkwagen") {
            return BodyType::Hearse;
        }
        if has("motor caravan") || has("motorhome") || has("camper") {
            return BodyType::MotorCaravan;
        }
        if has("armoured") || has("armored") {
            return BodyType::ArmouredVehicle;
        }
        if has("wheelchair") {
            return BodyType::WheelchairAccessible;
        }
        if has("crane") {
            return BodyType::MobileCrane;
        }
        // Trailers & buses.
        if has("semi-trailer") || has("semitrailer") {
            return BodyType::SemiTrailer;
        }
        if has("trailer") {
            return BodyType::DrawbarTrailer;
        }
        if has("double") && (has("deck") || has("decker")) {
            return BodyType::DoubleDeckBus;
        }
        if has("bus") || has("coach") {
            return BodyType::SingleDeckBus;
        }
        // Goods vehicles.
        if has("pick-up") || has("pickup") || has("pick up") {
            return BodyType::PickUp;
        }
        if has("tractor unit") || has("semi tractor") {
            return BodyType::TractorUnit;
        }
        if has("road tractor") {
            return BodyType::RoadTractor;
        }
        if has("chassis") {
            return BodyType::ChassisCab;
        }
        // Passenger cars.
        if has("truck station wagon") {
            return BodyType::TruckStationWagon;
        }
        if has("multi-purpose")
            || has("multipurpose")
            || has("mpv")
            || has("minivan")
            || has("people carrier")
            || has("monovolume")
            // Market-segment words with no bodywork code — closest standard bucket.
            || has("suv")
            || has("sport utility")
            || has("crossover")
            || has("cuv")
            || has("off-road")
        {
            return BodyType::MultiPurpose;
        }
        if has("station wagon")
            || has("stationwagen")
            || has("estate")
            || has("wagon")
            || has("kombi")
            || has("combi")
            || has("touring")
            || has("avant")
            || has("variant")
            || has("break")
        {
            return BodyType::StationWagon;
        }
        if has("convertible")
            || has("cabrio")
            || has("roadster")
            || has("spider")
            || has("spyder")
            || has("targa")
            || has("drophead")
        {
            return BodyType::Convertible;
        }
        if has("coupe") || has("coupé") {
            return BodyType::Coupe;
        }
        if has("hatchback")
            || has("hatch")
            || has("liftback")
            || has("fastback")
            || has("sportback")
        {
            return BodyType::Hatchback;
        }
        if has("van") || has("gesloten opbouw") {
            return BodyType::Van;
        }
        if has("saloon")
            || has("sedan")
            || has("berlina")
            || has("berline")
            || has("limousine")
            || has("notchback")
        {
            return BodyType::Saloon;
        }
        // Generic "truck" / "lorry" last so "truck station wagon" etc. win above.
        if has("lorry") || has("truck") {
            return BodyType::Lorry;
        }
        BodyType::Unknown
    }
}

/// Fuel-type enumeration the decoder normalizes vPIC strings into.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum FuelType {
    Gasoline,
    Diesel,
    Electric,
    Hybrid,
    PluginHybrid,
    Ethanol,
    FlexFuel,
    Cng,
    Lng,
    Lpg,
    Hydrogen,
    FuelCell,
    Methanol,
    NaturalGas,
    Other,
}

impl FuelType {
    /// Parse a free-form vPIC fuel-type string into one of the enum variants.
    pub fn parse(s: &str) -> Self {
        let lc = s.to_ascii_lowercase();
        match lc.as_str() {
            x if x.contains("gasoline") => FuelType::Gasoline,
            x if x.contains("diesel") => FuelType::Diesel,
            x if x.contains("plug") => FuelType::PluginHybrid,
            x if x.contains("hybrid") => FuelType::Hybrid,
            x if x.contains("methanol") || x.contains("m85") => FuelType::Methanol,
            x if x.contains("e85") || x.contains("ethanol") => FuelType::Ethanol,
            x if x.contains("flex") || x.contains("ffv") => FuelType::FlexFuel,
            x if x.contains("cng") || x.contains("compressed natural") => FuelType::Cng,
            x if x.contains("lng") || x.contains("liquefied natural") => FuelType::Lng,
            x if x.contains("lpg") || x.contains("propane") => FuelType::Lpg,
            x if x.contains("fuel cell") => FuelType::FuelCell,
            x if x.contains("hydrogen") => FuelType::Hydrogen,
            x if x.contains("electric") => FuelType::Electric,
            x if x.contains("natural gas") => FuelType::NaturalGas,
            _ => FuelType::Other,
        }
    }
}

/// Drive layout, normalised from the free-text `drive` strings in the engine
/// catalog (`"Front Wheel Drive"`, `"All Wheel Drive"`, …).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum DriveType {
    Fwd,
    Rwd,
    Awd,
    FourWheelDrive,
    Other,
}

impl DriveType {
    /// Parse a free-form drive-layout string into a variant.
    pub fn parse(s: &str) -> Self {
        let lc = s.to_ascii_lowercase();
        if lc.contains("front") {
            DriveType::Fwd
        } else if lc.contains("rear") {
            DriveType::Rwd
        } else if lc.contains("all wheel") || lc.contains("all-wheel") || lc.trim() == "awd" {
            DriveType::Awd
        } else if lc.contains("four wheel")
            || lc.contains("four-wheel")
            || lc.contains("4wd")
            || lc.contains("4x4")
        {
            DriveType::FourWheelDrive
        } else {
            DriveType::Other
        }
    }
}

/// Transmission type, normalised from the free-text `gearbox` strings in the
/// engine catalog (`"6-Speed Manual"`, `"7-Speed Dual Clutch"`, `"CVT"`, …).
/// Gear count is separate — see [`crate::EngineRow::gearbox_speeds`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum Transmission {
    Manual,
    Automatic,
    DualClutch,
    Cvt,
    SingleSpeed,
    AutomatedManual,
    Other,
}

impl Transmission {
    /// Parse a free-form gearbox string into a variant. Order matters: specific
    /// box types (CVT, dual-clutch, automated-manual) are tested before the
    /// generic `"manual"` / `"automatic"` substrings.
    pub fn parse(s: &str) -> Self {
        let lc = s.to_ascii_lowercase();
        if lc.contains("cvt") || lc.contains("continuously variable") {
            Transmission::Cvt
        } else if lc.contains("single-speed") || lc.contains("single speed") || lc.contains("1-speed")
        {
            Transmission::SingleSpeed
        } else if lc.contains("dual clutch")
            || lc.contains("dual-clutch")
            || lc.contains("twin clutch")
            || lc.contains("dsg")
            || lc.contains("dct")
            || lc.contains("dkg")
            || lc.contains("pdk")
            || lc.contains("s tronic")
            || lc.contains("s-tronic")
            || lc.contains("powershift")
        {
            Transmission::DualClutch
        } else if lc.contains("automated manual")
            || lc.contains("automatic manual")
            || lc.contains("automatized")
            || lc.contains("semi-automatic")
            || lc.contains("amt")
        {
            Transmission::AutomatedManual
        } else if lc.contains("manual") {
            Transmission::Manual
        } else if lc.contains("automatic") || lc.contains("auto") || lc.contains("tiptronic") {
            Transmission::Automatic
        } else {
            Transmission::Other
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn vin_uppercases_input() {
        let v = Vin::new("1hgcm82633a004352").unwrap();
        assert_eq!(v.as_str(), "1HGCM82633A004352");
    }

    #[test]
    fn vin_section_accessors() {
        let v = Vin::new("1HGCM82633A004352").unwrap();
        assert_eq!(v.wmi(), "1HG");
        assert_eq!(v.vds(), "CM8263");
        assert_eq!(v.vis(), "3A004352");
        assert_eq!(v.check_digit(), '3');
        assert_eq!(v.year_code(), '3');
        assert_eq!(v.plant_code(), 'A');
    }

    #[test]
    fn vin_display_returns_canonical() {
        let v = Vin::new("1hgcm82633a004352").unwrap();
        assert_eq!(format!("{}", v), "1HGCM82633A004352");
    }

    #[test]
    fn body_type_parse_standard_mappings() {
        // vPIC English
        assert_eq!(BodyType::parse("4-Door Sedan"), BodyType::Saloon);
        assert_eq!(BodyType::parse("2-Door Coupe"), BodyType::Coupe);
        assert_eq!(BodyType::parse("Hatchback"), BodyType::Hatchback);
        assert_eq!(BodyType::parse("Station Wagon"), BodyType::StationWagon);
        assert_eq!(BodyType::parse("Convertible"), BodyType::Convertible);
        assert_eq!(BodyType::parse("2-Door Cabriolet"), BodyType::Convertible);
        assert_eq!(BodyType::parse("Roadster"), BodyType::Convertible);
        assert_eq!(BodyType::parse("Crew Cab Pickup"), BodyType::PickUp);
        assert_eq!(BodyType::parse("Cargo Van"), BodyType::Van);
        // EU / continental / registry vocab
        assert_eq!(BodyType::parse("Berline"), BodyType::Saloon);
        assert_eq!(BodyType::parse("Kombi"), BodyType::StationWagon);
        assert_eq!(BodyType::parse("Avant"), BodyType::StationWagon);
        assert_eq!(BodyType::parse("stationwagen"), BodyType::StationWagon);
        assert_eq!(BodyType::parse("gesloten opbouw"), BodyType::Van);
        // MPV + market-segment words (no bodywork code) collapse to AF
        assert_eq!(BodyType::parse("Minivan"), BodyType::MultiPurpose);
        assert_eq!(
            BodyType::parse("Sport Utility Vehicle (SUV)"),
            BodyType::MultiPurpose
        );
        assert_eq!(
            BodyType::parse("Crossover Utility Vehicle (CUV)"),
            BodyType::MultiPurpose
        );
        // special-purpose
        assert_eq!(BodyType::parse("Ambulance"), BodyType::Ambulance);
        assert_eq!(BodyType::parse("Hearse"), BodyType::Hearse);
        // category L two-wheelers / junk have no bodywork code
        assert_eq!(BodyType::parse("Motorcycle"), BodyType::Unknown);
        assert_eq!(BodyType::parse("Unknown blob"), BodyType::Unknown);
    }

    #[test]
    fn body_type_code_roundtrip() {
        for &b in BODY_TYPES {
            assert_eq!(BodyType::from_code(b.code()), Some(b), "roundtrip {b:?}");
        }
        assert_eq!(BODY_TYPES.len(), 38);
        assert_eq!(BodyType::from_code("aa"), Some(BodyType::Saloon)); // case-insensitive
        assert_eq!(BodyType::from_code("ZZ"), None);
        assert_eq!(BodyType::Unknown.code(), "");
    }

    #[test]
    fn drive_type_parse() {
        assert_eq!(DriveType::parse("Front Wheel Drive"), DriveType::Fwd);
        assert_eq!(DriveType::parse("Rear Wheel Drive"), DriveType::Rwd);
        assert_eq!(DriveType::parse("All Wheel Drive"), DriveType::Awd);
        assert_eq!(
            DriveType::parse("Four Wheel Drive"),
            DriveType::FourWheelDrive
        );
        assert_eq!(DriveType::parse("4x4"), DriveType::FourWheelDrive);
        assert_eq!(DriveType::parse(""), DriveType::Other);
    }

    #[test]
    fn transmission_parse() {
        assert_eq!(Transmission::parse("6-Speed Manual"), Transmission::Manual);
        assert_eq!(
            Transmission::parse("7-Speed Automatic"),
            Transmission::Automatic
        );
        assert_eq!(
            Transmission::parse("7-Speed Dual Clutch"),
            Transmission::DualClutch
        );
        assert_eq!(Transmission::parse("6-Speed DSG"), Transmission::DualClutch);
        assert_eq!(Transmission::parse("7-Speed S tronic"), Transmission::DualClutch);
        assert_eq!(Transmission::parse("CVT"), Transmission::Cvt);
        assert_eq!(
            Transmission::parse("Single-Speed"),
            Transmission::SingleSpeed
        );
        assert_eq!(
            Transmission::parse("5-Speed Automated Manual"),
            Transmission::AutomatedManual
        );
        assert_eq!(Transmission::parse("Tiptronic"), Transmission::Automatic);
        assert_eq!(Transmission::parse(""), Transmission::Other);
    }

    #[test]
    fn fuel_type_full_coverage() {
        assert_eq!(FuelType::parse("Gasoline"), FuelType::Gasoline);
        assert_eq!(FuelType::parse("Diesel"), FuelType::Diesel);
        assert_eq!(FuelType::parse("Electric"), FuelType::Electric);
        assert_eq!(FuelType::parse("Plug-in Hybrid"), FuelType::PluginHybrid);
        assert_eq!(FuelType::parse("Hybrid"), FuelType::Hybrid);
        assert_eq!(FuelType::parse("E85"), FuelType::Ethanol);
        assert_eq!(FuelType::parse("Ethanol (E85)"), FuelType::Ethanol);
        assert_eq!(
            FuelType::parse("Flexible Fuel Vehicle (FFV)"),
            FuelType::FlexFuel
        );
        assert_eq!(
            FuelType::parse("Compressed Natural Gas (CNG)"),
            FuelType::Cng
        );
        assert_eq!(
            FuelType::parse("Liquefied Natural Gas (LNG)"),
            FuelType::Lng
        );
        assert_eq!(
            FuelType::parse("Liquefied Petroleum Gas (LPG)"),
            FuelType::Lpg
        );
        assert_eq!(
            FuelType::parse("Compressed Hydrogen/Hydrogen"),
            FuelType::Hydrogen
        );
        assert_eq!(FuelType::parse("Fuel Cell"), FuelType::FuelCell);
        assert_eq!(FuelType::parse("Methanol (M85)"), FuelType::Methanol);
        assert_eq!(FuelType::parse("Unknown"), FuelType::Other);
    }
}