pub struct Base64<'a> { /* private fields */ }
Expand description
Base64 number
value: a Vec
conf: the config specific for this Base64 number
Implements Clone, Debug, Eqs, and Compare
Implementations§
source§impl<'a> Base64<'a>
impl<'a> Base64<'a>
sourcepub fn decode_to_bytes(&self) -> Vec<u8>
pub fn decode_to_bytes(&self) -> Vec<u8>
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);
}
sourcepub fn decode_to_unsigned(&self) -> Result<u128, Base64Error>
pub fn decode_to_unsigned(&self) -> Result<u128, Base64Error>
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),
}
}
source§impl<'a> Base64<'a>
impl<'a> Base64<'a>
sourcepub fn new_encode_unsigned(unsigned: &u128, conf: &'a Config<'a>) -> Self
pub fn new_encode_unsigned(unsigned: &u128, conf: &'a Config<'a>) -> Self
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
}
sourcepub fn encode_unsigned(&mut self, unsigned: &u128)
pub fn encode_unsigned(&mut self, unsigned: &u128)
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
}
sourcepub fn new_encode_bytes(s: &[u8], conf: &'a Config<'_>) -> Self
pub fn new_encode_bytes(s: &[u8], conf: &'a Config<'_>) -> Self
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);
}
sourcepub fn encode_bytes(&mut self, s: &[u8])
pub fn encode_bytes(&mut self, s: &[u8])
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);
}
source§impl<'a> Base64<'a>
impl<'a> Base64<'a>
sourcepub fn default() -> Self
pub fn default() -> Self
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);
}
sourcepub fn new_random(len: usize, conf: &'a Config<'a>) -> Self
pub fn new_random(len: usize, conf: &'a Config<'a>) -> Self
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
}
sourcepub fn set_random(&mut self, len: usize)
pub fn set_random(&mut self, len: usize)
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
}
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
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
}
sourcepub fn new_from_string(
new: &str,
conf: &'a Config<'a>
) -> Result<Self, Base64Error>
pub fn new_from_string(
new: &str,
conf: &'a Config<'a>
) -> Result<Self, Base64Error>
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
}
}
sourcepub fn set_config(&mut self, conf: &'a Config<'a>)
pub fn set_config(&mut self, conf: &'a Config<'a>)
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 _===
}
sourcepub fn set_from_string(&mut self, new: &str) -> bool
pub fn set_from_string(&mut self, new: &str) -> bool
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 _===
}
sourcepub fn expand_to(&mut self, len: usize)
pub fn expand_to(&mut self, len: usize)
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 _===
}
sourcepub fn truncate_to(&mut self, len: usize)
pub fn truncate_to(&mut self, len: usize)
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§
source§impl<'a> Ord for Base64<'a>
impl<'a> Ord for Base64<'a>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a> PartialOrd<Base64<'a>> for Base64<'a>
impl<'a> PartialOrd<Base64<'a>> for Base64<'a>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more