From 1ce41e332929852ca22535443101fb18535f7c0f Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Thu, 7 Nov 2024 15:06:28 +0100 Subject: [PATCH] Take actions by value (#672) --- crates/cli-tools/CHANGELOG.md | 1 + crates/cli-tools/src/action.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/cli-tools/CHANGELOG.md b/crates/cli-tools/CHANGELOG.md index 94ac1661..49814b76 100644 --- a/crates/cli-tools/CHANGELOG.md +++ b/crates/cli-tools/CHANGELOG.md @@ -4,6 +4,7 @@ ### Major +- Change `action::{Transfer,RustApplet{New,Build,Test}}` to consume `self` - Remove `Default` for `action::RustAppletBuild` (implement `clap::Parser` instead) - Add `action` feature to gate the `action` module - Change API to be async using tokio diff --git a/crates/cli-tools/src/action.rs b/crates/cli-tools/src/action.rs index c3c67c52..55aaef22 100644 --- a/crates/cli-tools/src/action.rs +++ b/crates/cli-tools/src/action.rs @@ -342,7 +342,7 @@ pub struct Transfer { impl Transfer { async fn run( - &self, connection: &mut dyn Connection, payload: impl AsRef, message: &'static str, + self, connection: &mut dyn Connection, payload: impl AsRef, message: &'static str, finish: Option>) -> Result>, ) -> Result<()> where @@ -364,14 +364,14 @@ impl Transfer { ) .with_message("Starting"); progress.enable_steady_tick(Duration::from_millis(200)); - connection.call::(Request::Start { dry_run: *dry_run }).await?.get(); + connection.call::(Request::Start { dry_run }).await?.get(); progress.set_message("Writing"); - for chunk in payload.chunks(*chunk_size) { + for chunk in payload.chunks(chunk_size) { progress.inc(chunk.len() as u64); connection.call::(Request::Write { chunk }).await?.get(); } progress.set_message("Finishing"); - match (*dry_run, finish) { + match (dry_run, finish) { (false, Some(finish)) => final_call::(connection, Request::Finish, finish).await?, _ => *connection.call::(Request::Finish).await?.get(), } @@ -456,23 +456,23 @@ pub struct RustAppletNew { } impl RustAppletNew { - pub async fn run(&self) -> Result<()> { + pub async fn run(self) -> Result<()> { let RustAppletNew { path, name } = self; let mut cargo = Command::new("cargo"); - cargo.args(["new", "--lib"]).arg(path); + cargo.args(["new", "--lib"]).arg(&path); if let Some(name) = name { cargo.arg(format!("--name={name}")); } cmd::execute(&mut cargo).await?; - cmd::execute(Command::new("cargo").args(["add", "wasefire"]).current_dir(path)).await?; + cmd::execute(Command::new("cargo").args(["add", "wasefire"]).current_dir(&path)).await?; let mut cargo = Command::new("cargo"); cargo.args(["add", "wasefire-stub", "--optional"]); - cmd::execute(cargo.current_dir(path)).await?; + cmd::execute(cargo.current_dir(&path)).await?; let mut sed = Command::new("sed"); sed.arg("-i"); sed.arg("s#^wasefire-stub\\( = .\"dep:wasefire-stub\"\\)#test\\1, \"wasefire/test\"#"); sed.arg("Cargo.toml"); - cmd::execute(sed.current_dir(path)).await?; + cmd::execute(sed.current_dir(&path)).await?; tokio::fs::remove_file(path.join("src/lib.rs")).await?; fs::write(path.join("src/lib.rs"), include_str!("data/lib.rs")).await?; Ok(()) @@ -512,7 +512,7 @@ pub struct RustAppletBuild { } impl RustAppletBuild { - pub async fn run(&self, dir: impl AsRef) -> Result<()> { + pub async fn run(self, dir: impl AsRef) -> Result<()> { let metadata = metadata(dir.as_ref()).await?; let package = &metadata.packages[0]; let target_dir = @@ -582,7 +582,7 @@ pub struct RustAppletTest { } impl RustAppletTest { - pub async fn run(&self, dir: impl AsRef) -> Result<()> { + pub async fn run(self, dir: impl AsRef) -> Result<()> { let metadata = metadata(dir.as_ref()).await?; let package = &metadata.packages[0]; ensure!(package.features.contains_key("test"), "missing test feature");