#settings #environment #template

scuffle-settings

Tools for managing configuration from environment variables or config files

8 releases

Uses new Rust 2024

new 0.1.4 May 17, 2025
0.1.3 May 15, 2025
0.1.2 Apr 27, 2025
0.1.1 Feb 21, 2025
0.0.0 Nov 28, 2024

#104 in Configuration

Download history 1/week @ 2025-01-26 23/week @ 2025-02-02 262/week @ 2025-02-09 162/week @ 2025-02-16 39/week @ 2025-02-23 29/week @ 2025-03-02 12/week @ 2025-03-09 12/week @ 2025-03-16 3/week @ 2025-03-23 4/week @ 2025-04-06 3/week @ 2025-04-13 122/week @ 2025-04-27 18/week @ 2025-05-04 147/week @ 2025-05-11

287 downloads per month
Used in scuffle-image-processor

MIT/Apache

39KB
477 lines

scuffle-settings

[!WARNING]
This crate is under active development and may not be stable.

License: MIT OR Apache-2.0 docs.rs crates.io GitHub Actions: ci Codecov


A crate designed to provide a simple interface to load and manage settings.

This crate is a wrapper around the config crate and clap crate to provide a simple interface to load and manage settings.

See the changelog for a full release history.

Feature flags

  • cli — Enables cli parsing using clap
  • ron — Enables the ron format
  • toml — Enables the toml format
  • yaml — Enables the yaml format
  • json — Enables the json format
  • json5 — Enables the json5 formast
  • ini — Enables the ini format
  • all-formats — Enables all formats
  • templates — Enables templating support via jinja
  • bootstrap — Enables scuffle-bootstrap support
  • full — Enables everything
  • docs — Enables changelog and documentation of feature flags

Examples

With scuffle_bootstrap

// Define a config struct like this
// You can use all of the serde attributes to customize the deserialization
#[derive(serde_derive::Deserialize)]
struct MyConfig {
    some_setting: String,
    #[serde(default)]
    some_other_setting: i32,
}

// Implement scuffle_boostrap::ConfigParser for the config struct like this
scuffle_settings::bootstrap!(MyConfig);

/// Our global state
struct Global;

impl scuffle_bootstrap::global::Global for Global {
    type Config = MyConfig;

    async fn init(config: MyConfig) -> anyhow::Result<Arc<Self>> {
        // Here you now have access to the config
        Ok(Arc::new(Self))
    }
}

Without scuffle_bootstrap

// Define a config struct like this
// You can use all of the serde attributes to customize the deserialization
#[derive(serde_derive::Deserialize)]
struct MyConfig {
    some_setting: String,
    #[serde(default)]
    some_other_setting: i32,
}

// Parsing options
let options = scuffle_settings::Options {
    env_prefix: Some("MY_APP"),
    ..Default::default()
};
// Parse the settings
let settings: MyConfig = scuffle_settings::parse_settings(options)?;

See Options for more information on how to customize parsing.

Templates

If the templates feature is enabled, the parser will attempt to render the configuration file as a jinja template before processing it.

All environment variables set during execution will be available under the env variable inside the file.

Example TOML file:

some_setting = "${{ env.MY_APP_SECRET }}"

Use ${{ and }} for variables, {% and %} for blocks and {# and #} for comments.

Command Line Interface

The following options are available for the CLI:

  • --config or -c

    Path to a configuration file. This option can be used multiple times to load multiple files.

  • --override or -o

    Provide an override for a configuration value, in the format KEY=VALUE.

License

This project is licensed under the MIT or Apache-2.0 license. You can choose between one of them if you use this work.

SPDX-License-Identifier: MIT OR Apache-2.0

Dependencies

~1.3–9.5MB
~106K SLoC