static-toml

Effortlessly embed TOML files into your Rust code as static data with custom data structures.


Version License Docs CI

About

Embed TOML files into your Rust binaries via a procedural macro. This library enables the inclusion of TOML files at compile-time and generates static data structures that can be directly accessed by your Rust code without the need for runtime parsing.

Key Features

Usage

First, make sure to add static-toml to your Cargo.toml dependencies: Either by command line: shell cargo add static-toml Or by adding it to your Cargo.toml directly: toml [dependencies] static-toml = "1"

Then, make use of the static_toml! macro to include your TOML file: ```toml

message.toml

[info] welcome = "Welcome to our application!" update = "Your data has been updated successfully."

[errors] filenotfound = "The requested file could not be found." permission_denied = "You do not have permission to perform this action." ```

```rust statictoml::statictoml! { static MESSAGES = include_toml!("messages.toml"); }

const WELCOME_MESSAGE: &str = MESSAGES.info.welcome; ``` This will read your TOML file and generate Rust data structures accordingly. Now you can access the values from the TOML file with ease.

Customization Options

You can configure how the macro should generate data types: rust static_toml! { #[static_toml( prefix = Prefix, suffix = Suffix, root_mod = cfg, values_ident = items, prefer_slices = false )] static CONFIG = include_toml!("config.toml"); }

Enhancing Your Types

You can use doc comments, derive attributes, and other attributes. Additionally, you can set visibility. Your code can be clean and descriptive.

rust static_toml! { /// The configuration. #[derive(Debug)] #[allow(missing_docs)] pub static CONFIG = include_toml!("config.toml"); }

Error Handling

Encounter compile errors? No worries. static-toml provides informative error messages, whether it's TOML parsing or file access issues.

Example

Suppose you have a simple TOML file like this: ```toml

config.toml

[database] url = "localhost" port = 5432 ```

Use the static_toml! macro: rust static_toml! { static CONFIG = include_toml!("config.toml"); }

And just like that, you have Rust data structures ready to use. rust assert_eq!(CONFIG.database.url, "localhost"); assert_eq!(CONFIG.database.port, 5432);