Function rusty_ulid::crockford::parse_crockford_u128
source · pub fn parse_crockford_u128(input: &str) -> Result<u128, DecodingError>
Expand description
Parses the given crockford Base32 string into a u128
.
Examples
let parsed = parse_crockford_u128("0000000000000000000000007Z");
assert_eq!(Ok(0xFF), parsed);
When decoding, upper and lower case letters are accepted,
i
and l
will be treated as 1
and o
will be treated as 0
.
use rusty_ulid::crockford::*;
let parsed = parse_crockford_u128("00000000000000000x1iIlLoO0")?;
let mut string_representation = String::new();
append_crockford_u128(parsed, &mut string_representation);
assert_eq!(string_representation, "00000000000000000X11111000");
Errors
Parsing a string with other than 26 bytes results in InvalidLength
.
use rusty_ulid::crockford::*;
let nope = parse_crockford_u128("1234567890123456789012345");
assert_eq!(Err(DecodingError::InvalidLength), nope);
use rusty_ulid::crockford::*;
let nope = parse_crockford_u128("123456789012345678901234567");
assert_eq!(Err(DecodingError::InvalidLength), nope);
Parsing 26 bytes results in DataTypeOverflow
if the u128
would overflow.
use rusty_ulid::crockford::*;
let yeah = parse_crockford_u128("7ZZZZZZZZZZZZZZZZZZZZZZZZZ");
assert_eq!(Ok(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF), yeah);
let nope = parse_crockford_u128("80000000000000000000000000");
assert_eq!(Err(DecodingError::DataTypeOverflow), nope);
Parsing a string containing an invalid character results in InvalidChar
containing
the character.
use rusty_ulid::crockford::*;
let nope = parse_crockford_u128("0000000000000000000000000U");
assert_eq!(Err(DecodingError::InvalidChar('U')), nope);