Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Make conversions to `wasmtime::Result` postfix-friendly (#10082)
Marshall Bowersa1cb6772bfa2
diff --git a/crates/extension/src/wasm_host/wit.rs b/crates/extension/src/wasm_host/wit.rsindex da14f046642..a59b94a2651 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<T> {+ fn to_wasmtime_result(self) -> wasmtime::Result<Result<T, String>>;+}++impl<T> ToWasmtimeResult<T> for Result<T> {+ fn to_wasmtime_result(self) -> wasmtime::Result<Result<T, String>> {+ 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.rsindex cd1b429bb2f..726fda08508 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<Result<String, String>> {- 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<Result<String, String>> {- 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<Result<Option<String>, 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<Result<(), String>> {- 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<Result<GithubRelease, String>> {- 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<Result<(), String>> {- 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<Result<(), String>> {@@ -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<T>(result: Result<T>) -> wasmtime::Result<Result<T, String>> {- Ok(result.map_err(|error| error.to_string()))-}