diff --git a/Cargo.toml b/Cargo.toml index 30f911c..8855350 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ structopt = "0.3.26" # test fixtures for engine tests rstest = "0.12.0" rstest_reuse = "0.3.0" +lazy_static = "1.4.0" [features] default = ["std"] diff --git a/src/alphabet.rs b/src/alphabet.rs index 7cd1b57..11f6eb4 100644 --- a/src/alphabet.rs +++ b/src/alphabet.rs @@ -12,6 +12,10 @@ const ALPHABET_SIZE: usize = 64; /// Common alphabets are provided as constants, and custom alphabets /// can be made via `from_str` or the `TryFrom` implementation. /// +/// # Examples +/// +/// Building and using a custom Alphabet: +/// /// ``` /// let custom = base64::alphabet::Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").unwrap(); /// @@ -19,6 +23,33 @@ const ALPHABET_SIZE: usize = 64; /// &custom, /// base64::engine::general_purpose::PAD); /// ``` +/// +/// Building a const: +/// +/// ``` +/// use base64::alphabet::Alphabet; +/// +/// static CUSTOM: Alphabet = { +/// // Result::unwrap() isn't const yet, but panic!() is OK +/// match Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") { +/// Ok(x) => x, +/// Err(_) => panic!("creation of alphabet failed"), +/// } +/// }; +/// ``` +/// +/// Building a lazy_static: +/// +/// ``` +/// use base64::{ +/// alphabet::Alphabet, +/// engine::{general_purpose::GeneralPurpose, GeneralPurposeConfig}, +/// }; +/// +/// lazy_static::lazy_static! { +/// static ref CUSTOM: Alphabet = Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").unwrap(); +/// } +/// ``` #[derive(Clone, Debug, Eq, PartialEq)] pub struct Alphabet { pub(crate) symbols: [u8; ALPHABET_SIZE], diff --git a/src/engine/tests.rs b/src/engine/tests.rs index 6430b35..b048005 100644 --- a/src/engine/tests.rs +++ b/src/engine/tests.rs @@ -623,7 +623,7 @@ fn decode_padding_before_final_non_padding_char_error_invalid_byte(engine fn decode_pad_mode_requires_canonical_rejects_non_canonical(engine_wrapper: E) { let engine = E::standard_with_pad_mode(true, DecodePaddingMode::RequireCanonical); - let suffixes = vec!["/w", "/w=", "iYU"]; + let suffixes = ["/w", "/w=", "iYU"]; for num_prefix_quads in 0..256 { for &suffix in suffixes.iter() { let mut encoded = "AAAA".repeat(num_prefix_quads); @@ -896,7 +896,7 @@ fn decode_pad_mode_requires_no_padding_accepts_no_padding(engi fn decode_pad_mode_requires_no_padding_rejects_any_padding(engine_wrapper: E) { let engine = E::standard_with_pad_mode(true, DecodePaddingMode::RequireNone); - let suffixes = vec!["/w=", "/w==", "iYU="]; + let suffixes = ["/w=", "/w==", "iYU="]; for num_prefix_quads in 0..256 { for &suffix in suffixes.iter() { let mut encoded = "AAAA".repeat(num_prefix_quads); diff --git a/src/write/encoder_tests.rs b/src/write/encoder_tests.rs index ce76d63..1f1a165 100644 --- a/src/write/encoder_tests.rs +++ b/src/write/encoder_tests.rs @@ -358,7 +358,7 @@ fn retrying_writes_that_error_with_interrupted_works() { Ok(_) => break, Err(e) => match e.kind() { io::ErrorKind::Interrupted => continue, - _ => Err(e).unwrap(), // bail + _ => panic!("{:?}", e), // bail }, } }