From 8402c5438d80df3b09b3ee6e8623160adbc34c22 Mon Sep 17 00:00:00 2001 From: rami3l Date: Wed, 11 Dec 2024 00:07:32 +0800 Subject: [PATCH] feat(config): accept `0`/`1` as valid env var config values --- src/config.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4763898507..da4c1540ad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,9 +13,10 @@ use std::{env, path::PathBuf}; use figment::{ providers::{Env, Format, Toml}, + util::bool_from_str_or_int, Figment, Provider, }; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize}; use tap::prelude::*; /// The crate name. @@ -33,28 +34,33 @@ const CONFIG_FILE_ENV: &str = "PACAPTR_CONFIG"; #[allow(clippy::struct_excessive_bools)] pub struct Config { /// Perform a dry run. - #[serde(default)] + #[serde(default, deserialize_with = "bool_from_str_or_int")] pub dry_run: bool, /// Prevent reinstalling previously installed packages. - #[serde(default)] + #[serde(default, deserialize_with = "bool_from_str_or_int")] pub needed: bool, /// Answer yes to every question. - #[serde(default)] + #[serde(default, deserialize_with = "bool_from_str_or_int")] pub no_confirm: bool, /// Remove cache after installation. - #[serde(default)] + #[serde(default, deserialize_with = "bool_from_str_or_int")] pub no_cache: bool, /// Suppress log output. + #[serde(default, deserialize_with = "option_bool_from_str_or_int")] pub quiet: Option, /// The default package manager to be invoked. pub default_pm: Option, } +fn option_bool_from_str_or_int<'de, D: Deserializer<'de>>(de: D) -> Result, D::Error> { + bool_from_str_or_int(de).map(Some) +} + impl Config { /// Returns the value of the `quiet` flag if it is present, /// otherwise returns whether the current `stdout` is **not** a TTY.