mirror of
https://github.com/OMGeeky/confique.git
synced 2026-02-23 15:38:30 +01:00
Change API of parse_env functions
I think this is an overall improvement. A few things done here: - They are available as `::env::parse` instead of `::env_utils`. The `env` module gets functions of its own soon enough. - Renamed functions to be shorter: `list_by_sep`, `list_by_comma`, ... - The docs were adjusted. One example is enough. And the functions with a fixed separator don't need the full docs again. That way we can avoid the `paste` dependency. - Functions now always return `Result<C, _>`. While the previous version was slightly more flexible, I don't think anyone would benefit from that flexibility (as `parse_env` requires `Result<_, _>` anyway) and this way it's a bit clearer what the function does, especially for those who don't know the nifty `FromIterator for Result` impl. - The example was adjusted accordingly. I also changed the names to obviously dummy names as I didn't know the existing names and don't want to spend time investigating whether I want their names in my code base :D
This commit is contained in:
@@ -1,39 +1,24 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use confique::{
|
||||
env_utils::{
|
||||
to_collection_by_char_separator, to_collection_by_comma, to_collection_by_semicolon,
|
||||
},
|
||||
Config,
|
||||
};
|
||||
use confique::Config;
|
||||
use std::{collections::HashSet, num::NonZeroU64, path::PathBuf, str::FromStr, convert::Infallible};
|
||||
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
struct Conf {
|
||||
#[config(
|
||||
env = "PATHS",
|
||||
parse_env = to_collection_by_comma
|
||||
)]
|
||||
#[config(env = "PATHS", parse_env = confique::env::parse::list_by_colon)]
|
||||
paths: HashSet<PathBuf>,
|
||||
#[config(
|
||||
env = "PORTS",
|
||||
parse_env = to_collection_by_semicolon
|
||||
)]
|
||||
|
||||
#[config(env = "PORTS", parse_env = confique::env::parse::list_by_comma)]
|
||||
ports: Vec<u16>,
|
||||
#[config(
|
||||
env = "NAMES",
|
||||
parse_env = to_collection_by_char_separator::<'|', _, _>
|
||||
)]
|
||||
|
||||
#[config(env = "NAMES", parse_env = confique::env::parse::list_by_sep::<'|', _, _>)]
|
||||
names: Vec<String>,
|
||||
#[config(
|
||||
env = "TIMEOUT",
|
||||
parse_env = NonZeroU64::from_str,
|
||||
)]
|
||||
|
||||
#[config(env = "TIMEOUT", parse_env = NonZeroU64::from_str)]
|
||||
timeout_seconds: NonZeroU64,
|
||||
#[config(
|
||||
env = "FORMATS",
|
||||
parse_env = parse_formats,
|
||||
)]
|
||||
|
||||
#[config(env = "FORMATS", parse_env = parse_formats)]
|
||||
formats: Vec<Format>,
|
||||
}
|
||||
|
||||
@@ -45,6 +30,7 @@ enum Format {
|
||||
Yaml,
|
||||
}
|
||||
|
||||
/// Example custom parser.
|
||||
fn parse_formats(input: &str) -> Result<Vec<Format>, Infallible> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
@@ -66,11 +52,8 @@ fn parse_formats(input: &str) -> Result<Vec<Format>, Infallible> {
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
std::env::set_var("PATHS", "/bin/ls,/usr/local/bin,/usr/bin/ls");
|
||||
std::env::set_var("PORTS", "8080;8888;8000");
|
||||
std::env::set_var(
|
||||
"NAMES",
|
||||
"Mervinc Harmon|Alfreda Valenzuela|Arlen Cabrera|Damon Rice|Willie Schwartz",
|
||||
);
|
||||
std::env::set_var("PORTS", "8080,8888,8000");
|
||||
std::env::set_var("NAMES", "Alex|Peter|Mary");
|
||||
std::env::set_var("TIMEOUT", "100");
|
||||
std::env::set_var("FORMATS", "json5,yaml;.env");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user