🔧 add config-rs

This commit is contained in:
2022-12-03 22:50:12 -05:00
parent b79912b352
commit 1468631245
4 changed files with 525 additions and 19 deletions

View File

@@ -1,5 +1,7 @@
use clap::{Parser, Subcommand};
mod settings;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
@@ -14,6 +16,9 @@ enum Commands {
}
fn main() {
let cfg = settings::Settings::new();
println!("Config parsed: {:?}", cfg);
let cli = Cli::parse();
match &cli.command {

19
src/settings.rs Normal file
View File

@@ -0,0 +1,19 @@
use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Settings {
pub foo: Option<String>,
}
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let s = Config::builder()
.add_source(File::with_name("marvin").required(false))
.add_source(Environment::with_prefix("marvin"))
.build()?;
s.try_deserialize()
}
}