Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration support #115

Closed
2 tasks
JunichiSugiura opened this issue Sep 24, 2022 · 0 comments · Fixed by #116 or #117
Closed
2 tasks

Configuration support #115

JunichiSugiura opened this issue Sep 24, 2022 · 0 comments · Fixed by #116 or #117
Assignees
Labels
core enhancement New feature or request epic

Comments

@JunichiSugiura
Copy link
Member

JunichiSugiura commented Sep 24, 2022

User story

As an application developer, I'd like a plugin that allows users to easily override app default configuration.

Example

[dependency]
config = "0.13"
serde = { version = "1.0", features = ["derive"] }
dirs = "4.0"
use dip::prelude::*;

fn main() {
    App::new()
        .add_plugin(ConfigPlugin)
        .add_system(log_config)
        .run();
}

#[ConfigPlugin]
#[derive(Debug, Deserialize)]
struct Config {
    base_url: String,
    backend: Backend,
}

#[derive(Debug, Deserialize)]
struct Backend {
    api_key: String,
    api_secret: String,
}

fn log_config(config: Res<Config>) {
    println!("{config:#?}");
}

Specs

User config file path priority

  1. env
  2. ./{CARGO_PKG_NAME} (e.g. project root): mainly for development purposes
  3. $HOME/.{CARGO_PKG_NAME}
  4. $HOME/.config/{CARGO_PKG_NAME}/{CARGO_PKG_NAME}
  5. $HOME/.config/{CARGO_PKG_NAME}

Supported file types

mehcode/config-rs#feature-flags

  • toml
  • yaml
  • json
  • json5
  • ini
  • ron

Cross-platform path

dirs-dev/dirs-rs#features

  • home_dir
    • Linux/Redox: Some($HOME)
    • Windows: Some({FOLDERID_Profile})
    • macOS: Some($HOME)
  • config_dir
    • Linux/Redox: Some($XDG_CONFIG_HOME) or Some($HOME/.config)
    • Windows: Some({FOLDERID_RoamingAppData})
    • macOS: Some($HOME/Library/Application Support)

ConfigPlugin derive macro

Options

  • CONFIG_PATH env
    • mainly for development or testing purposes
    • e.g. CONFIG_PATH=config/development cargo run
  • default_paths
    • enable/disable default lookup paths
    • e.g.
      ConfigPlugin::new().default_paths(false)
    • default to true
  • default_from_str
    • To override lookup path for default file (eventually included in binary)
    • default to "config/default.toml"
    • e.g.
      ConfigPlugin:new().default_from_str("config/default.yaml")
  • default_file_format
    • To override default file format
    • e.g.
      ConfigPlugin::new().default_file_format(config::FileFormat::Yaml)
    • default to config::FileFormat::Toml
  • env_prefix
    • e.g.
      ConfigPlugin::new().env_prefix("APP") // → `MYAPP__BASE_URL` instead of `BASE_URL`
    • default to ""
  • env_separator
    • e.g.
      ConfigPlugin::new().env_separator("___") // → BACKEND___API_KEY
    • default to "__"
      • to avoid conflicts between field name with underscore and nested field name

Watch support

Async source support

  • Fetch config file remotely
  • example

Limitation

  • only read, no write support for now

Dependencies

  • mehcode/config-rs: ⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
  • serde-rs/serde: Serialization framework for Rust
  • dirs-dev/dirs-rs: a low-level library that provides config/cache/data paths, following the respective conventions on Linux, macOS and Windows
@JunichiSugiura JunichiSugiura self-assigned this Sep 24, 2022
This was referenced Sep 24, 2022
@JunichiSugiura JunichiSugiura added enhancement New feature or request CLI dip_cli related desktop dip_desktop related labels Sep 25, 2022
@JunichiSugiura JunichiSugiura changed the title Config file support Configuration support Sep 25, 2022
This was linked to pull requests Oct 10, 2022
@JunichiSugiura JunichiSugiura added core and removed CLI dip_cli related desktop dip_desktop related server labels Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core enhancement New feature or request epic
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant