15 releases

0.1.15 Apr 28, 2024
0.1.14 Nov 21, 2023
0.1.13 May 20, 2023
0.1.12 May 27, 2021
0.1.1 Nov 18, 2017

#487 in Encoding

Download history 101430/week @ 2024-04-04 96819/week @ 2024-04-11 95167/week @ 2024-04-18 88130/week @ 2024-04-25 92548/week @ 2024-05-02 90851/week @ 2024-05-09 97804/week @ 2024-05-16 108997/week @ 2024-05-23 114225/week @ 2024-05-30 94669/week @ 2024-06-06 101559/week @ 2024-06-13 93307/week @ 2024-06-20 86114/week @ 2024-06-27 87058/week @ 2024-07-04 120939/week @ 2024-07-11 117453/week @ 2024-07-18

430,282 downloads per month
Used in 806 crates (19 directly)

MIT license

130KB
1.5K SLoC

This library provides macros to define compile-time byte slices and arrays from encoded strings (using common bases like base64, base32, or hexadecimal, and also custom bases). It also provides a macro to define compile-time custom encodings to be used with the data-encoding crate.

See the documentation for more details.

Up to Rust 1.50, you may need to add the following to your .cargo/config.toml to use this library in no-std or no-alloc environments:

[unstable]
features = ["host_dep"]

From Rust 1.51, you may need to add the following to your Cargo.toml:

[package]
resolver = "2"

lib.rs:

Macros for data-encoding

This library provides macros to define compile-time byte arrays from encoded strings (using common bases like base64, base32, or hexadecimal, and also custom bases). It also provides a macro to define compile-time custom encodings to be used with the data-encoding crate at run-time.

Up to Rust 1.50, you may need to add the following to your .cargo/config.toml to use this library in no-std or no-alloc environments:

[unstable]
features = ["host_dep"]

From Rust 1.51, you may need to add the following to your Cargo.toml:

[package]
resolver = "2"

Examples

You can define a compile-time byte slice from an encoded string literal:

const HELLO_SLICE: &'static [u8] = &data_encoding_macro::hexlower!("68656c6c6f");
const FOOBAR_SLICE: &'static [u8] = &data_encoding_macro::base64!("Zm9vYmFy");

You can also define a compile-time byte array from an encoded string literal:

data_encoding_macro::hexlower_array!("const HELLO" = "68656c6c6f");
data_encoding_macro::base64_array!("const FOOBAR" = "Zm9vYmFy");

You can define a compile-time custom encoding from its specification:

const HEX: data_encoding::Encoding = data_encoding_macro::new_encoding! {
    symbols: "0123456789abcdef",
    translate_from: "ABCDEF",
    translate_to: "abcdef",
};
const BASE64: data_encoding::Encoding = data_encoding_macro::new_encoding! {
    symbols: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
    padding: '=',
};

Dependencies

~1.5MB
~35K SLoC