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
impl Base32
Sourcepub fn encode(&self, encodable_data: &mut [EncodableData]) -> String
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));Sourcepub fn is_valid_client_routing_label(
&self,
total_num_bits: u8,
client_routing_label: &[u8],
) -> Result<(), DecodeLengthError>
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())
};Sourcepub fn decode(
&self,
encodable_data: &mut [EncodableData],
encoded_label: &[u8],
total_num_bits: u8,
) -> Result<(), DecodeLengthError>
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())
};