From bb6b5cc1623d411d65c06f270c970584abd447df Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 27 Aug 2022 19:58:23 +0200 Subject: [PATCH] Change wrapper to builder pattern (fancy stuff) Signed-off-by: trivernis --- src/{wrapper => builder}/mod.rs | 0 src/{wrapper => builder}/pacman.rs | 37 ++++++++++++++---------------- src/main.rs | 2 +- src/operations/install.rs | 11 +++++---- 4 files changed, 24 insertions(+), 26 deletions(-) rename src/{wrapper => builder}/mod.rs (100%) rename src/{wrapper => builder}/pacman.rs (77%) diff --git a/src/wrapper/mod.rs b/src/builder/mod.rs similarity index 100% rename from src/wrapper/mod.rs rename to src/builder/mod.rs diff --git a/src/wrapper/pacman.rs b/src/builder/pacman.rs similarity index 77% rename from src/wrapper/pacman.rs rename to src/builder/pacman.rs index 2339b67..4d75cba 100644 --- a/src/wrapper/pacman.rs +++ b/src/builder/pacman.rs @@ -1,31 +1,13 @@ use crate::internal::{commands::ShellCommand, error::AppResult, structs::Options}; -pub struct PacmanWrapper; - -impl PacmanWrapper { - pub async fn install(args: PacmanInstallArgs) -> AppResult<()> { - let mut command = ShellCommand::pacman().elevated().arg("-S").arg("--needed"); - - if args.no_confirm { - command = command.arg("--noconfirm") - } - - if args.as_deps { - command = command.arg("--asdeps") - } - - command.args(args.packages).wait_success().await - } -} - #[derive(Debug, Default)] -pub struct PacmanInstallArgs { +pub struct PacmanInstallBuilder { packages: Vec, as_deps: bool, no_confirm: bool, } -impl PacmanInstallArgs { +impl PacmanInstallBuilder { pub fn from_options(options: Options) -> Self { Self::default() .as_deps(options.asdeps) @@ -50,4 +32,19 @@ impl PacmanInstallArgs { self } + + #[tracing::instrument(level = "debug")] + pub async fn install(self) -> AppResult<()> { + let mut command = ShellCommand::pacman().elevated().arg("-S").arg("--needed"); + + if self.no_confirm { + command = command.arg("--noconfirm") + } + + if self.as_deps { + command = command.arg("--asdeps") + } + + command.args(self.packages).wait_success().await + } } diff --git a/src/main.rs b/src/main.rs index 37dcac3..cf8b7f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,9 +15,9 @@ use tracing_subscriber::EnvFilter; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; mod args; +mod builder; mod internal; mod operations; -mod wrapper; #[tokio::main(flavor = "current_thread")] async fn main() { diff --git a/src/operations/install.rs b/src/operations/install.rs index be7408c..b014650 100644 --- a/src/operations/install.rs +++ b/src/operations/install.rs @@ -1,5 +1,5 @@ +use crate::builder::pacman::PacmanInstallBuilder; use crate::internal::exit_code::AppExitCode; -use crate::wrapper::pacman::{PacmanInstallArgs, PacmanWrapper}; use crate::{crash, info, log, Options}; pub async fn install(packages: Vec, options: Options) { @@ -11,10 +11,11 @@ pub async fn install(packages: Vec, options: Options) { log!("Installing from repos: {:?}", &packages); } - let result = PacmanWrapper::install( - PacmanInstallArgs::from_options(options).packages(packages.clone()), - ) - .await; + let result = PacmanInstallBuilder::from_options(options) + .packages(packages.clone()) + .install() + .await; + if result.is_err() { crash!( AppExitCode::PacmanError,