🔧 add config-rs
This commit is contained in:
@@ -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
19
src/settings.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user