Skip to content

Commit

Permalink
Merge pull request #41 from tsirysndr/downcast-installer-macro
Browse files Browse the repository at this point in the history
use `downcast_installer` macro
  • Loading branch information
tsirysndr authored Jun 9, 2023
2 parents d94861f + 51e324e commit 2545a19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 91 deletions.
101 changes: 10 additions & 91 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
dnf::DnfInstaller, git::GitInstaller, nix::NixInstaller, yum::YumInstaller,
zypper::ZypperInstaller, Installer,
},
macros::{add_vertex, add_vertex_with_condition},
macros::{add_vertex, add_vertex_with_condition, downcast_installer},
types::{
configuration::Configuration,
curl::{default_brew_installer, default_nix_installer},
Expand Down Expand Up @@ -38,96 +38,15 @@ impl From<Box<dyn Installer + 'static>> for Vertex {
.map(|x| x.to_string())
.collect(),
provider: installer.provider().to_string(),
apt: match installer.provider() {
"apt" => Some(
installer
.as_any()
.downcast_ref::<AptInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
brew: match installer.provider() {
"brew" => Some(
installer
.as_any()
.downcast_ref::<BrewInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
curl: match installer.provider() {
"curl" => Some(
installer
.as_any()
.downcast_ref::<CurlInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
git: match installer.provider() {
"git" => Some(
installer
.as_any()
.downcast_ref::<GitInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
nix: match installer.provider() {
"nix" => Some(
installer
.as_any()
.downcast_ref::<NixInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
yum: match installer.provider() {
"yum" => Some(
installer
.as_any()
.downcast_ref::<YumInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
dnf: match installer.provider() {
"dnf" => Some(
installer
.as_any()
.downcast_ref::<DnfInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
zypper: match installer.provider() {
"zypper" => Some(
installer
.as_any()
.downcast_ref::<ZypperInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
apk: match installer.provider() {
"apk" => Some(
installer
.as_any()
.downcast_ref::<ApkInstaller>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
},
apt: downcast_installer!("apt", installer, AptInstaller),
brew: downcast_installer!("brew", installer, BrewInstaller),
curl: downcast_installer!("curl", installer, CurlInstaller),
git: downcast_installer!("git", installer, GitInstaller),
nix: downcast_installer!("nix", installer, NixInstaller),
yum: downcast_installer!("yum", installer, YumInstaller),
dnf: downcast_installer!("dnf", installer, DnfInstaller),
zypper: downcast_installer!("zypper", installer, ZypperInstaller),
apk: downcast_installer!("apk", installer, ApkInstaller),
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ macro_rules! add_vertex_with_condition {
};
}

macro_rules! downcast_installer {
($label: expr,$installer: ident, $installer_type: ident) => {
match $installer.provider() {
$label => Some(
$installer
.as_any()
.downcast_ref::<$installer_type>()
.map(|x| x.clone())
.unwrap(),
),
_ => None,
}
};
}

pub(crate) use add_vertex;
pub(crate) use add_vertex_with_condition;
pub(crate) use apk_add;
Expand All @@ -331,6 +346,7 @@ pub(crate) use apt_install;
pub(crate) use brew_install;
pub(crate) use check_version;
pub(crate) use dnf_install;
pub(crate) use downcast_installer;
pub(crate) use exec_bash;
pub(crate) use exec_bash_with_output;
pub(crate) use exec_piped_sudo;
Expand Down

0 comments on commit 2545a19

Please sign in to comment.