rust-cli / rust-cli/config-rs

Clarification on how to use "watch" in v0.13

Open
#317 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3.2k
Forks
265
Avg merge
2h 42m
Merged PRs (30d)
4

Description

Nice work. The refactoring in the latest version makes a lot of sense. I'm speaking based on my experience with the new use of the "builder" pattern.

I'm trying to workout how to "get from a to b" in such a way that I can use the ability to watch a configuration file. The type that I ended-up with does not support read nor write.

At some level, I recognize this is a bit of a naive question. Notwithstanding, here is what I have:

impl Settings {
    pub fn new() -> Result<Self, ConfigError> {
        let mode = env::var("RUST_ENV").unwrap_or_else(|_| "Development".into());

        let settings = Config::builder()
            .add_source(File::with_name(CONFIG_FILE_PATH))
            // use env Development, Production or Testing to set override
            .add_source(File::with_name(&format!("{}/{}", CONFIG_FILE_ROOT, mode)).required(false))
            .add_source(Environment::default().separator("_"))
            .build()?;

        settings.try_deserialize()
    }
}

lazy_static! {
    pub static ref CONFIG: Settings = Settings::new().unwrap();
}

Clearly I need to "hold-off" on instantiating the Settings so that I can use the read and write functions per the example.

async fn reload_config() -> impl IntoResponse {
    config::CONFIG.write().unwrap().refresh().unwrap();
    show_settings()
}
fn show_settings() -> String {
    format!(
        " * Settings :: \n\x1b[31m{:?}\x1b[0m",
        config::CONFIG
            .read()
            .unwrap()
            .clone()
            .try_deserialize::<HashMap<String, String>>()
            .unwrap()
    )
}

Without calling try_deserialize, and later the calling Settings::new().unwrap() to set the value of static ref, I don't get the expected type "Settings".

What is the type that I should be using to host the settings so that I can create the "Settings" instance upon the read and write combination?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the v0.13 builder and watch example, then trace the Config::builder, build, try_deserialize, read, write, and refresh calls shown in the issue. Determine the supported lifecycle and type for watched configuration, and document a minimal example covering initialization, refresh, and reading Settings.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.