pub struct Base32 {}
Expand description

Struct for encoding, decoding, and validating EncodableData with Base32.

Uses lowercase version of the RFC 4648 Base32 alphabet. Methods treat each set of 5 bits in EncodableData as a separate character. Invalid characters will be treated as ‘a’ instead of marking the entire label as invalid for efficiency. Contains no properties, for usage see individual functions or ClientRoutingLabel.

Implementations§

Source§

impl Base32

Source

pub fn encode(&self, encodable_data: &mut [EncodableData]) -> String

Returns a lowercase Base32 string encoded from encodable_data.

Iterates over encodable_data, encoding bits from value until not enough bits remain to make a full char. Remaining bits are then used in the subsequent iteration. After iterating over everything, if there are not enough bits to make a char 0 will be used to pad the left over bits. Encoding uses a lowercase version of the RFC 4648 Base32 alphabet.

§Examples:
use amazon_cloudfront_client_routing_lib::encode_decode::Base32;
use amazon_cloudfront_client_routing_lib::client_routing_label::EncodableData;
 
let encoding_system = Base32 {};
let encodable_data = &mut [
    EncodableData { // 0b01010 => "k"
        value: 10,
        num_bits: 5
    },
    EncodableData { // 0b00011_11011 => "d3"
        value: 123,
        num_bits: 10
    },
    EncodableData { // 0b0 => "a"
        value: 0,
        num_bits: 1
    },
];
 
assert_eq!("kd3a", encoding_system.encode(encodable_data));
Source

pub fn is_valid_client_routing_label( &self, total_num_bits: u8, client_routing_label: &[u8], ) -> Result<(), DecodeLengthError>

Validates client_routing_label is the proper length to fit total_num_bits.

Calculates how many chars would be encoded for total_num_bits and then checks if the client_routing_label has that many chars. Returns a Result with ‘()’ if it’s valid or a DecodeLengthError if it’s not valid.

§Examples:
use amazon_cloudfront_client_routing_lib::encode_decode::Base32;
 
let encoding_system = Base32 {};
 
// valid
match encoding_system.is_valid_client_routing_label(145, b"abaaaaaaaaaaaaaaaaaaaackvj5oa") {
    Ok(()) => (),
    Err(_e) => panic!("Threw error when shouldn't have.")
};
 
// invalid
match encoding_system.is_valid_client_routing_label(145, b"abaaaaaaaaaaaaaaaaaaaackvj5oabcd") {
    Ok(()) => (),
    Err(e) => assert_eq!("Passed 32 - expected 29 characters", e.to_string())
};
Source

pub fn decode( &self, encodable_data: &mut [EncodableData], encoded_label: &[u8], total_num_bits: u8, ) -> Result<(), DecodeLengthError>

Sets encodable_data based on passed encoded_label.

Validates encoded_label is valid based on total_num_bits. If not valid, returns a Result containing DecodeLengthError. If valid, iterates over encodable_data and sets each value based on the label value. Invalid characters in a label are treated as if they had a value of 0.

§Examples:
use amazon_cloudfront_client_routing_lib::encode_decode::Base32;
use amazon_cloudfront_client_routing_lib::client_routing_label::EncodableData;
 
let encoding_system = Base32 {};
 
// valid
let encodable_data = &mut [
    EncodableData {
        value: 0,
        num_bits: 5
    },
    EncodableData {
        value: 0,
        num_bits: 10
    },
    EncodableData {
        value: 0,
        num_bits: 1
    },
];
 
match encoding_system.decode(encodable_data, b"kd3a", 16) {
    Ok(()) => {
        assert_eq!(10, encodable_data[0].value);
        assert_eq!(123, encodable_data[1].value);
        assert_eq!(0, encodable_data[2].value);
    },
    Err(_e) => panic!("Threw error when shouldn't have.")
};
 
// invalid
match encoding_system.decode(encodable_data, b"kd3a", 10) {
    Ok(()) => panic!("Didn't throw error when should have."),
    Err(e) => assert_eq!("Passed 4 - expected 2 characters", e.to_string())
};

Trait Implementations§

Source§

impl Clone for Base32

Source§

fn clone(&self) -> Base32

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Base32

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Base32

Auto Trait Implementations§

§

impl Freeze for Base32

§

impl RefUnwindSafe for Base32

§

impl Send for Base32

§

impl Sync for Base32

§

impl Unpin for Base32

§

impl UnwindSafe for Base32

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V