fix: don't overwrite config file

So far we have written a default config file if we encountered any error
when trying to read the one that should exist. That included:
* missing key-value-pairs,
* empty file, and
* a non-existant file.

Therefore, when a field is added to the Config struct, the entire config
file was overwritten with a default config. This occured during #20.

Instead, we now provide default values in form of generator functions to
serde. Any value that is present in the config is then parsed and
replaces the default value. Only if the config file does not exist we
create an empty one.

Closes #26.
This commit is contained in:
steven-omaha
2023-05-19 17:41:33 +02:00
parent 1237e7ed3b
commit 4a1d67847e
4 changed files with 48 additions and 17 deletions
@@ -11,7 +11,7 @@ use crate::{impl_backend_constants, Group, Package};
#[derive(Debug, Clone)]
pub struct Arch {
pub(crate) binary: String,
pub(crate) aur_rm_args: Option<Vec<String>>,
pub(crate) aur_rm_args: Vec<String>,
pub(crate) packages: HashSet<Package>,
}
@@ -72,9 +72,7 @@ impl Backend for Arch {
let mut cmd = Command::new(&self.binary);
cmd.args(self.get_switches_remove());
if let Some(rm_args) = &self.aur_rm_args {
cmd.args(rm_args);
}
cmd.args(&self.aur_rm_args);
if noconfirm {
cmd.args(self.get_switches_noconfirm());
@@ -124,7 +122,7 @@ impl Arch {
pub(crate) fn new() -> Self {
Self {
binary: BINARY.to_string(),
aur_rm_args: None,
aur_rm_args: vec![],
packages: HashSet::new(),
}
}
+15 -2
View File
@@ -10,17 +10,30 @@ use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
/// The AUR helper to use for Arch Linux.
#[serde(default = "aur_helper")]
pub aur_helper: String,
/// Additional arguments to pass to `aur_helper` when removing a package.
pub aur_rm_args: Option<Vec<String>>,
#[serde(default)]
pub aur_rm_args: Vec<String>,
/// Install Flatpak packages system-wide
#[serde(default = "yes")]
pub flatpak_systemwide: bool,
/// Warn the user when a group is not a symlink.
#[serde(default = "yes")]
pub warn_not_symlinks: bool,
/// Backends the user does not want to use even though the binary exists.
#[serde(default)]
pub disabled_backends: Vec<String>,
}
fn yes() -> bool {
true
}
fn aur_helper() -> String {
"paru".into()
}
impl Config {
/// Load the config from the associated file.
///
@@ -69,7 +82,7 @@ impl Default for Config {
fn default() -> Self {
Self {
aur_helper: "paru".into(),
aur_rm_args: None,
aur_rm_args: vec![],
flatpak_systemwide: true,
warn_not_symlinks: true,
disabled_backends: vec![],
+1 -1
View File
@@ -125,7 +125,7 @@ impl Pacdef {
{
if let Some(arch) = backend.as_any_mut().downcast_mut::<crate::backend::Arch>() {
arch.binary = self.config.aur_helper.clone();
arch.aur_rm_args = self.config.aur_rm_args.take();
arch.aur_rm_args = self.config.aur_rm_args.clone();
}
}