Struct lb64::Base64

source ·
pub struct Base64<'a> { /* private fields */ }
Expand description

Base64 number

value: a Vec representing the value of the Base64 number

conf: the config specific for this Base64 number

Implements Clone, Debug, Eqs, and Compare

Implementations§

Decode a Base64 value to it’s a Vector of u8

Return:

The vector of u8 corresponding to the data that was encoded into base64

Example:
extern crate lb64;
use lb64::{Base64, config::STANDARD};

fn main() {
    let word: &str = "Hello";
    let mut b64 =  Base64::new_encode_bytes(word.as_bytes(), STANDARD);
    let decoded_word = match String::from_utf8(b64.decode_to_bytes()) {
        Ok(value) => value,
        Err(e) => panic!("{}", e),
    };
    println!("Before = {}\nAfter = {}", word, decoded_word);
}

Loop over Base64 number convert each value to it’s corresponding unsigned value and sum all of those

Return:

Result with either the u128 or Base64Error::OverflowError

Example:
extern crate lb64;
use lb64::{Base64, config::STANDARD};

fn main() {
    let mut b64 =  Base64::new_encode_unsigned(&8, STANDARD);
    match b64.decode_to_unsigned() {
        Ok(value) => println!("{}", value),
        Err(e) => println!("{}", e),
    }
}

Creates a base64 number equivalent to the provided unsigned value

Parameters:

unsigned, the unsigned value to convert

Return:

the new base64 number equivalent to the unsigned value passed

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let b64 = Base64::new_encode_unsigned(&128, URL_SAFE_NO_PADDING); // Sets b64 to the equivalent Base64 of 128
    println!("{}", b64); // prints "CA"
    let b64 = Base64::new_encode_unsigned(&128, URL_SAFE_PADDING); // Sets b64 to the equivalent Base64 of 128 with padding
    println!("{}", b64); // prints "CA==" so it's divisble by 4
}

Sets the base64 value from an unsigned integer u128

Parameters:

the unsigned value to set the b64 equivalent to

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let mut b64 = Base64::default(); // Sets b64 to default which is Standard config and "A"
    println!("{}", b64); // prints "A"
    b64.encode_unsigned(&2);
    println!("{}", b64); // prints "C===" because Standard has padding
}

Encodes the provided bytes slice into Base64

Parameters:

The configuration struct

&u8 the bytes to convert

Returns:

The new Base64 number

Example:
extern crate lb64;
use lb64::{Base64};
use lb64::config::MIME; // Include MIME config

fn main() {
    let word: &str = "Hi";
    let b64 = Base64::new_encode_bytes(word.as_bytes(), MIME);
    println!("{}", b64);
}

Sets the Base64 value to the encoded byte value in base64

Parameters:

The bytes to encode

Example:
extern crate lb64;
use lb64::{Base64};
use lb64::config::MIME; // Include MIME config

fn main() {
    let word: &str = "Hi";
    let mut b64 = Base64::new_encode_bytes(word.as_bytes(), MIME);
    println!("{}", b64);
    let new_word: &str = "Hi!";
    b64.encode_bytes(new_word.as_bytes()); // Changes the encoded
    println!("{}", b64);
}

Creates a default Base64 number equivalent to 0 (“A”) with STANDARD

Returns:

The new Base64 number with the Standard configuration and value of “A”

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64

fn main() {
    let b64 = Base64::default(); // Creates new Base64 of value "A"
    println!("{}", b64);
}

Creates a random base64 number of at least the provided length.

Parameters:

new length of base64 number and the configuration struct, Note if the configuration specifies padding then the length may be higher if the length specififed isn’t divisible by 4

#Returns: the new random base64 number

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let b64 = Base64::new_random(5, URL_SAFE_NO_PADDING); // Sets the length to 5 and randomly generates the values
    println!("{}", b64); // Since there's no padding then the length will be 5
    let b64 = Base64::new_random(5, URL_SAFE_PADDING); // Generates a random value of length 5, but with padding
    println!("{}", b64); // Since there's padding then the length will be divisible by 4 therefore length 8
}

Sets the value of the Base64 number to a random value. Param: len, length for base64 number

Parameters:

The minimum length for the base64 number

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let mut b64 = Base64::new_random(5, URL_SAFE_NO_PADDING); // Sets the length to 5 and randomly generates the values
    println!("{}", b64); // Since there's no padding then the length will be 5
    b64.set_random(8);
    println!("{}", b64); // No padding length will now be 8
}

Get length of Base64 number

Return:

Return usize of Base64 number

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING}; // Constant config

fn main() {
    let mut b64 = Base64::new_random(5, URL_SAFE_NO_PADDING); // Sets the length to 5 and randomly generates the values
    println!("{}", b64); // Since there's no padding then the length will be 5
    println!("{}", b64.len()); // Length of 5
}

Sets Base64 to that String if it’s valid

Return:

If all characters are valid Base64 return Self otherwise a Base64Error::InvalidBase64CharacterError

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let b64 = Base64::new_from_string("Hello", URL_SAFE_PADDING); // Sets b64 to the string if the string is valid Base64
    // It returns a Result<Base64, String>
    match b64 {
        Ok(value) => println!("{}", 64), // prints "Hello===" adds padding so it's divisible by 4
        Err(e) => println!("{}", e), // The error message stating the first incorrect character
    }
}

Takes a new configuration and converts the Base64 number to that representation

Example
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let mut b64 = Base64::new_encode_unsigned(&63, URL_SAFE_NO_PADDING); // Sets b64 to _
    b64.set_config(URL_SAFE_PADDING); // Changes configs and adds padding to _
    println!("{}", b64); // Prints _===
}

Sets the Base64 value to a given String

Return:

false if any value is invalid

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let mut b64 = Base64::new_encode_unsigned(&63, URL_SAFE_NO_PADDING); // Sets b64 to _
    b64.set_config(URL_SAFE_PADDING); // Changes configs and adds padding to _
    println!("{}", b64); // Prints _===
}

Extends base 64 number by prepending As to it to fit a new size

Parameters:

len, the new size of the base64 value

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_NO_PADDING, URL_SAFE_PADDING}; // Constant configs

fn main() {
    let mut b64 = Base64::new_encode_unsigned(&63, URL_SAFE_NO_PADDING); // Sets b64 to _
    b64.set_config(URL_SAFE_PADDING); // Changes configs and adds padding to _
    println!("{}", b64); // Prints _===
}

Truncates base64 number be removing the most significant values until it fits the new size

Parameters:

len, the new size of the base64 value. Must be greater than 0

Example:
extern crate lb64; // Import/Include crate
use lb64::{Base64}; // Base64
use lb64::config::{URL_SAFE_PADDING, URL_SAFE_NO_PADDING}; // Constant configs

fn main() {
    let mut b64 = Base64::new_encode_unsigned(&63, URL_SAFE_PADDING); // Sets b64 to _===
    println!("{}", b64); // Prints _===
    b64.truncate_to(2); // This does essentially nothing because padding is required and therefore it must be divisible by 4
    println!("{}", b64); // Prints _=== stil
    let mut b64 = Base64::new_encode_unsigned(&63, URL_SAFE_NO_PADDING); // Sets b64 to _
    b64.truncate_to(1); // Length is already 1 so it remains _
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.