figment_string

Crates.iofigment_string
lib.rsfigment_string
version0.1.0
sourcesrc
created_at2023-11-08 08:16:45.158895
updated_at2023-11-08 08:16:45.158895
descriptionForces figment config types to be parsed as strings
homepagehttps://github.com/squidpickles/figment_string
repositoryhttps://github.com/squidpickles/figment_string.git
max_upload_size
id1028847
size19,226
Kevin R (squidpickles)

documentation

https://docs.rs/figment_string

README

figment_string

Forces data parsed by a figment::Provider to be parsed as a string. See this GitHub issue and the figment::Providers::Env documentation for some background.

Ever see this error?

% env NAME=8080 cargo run
Error: invalid type: found unsigned int `8080`, expected a string for key "NAME"
 in environment variable(s)

Well, now you can do this, and it will work even with numbers or booleans or whatever as input:

# use figment::{Figment, providers::Env};
# use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
   #[serde(deserialize_with = "figment_string::deserialize_as_string")]
   name: String,
}

fn main() {
    temp_env::with_var("NAME", Some("8080"), || {
        let config: Config = Figment::new()
            .merge(figment::providers::Env::raw())
            .extract()
            .unwrap();
        println!("Hello, {}!", config.name);
    });
}
Commit count: 1

cargo fmt