Skip to main content

Crate credit_card_type

Crate credit_card_type 

Source
Expand description

§credit-card-type — detect a credit card brand from a (partial) number

Determine the card brand (Visa, Mastercard, American Express, …) from a full or partial card number — designed for type-as-you-go detection as a user types. It is not a validation library; it is the building block a payment UI uses to show the right brand icon, apply the right spacing, and hint the CVV length.

A faithful Rust port of Braintree’s credit-card-type npm package, which has no Rust equivalent. Zero dependencies and #![no_std].

use credit_card_type::credit_card_type;

// A complete-enough prefix resolves to a single brand.
let cards = credit_card_type("4111");
assert_eq!(cards.len(), 1);
assert_eq!(cards[0].type_, "visa");
assert_eq!(cards[0].nice_type, "Visa");
assert_eq!(cards[0].gaps, &[4, 8, 12]);
assert_eq!(cards[0].code.name, "CVV");

// An ambiguous prefix returns every candidate brand.
let ambiguous = credit_card_type("6");
assert!(ambiguous.len() > 1);

The input should be normalized (digits only, no spaces or separators) before use.

Modules§

brand
Code-friendly brand identifiers returned in CardType::type_.

Structs§

CardType
A detected (candidate) card brand and its formatting metadata.
Code
The security-code metadata for a card brand (e.g. CVV, size 3).

Functions§

credit_card_type
Detect the card brand(s) for a (possibly partial) card number.
get_type_info
Look up a single brand’s metadata by its brand identifier (e.g. "visa"), or None if the identifier is not recognized.