From a1cb6772bfa238e30522b766ae77a51ddfef0b88 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 2 Apr 2024 11:38:15 -0400 Subject: [PATCH] Make conversions to `wasmtime::Result` postfix-friendly (#10082) This PR refactors the conversions to `wasmtime::Result` to use a trait so that we can perform the conversions in a postfix-friendly way. Release Notes: - N/A --- crates/extension/src/wasm_host/wit.rs | 10 ++ .../src/wasm_host/wit/since_v0_0_4.rs | 108 ++++++++---------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/crates/extension/src/wasm_host/wit.rs b/crates/extension/src/wasm_host/wit.rs index da14f0466423cf7e5d02ace11f5141c9e992af1a..a59b94a26510eb9d77189fa7cf8daddbcbaecd9b 100644 --- a/crates/extension/src/wasm_host/wit.rs +++ b/crates/extension/src/wasm_host/wit.rs @@ -119,3 +119,13 @@ impl Extension { } } } + +trait ToWasmtimeResult { + fn to_wasmtime_result(self) -> wasmtime::Result>; +} + +impl ToWasmtimeResult for Result { + fn to_wasmtime_result(self) -> wasmtime::Result> { + Ok(self.map_err(|error| error.to_string())) + } +} diff --git a/crates/extension/src/wasm_host/wit/since_v0_0_4.rs b/crates/extension/src/wasm_host/wit/since_v0_0_4.rs index cd1b429bb2fbbdfb0a705a9a5b9ea1fbad2c2be6..726fda08508c0aba515da01ab9f97e71b7c86a7c 100644 --- a/crates/extension/src/wasm_host/wit/since_v0_0_4.rs +++ b/crates/extension/src/wasm_host/wit/since_v0_0_4.rs @@ -1,3 +1,4 @@ +use crate::wasm_host::wit::ToWasmtimeResult; use crate::wasm_host::WasmState; use anyhow::{anyhow, Result}; use async_compression::futures::bufread::GzipDecoder; @@ -76,37 +77,34 @@ impl HostWorktree for WasmState { #[async_trait] impl ExtensionImports for WasmState { async fn node_binary_path(&mut self) -> wasmtime::Result> { - convert_result( - self.host - .node_runtime - .binary_path() - .await - .map(|path| path.to_string_lossy().to_string()), - ) + self.host + .node_runtime + .binary_path() + .await + .map(|path| path.to_string_lossy().to_string()) + .to_wasmtime_result() } async fn npm_package_latest_version( &mut self, package_name: String, ) -> wasmtime::Result> { - convert_result( - self.host - .node_runtime - .npm_package_latest_version(&package_name) - .await, - ) + self.host + .node_runtime + .npm_package_latest_version(&package_name) + .await + .to_wasmtime_result() } async fn npm_package_installed_version( &mut self, package_name: String, ) -> wasmtime::Result, String>> { - convert_result( - self.host - .node_runtime - .npm_package_installed_version(&self.work_dir(), &package_name) - .await, - ) + self.host + .node_runtime + .npm_package_installed_version(&self.work_dir(), &package_name) + .await + .to_wasmtime_result() } async fn npm_install_package( @@ -114,12 +112,11 @@ impl ExtensionImports for WasmState { package_name: String, version: String, ) -> wasmtime::Result> { - convert_result( - self.host - .node_runtime - .npm_install_packages(&self.work_dir(), &[(&package_name, &version)]) - .await, - ) + self.host + .node_runtime + .npm_install_packages(&self.work_dir(), &[(&package_name, &version)]) + .await + .to_wasmtime_result() } async fn latest_github_release( @@ -127,29 +124,28 @@ impl ExtensionImports for WasmState { repo: String, options: GithubReleaseOptions, ) -> wasmtime::Result> { - convert_result( - maybe!(async { - let release = util::github::latest_github_release( - &repo, - options.require_assets, - options.pre_release, - self.host.http_client.clone(), - ) - .await?; - Ok(GithubRelease { - version: release.tag_name, - assets: release - .assets - .into_iter() - .map(|asset| GithubReleaseAsset { - name: asset.name, - download_url: asset.browser_download_url, - }) - .collect(), - }) + maybe!(async { + let release = util::github::latest_github_release( + &repo, + options.require_assets, + options.pre_release, + self.host.http_client.clone(), + ) + .await?; + Ok(GithubRelease { + version: release.tag_name, + assets: release + .assets + .into_iter() + .map(|asset| GithubReleaseAsset { + name: asset.name, + download_url: asset.browser_download_url, + }) + .collect(), }) - .await, - ) + }) + .await + .to_wasmtime_result() } async fn current_platform(&mut self) -> Result<(Os, Architecture)> { @@ -199,7 +195,7 @@ impl ExtensionImports for WasmState { path: String, file_type: DownloadedFileType, ) -> wasmtime::Result> { - let result = maybe!(async { + maybe!(async { let path = PathBuf::from(path); let extension_work_dir = self.host.work_dir.join(self.manifest.id.as_ref()); @@ -273,8 +269,8 @@ impl ExtensionImports for WasmState { Ok(()) }) - .await; - convert_result(result) + .await + .to_wasmtime_result() } async fn make_file_executable(&mut self, path: String) -> wasmtime::Result> { @@ -288,18 +284,12 @@ impl ExtensionImports for WasmState { use std::fs::{self, Permissions}; use std::os::unix::fs::PermissionsExt; - return convert_result( - fs::set_permissions(&path, Permissions::from_mode(0o755)).map_err(|error| { - anyhow!("failed to set permissions for path {path:?}: {error}") - }), - ); + return fs::set_permissions(&path, Permissions::from_mode(0o755)) + .map_err(|error| anyhow!("failed to set permissions for path {path:?}: {error}")) + .to_wasmtime_result(); } #[cfg(not(unix))] Ok(Ok(())) } } - -fn convert_result(result: Result) -> wasmtime::Result> { - Ok(result.map_err(|error| error.to_string())) -}