pub struct UpcA(/* private fields */);Expand description
Universal Product Code version A (UPC-A)
A UPC-A code uniquely identifies a product using a 12-digit number. The last digit is a check digit.
§Examples
use upc_a::UpcA;
// Parse an ISRC from a string
let upc_a = UpcA::from_code(123456789012)?;
// Retrieve the numeric value
assert_eq!(upc_a.to_code(), 123456789012);
// Display a formatted ISRC
assert_eq!(upc_a.to_string(), "123456789012");§References
Implementations§
Source§impl UpcA
impl UpcA
Sourcepub const fn from_code(n: u64) -> Result<Self, UpcAParseError>
pub const fn from_code(n: u64) -> Result<Self, UpcAParseError>
Creates an UpcA from a numeric code.
The input must be a 12-digit number, and the last digit must be a valid checksum digit.
§Examples
use upc_a::UpcA;
// Valid UPC-A
let upc_a = UpcA::from_code(123456789012)?;
assert_eq!(upc_a.to_string(), "123456789012");
// Invalid UPC-A (incorrect checksum)
assert!(UpcA::from_code(123456789010).is_err());§Errors
Returns an UpcAParseError if:
- The integer value exceeds the maximum allowed value (999,999,999,999)
- The checksum digit is invalid
Sourcepub const fn from_bytes(bytes: &[u8; 5]) -> Result<Self, UpcAParseError>
pub const fn from_bytes(bytes: &[u8; 5]) -> Result<Self, UpcAParseError>
Creates a UpcA from a 5-byte binary representation.
This method deserializes a UPC-A code from its compact binary representation, which is primarily useful for binary serialization formats.
§Examples
use upc_a::UpcA;
let bytes = [0x14, 0x1A, 0x99, 0xBE, 0x1C];
let upc_a = UpcA::from_bytes(&bytes)?;
assert_eq!(upc_a.to_code(), 123456789012);§Errors
Returns an UpcAParseError if:
- The integer value exceeds the maximum allowed value (999,999,999,999)
- The checksum digit is invalid
Sourcepub const fn to_bytes(self) -> [u8; 5]
pub const fn to_bytes(self) -> [u8; 5]
Converts the UpcA to its compact 5-byte binary representation.
This method serializes a UPC-A code into a fixed-size array suitable for binary storage or
transmission. It is the inverse of from_bytes.
§Examples
use upc_a::UpcA;
use std::str::FromStr;
let upc_a = UpcA::from_code(123456789012)?;
let bytes = upc_a.to_bytes();
assert_eq!(bytes, *b"\x14\x1A\x99\xBE\x1C");
// Round-trip conversion
let round_trip = UpcA::from_bytes(&bytes)?;
assert_eq!(round_trip, upc_a);Trait Implementations§
Source§impl Display for UpcA
impl Display for UpcA
§Examples
use upc_a::UpcA;
let upc_a = UpcA::from_code(123456789012)?;
assert_eq!(upc_a.to_string(), "123456789012");Source§impl FromStr for UpcA
impl FromStr for UpcA
This implementation delegates to UpcA::from_code.
§Examples
use upc_a::UpcA;
use std::str::FromStr;
// Parse using FromStr
let upc_a = UpcA::from_str("123456789012")?;
// Or using the more idiomatic parse method
let upc_a: UpcA = "123456789012".parse()?;