18 releases

0.1.18 Apr 12, 2025
0.1.17 Feb 9, 2025
0.1.16 Jan 14, 2025
0.1.15 Apr 28, 2024
0.1.1 Nov 18, 2017

#486 in Encoding

Download history 229215/week @ 2025-01-06 292807/week @ 2025-01-13 281591/week @ 2025-01-20 321511/week @ 2025-01-27 333651/week @ 2025-02-03 371957/week @ 2025-02-10 263771/week @ 2025-02-17 241877/week @ 2025-02-24 581659/week @ 2025-03-03 412918/week @ 2025-03-10 188982/week @ 2025-03-17 177731/week @ 2025-03-24 169675/week @ 2025-03-31 182321/week @ 2025-04-07 165673/week @ 2025-04-14 166764/week @ 2025-04-21

693,776 downloads per month
Used in 1,103 crates (21 directly)

MIT license

135KB
1.5K SLoC

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: '=',
};

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"

Dependencies

~205–640KB
~15K SLoC