12 releases

0.1.12 May 27, 2021
0.1.10 Nov 2, 2020
0.1.8 Feb 22, 2020
0.1.7 Apr 22, 2019
0.1.1 Nov 18, 2017

#260 in Encoding

Download history 11064/week @ 2021-06-26 12969/week @ 2021-07-03 11876/week @ 2021-07-10 11188/week @ 2021-07-17 11716/week @ 2021-07-24 15838/week @ 2021-07-31 15861/week @ 2021-08-07 11820/week @ 2021-08-14 19152/week @ 2021-08-21 19068/week @ 2021-08-28 18968/week @ 2021-09-04 24013/week @ 2021-09-11 18783/week @ 2021-09-18 19810/week @ 2021-09-25 19581/week @ 2021-10-02 20714/week @ 2021-10-09

34,060 downloads per month
Used in 122 crates (5 directly)

MIT license

125KB
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");
# fn main() {}

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");
# fn main() {}

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: '=',
};
# fn main() {}

Dependencies

~295–700KB
~18K SLoC