macro_rules! base64_serde_type {
    ($visibility:vis $typename:ident, $engine:expr) => { ... };
    (impl_only, $typename:ident, $engine:expr) => { ... };
}
Expand description

Create a type with appropriate serialize and deserialize functions to use with serde when specifying how to serialize a particular field.

Once the type is defined, you can use #[serde(with = "YourTypeNameHere")] on a Vec<u8> field that you wished to serialize to base64 or deserialize from base64.

If you want to change resulting type’s visibility, prefix the desired type name with appropiate visibility, for example:

use base64_serde::base64_serde_type;

base64_serde_type!(pub IWillBeAPublicType, base64::engine::general_purpose::STANDARD);
base64_serde_type!(pub(crate) IWillBeACrateType, base64::engine::general_purpose::STANDARD);

Examples

Existing engine:

use base64_serde::base64_serde_type;

base64_serde_type!(Base64Standard, base64::engine::general_purpose::STANDARD);

#[derive(serde::Serialize, serde::Deserialize)]
struct ByteHolder {
    #[serde(with = "Base64Standard")]
    bytes: Vec<u8>,
}

Custom engine:

use base64_serde::base64_serde_type;

const BCRYPT_NO_PAD: base64::engine::GeneralPurpose =
    base64::engine::GeneralPurpose::new(
        &base64::alphabet::BCRYPT,
        base64::engine::general_purpose::NO_PAD
    );

base64_serde_type!(Base64BcryptNoPad, BCRYPT_NO_PAD);