Skip to content

Commit

Permalink
feat(config): accept 0/1 as valid env var config values
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Dec 10, 2024
1 parent e0f9a6b commit 8402c54
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<bool>,

/// The default package manager to be invoked.
pub default_pm: Option<String>,
}

fn option_bool_from_str_or_int<'de, D: Deserializer<'de>>(de: D) -> Result<Option<bool>, 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.
Expand Down

0 comments on commit 8402c54

Please sign in to comment.