A procedural macro for configuring constant values across crates

Overview

toml-cfg

Rough ideas:

  • Crates can declare variables that can be overridden
    • Anything const, e.g. usize, strings, etc.
  • (Only) The "root crate" can override these variables by including a cfg.toml file

Config file

# a toml-cfg file

[lib-one]
buffer_size = 4096

[lib-two]
greeting = "Guten tag!"

In the library

// lib-one
#[toml_cfg::toml_config]
pub struct Config {
    #[default(32)]
    buffer_size: usize,
}

// lib-two
#[toml_cfg::toml_config]
pub struct Config {
    #[default("hello")]
    greeting: &'static str,
}

Look at what we get!

# Print the "buffer_size" value from the `lib-one` crate.
# Since it has no cfg.toml, we just get the default value.
$ cd pkg-example/lib-one
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/lib-one`
32

# Print the "greeting" value from the `lib-two` crate.
# Since it has no cfg.toml, we just get the default value.
$ cd ../lib-two
$ cargo run
   Compiling lib-two v0.1.0 (/home/james/personal/toml-cfg/pkg-example/lib-two)
    Finished dev [unoptimized + debuginfo] target(s) in 0.32s
     Running `target/debug/lib-two`
hello

# Print the "buffer_size" value from `lib-one`, and "greeting"
# from `lib-two`. Since we HAVE defined a `cfg.toml` file, the
# values defined there are used instead.
$ cd ../application
$ cargo run
   Compiling lib-two v0.1.0 (/home/james/personal/toml-cfg/pkg-example/lib-two)
   Compiling application v0.1.0 (/home/james/personal/toml-cfg/pkg-example/application)
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/application`
4096
Guten tag!
You might also like...
A Rust macro for writing nested loop expressions

loop_chain A Rust macro for writing nested loop expressions Usage | Examples | Docs Dependencies [dependencies] loop_chain = "0.1.1" Usage For express

Macro assembler for Rust

Macro Assembler This crate implement JSC/SpiderMonkey like macro assembler. Macro assembler purpose is to generate machine code for different platform

A Rust attribute macro that adds memoization to a function (rhymes with Mickey)

michie (sounds like Mickey) — an attribute macro that adds memoization to a function. Table of contents Features Non-features key_expr key_type store_

Proc macro implementation of #[naked]

#[naked] Documentation This crate provide a proc macro version of the #[naked] attribute which can be used on stable Rust. Example // The SYSV64 calli

Macro for fast implementing serialize methods in serde::Serializer trait

impl_serialize! This library provides a simple procedural macro for fast implementing serialize methods in serde::Serializer trait. [dependencies] imp

hashmap macro for creating hashmap from provided key/value pairs

HashMap Macro Creates a HashMap from provided key/value pairs. Usage use std::collections::HashMap; use hashmap_macro::hashmap; let m: HashMap&str,

Library and proc macro to analyze memory usage of data structures in rust.
Library and proc macro to analyze memory usage of data structures in rust.

Allocative: memory profiler for Rust This crate implements a lightweight memory profiler which allows object traversal and memory size introspection.

Simple Rust derive-macro that simplifies integral enum creation

Integral enum A simple way to define integer-like enums. This macro implements bunch of traits that are usually implemented via looooong derive(..) at

proc-macro to help with using surrealdb's custom functions

SurrealDB Functions This is a proc-macro crate that given a path to a .surql file or a folder of .surql files, will parse DEFINE FUNCTION fn::s inside

Comments
  • Configuration file changes aren't automatically detected

    Configuration file changes aren't automatically detected

    Unfortunately, changing the cfg.toml does not prompt the macro to be re-invoked. For now, you'll need to run cargo clean after changing the config file.

    If you know how to fix this, please let me know!

    opened by jamesmunns 1
  • Valid config not found for cargo doc --open command

    Valid config not found for cargo doc --open command

    Unexpected behaviour of the TOML_CFG=require_cfg_present env:

    The cargo doc --open fails when TOML_CFG=require_cfg_present is set, even when there is an existing valid cfg.toml file. The cargo build, nonetheless, works as expected (i.e. fails only when the cfg.toml file is missing).

    Error:

    error: custom attribute panicked
      --> src\main.rs:10:1
       |
    10 | #[toml_cfg::toml_config]
       | ^^^^^^^^^^^^^^^^^^^^^^^^
       |
       = help: message: TOML_CFG=require_cfg_present set, but valid config not found!
    
    error: Compilation failed, aborting rustdoc
    
    error: could not document `tst-rust-esp32`
    
    opened by theguardian58 0
  • configuration file name

    configuration file name

    I've mistakenly used config.toml as file name several times, so I'd suggest a rename. However, a bare config.toml name would cause confusion with .cargo/config.toml, so how about something along the lines of project-config.toml or crate-config.toml?

    opened by spookyvision 0
Owner
James Munns
Bringing Rust to new places, big and small. Resources Team @ Rust Embedded Working Group
James Munns
A procedural macro to generate a new function implementation for your struct.

Impl New ?? A procedural macro to generate a new function implementation for your struct. ?? Add to your project Add this to your Cargo.toml: [depende

Mohammed Alotaibi 4 Sep 8, 2023
"Crates for Cheese" is a Rust collection library of those crates I consider a useful "extended standard".

cfc The purpose of this library is to provide a minimal list of currated crates which enhance the std library. In addition, most or all crates in this

null 0 Dec 23, 2021
Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]

Rust Latam: procedural macros workshop This repo contains a selection of projects designed to learn to write Rust procedural macros — Rust code that g

David Tolnay 2.5k Dec 29, 2022
Simple and customizable procedural noise generation library written in Rust.

libnoise A simple, performant, and customizable procedural noise generation library inspired by libnoise for C++ featuring: Easy coherent noise genera

SpoogieOogie 29 Aug 13, 2023
CloudLLM is a Rust library designed to seamlessly bridge applications with remote Language Learning Models (LLMs) across various platforms.

CloudLLM CloudLLM is a Rust library designed to seamlessly bridge applications with remote Language Learning Models (LLMs) across various platforms. W

null 4 Oct 13, 2023
UNIC: Unicode and Internationalization Crates for Rust

UNIC: Unicode and Internationalization Crates for Rust https://github.com/open-i18n/rust-unic UNIC is a project to develop components for the Rust pro

open-i18n — Open Internationalization Initiative 219 Nov 12, 2022
List public items (public API) of Rust library crates. Enables diffing public API between releases.

cargo wrapper for this library You probably want the cargo wrapper to this library. See https://github.com/Enselic/cargo-public-items. public_items Li

Martin Nordholts 20 Dec 26, 2022
Rust crates with map and set with interval keys (ranges x..y).

This crates implements map and set with interval keys (ranges x..y). IntervalMap is implemented using red-black binary tree, where each node contains

Timofey Prodanov 8 Aug 23, 2022
Game development practices with Rust programming language. I want to use different crates for this.

Hazır Oyun Motorlarını Kullanarak Rust Dili Yardımıyla Oyunlar Geliştirmek Rust programlama dilinde oyun geliştirmek için popüler birkaç hazır çatıyı

Burak Selim Senyurt 16 Dec 27, 2022
A collection of crates to make minecraft development (client, server) with rust possible.

rust-craft rust-craft is a collection of crates to make minecraft development (client, server) with rust possible. Motivation There's no better way of

João Victor 15 Mar 23, 2023