use downcasting for configuring backend objects

This commit is contained in:
steven-omaha
2023-02-23 14:23:04 +01:00
parent 53091759e5
commit ee4fd5da37
3 changed files with 14 additions and 11 deletions
@@ -8,7 +8,7 @@ use anyhow::{Context, Result};
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::{impl_backend_constants, Group, Package};
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Arch {
pub(crate) binary: String,
pub(crate) aur_rm_args: Option<Vec<String>>,
@@ -9,7 +9,7 @@ use serde_json::Value;
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::{impl_backend_constants, Group, Package};
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Rust {
pub(crate) packages: HashSet<Package>,
}
+12 -9
View File
@@ -1,3 +1,4 @@
use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::fs::{remove_file, File};
use std::os::unix::fs::symlink;
@@ -9,7 +10,7 @@ use const_format::formatcp;
use crate::action::*;
use crate::args;
use crate::backend::{Backend, Backends, ToDoPerBackend};
use crate::backend::{Arch, Backend, Backends, ToDoPerBackend};
use crate::cmd::run_edit_command;
use crate::env::get_single_var;
use crate::path::get_group_dir;
@@ -107,15 +108,17 @@ impl Pacdef {
to_install
}
#[allow(clippy::as_conversions)]
fn overwrite_values_from_config(&mut self, backend: Box<dyn Backend>) -> Box<dyn Backend> {
if backend.get_section() == "arch" {
Box::new(crate::backend::Arch {
binary: self.config.aur_helper.clone(),
aur_rm_args: self.config.aur_rm_args.take(),
packages: HashSet::new(),
})
} else {
backend
let b = Box::new(&backend as &dyn Any);
match b.downcast_ref::<Arch>() {
Some(inner) => {
let mut arch = inner.clone();
arch.binary = self.config.aur_helper.clone();
arch.aur_rm_args = self.config.aur_rm_args.take();
Box::new(arch)
}
None => backend,
}
}