Crate mb_vin

Source
Expand description

mb-vin - A lightweight VIN parser and validator

This crate provides efficient parsing and validation of VIN (Vehicle Identification Number).

Features:

  • No allocations (zero-copy)
  • Parse and validate VINs (length, character set, check digit)
  • Extract WMI (World Manufacturer Identifier), VDS, VIS
  • Lookup manufacturer and country from WMI
  • Infer manufacturing year from VIN
  • Optional serde support with serialization and deserialization
  • Optional simd to fast batch parse

§Examples

use mb_vin::Vin;

// Parse a valid VIN
let vin = Vin::parse(b"5GZCZ43D13S812715").unwrap();

// Access VIN components
println!("WMI (manufacturer): {}", vin.wmi());
println!("VDS (vehicle attributes): {}", vin.vds());
println!("VIS (vehicle identifier): {}", vin.vis());
println!("Check digit: {}", vin.check_digit());

if let Some(info) = vin.lookup_wmi_info() {
    println!("Country: {}", info.country);
    println!("Manufacturer: {}", info.manufacturer);
    println!("Region: {}", info.region);
}

if let Some(year) = vin.year_of_manufacture() {
    println!("Year: {}", year);
}

§Optional Serde Support

Enable the serde feature in your Cargo.toml:

mb-vin = { version = "0.8.0", features = ["serde"] }

The crate provides two approaches for serialization/deserialization:

  1. OwnedVin - Uses owned strings, recommended for most cases.
  2. Vin<'a> - For advanced use cases with custom deserializers that preserve input lifetimes.
use mb_vin::OwnedVin;
use serde_json;

// Deserialize from JSON
let vin: OwnedVin = serde_json::from_str("\"5GZCZ43D13S812715\"").unwrap();

// Access components
assert_eq!(vin.wmi, "5GZ");
assert_eq!(vin.vds, "CZ43D1");

// Convert to a borrowed Vin if needed
let borrowed = vin.as_vin();

// Serialize back to JSON
let json = serde_json::to_string(&vin).unwrap();
assert_eq!(json, "\"5GZCZ43D13S812715\"");

§Using with structures

use mb_vin::OwnedVin;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Vehicle {
    vin: OwnedVin,
    make: String,
    model: String,
    year: u16,
}

// Create from JSON
let json = r#"{
    "vin": "5GZCZ43D13S812715",
    "make": "General Motors",
    "model": "Suburban",
    "year": 2003
}"#;

let vehicle: Vehicle = serde_json::from_str(json).unwrap();
assert_eq!(vehicle.vin.wmi, "5GZ");

§Advanced: Using borrowed Vin<'a>

The Vin<'a> struct implements Deserialize<'de> for advanced use cases where you need zero-copy deserialization with custom deserializers that preserve input lifetimes. This is rarely needed for typical applications.

Structs§

Vin
Represents a parsed and validated Vehicle Identification Number
WmiInfo
Information about a World Manufacturer Identifier (WMI)

Enums§

Error
Errors that may occur during VIN parsing and validation

Constants§

MAX_LENGTH
Max vehicle identification number length.