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
This commit is contained in:
Marshall Bowers 2024-04-02 11:38:15 -04:00 committed by GitHub
parent c62239e9f0
commit a1cb6772bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 59 deletions

View file

@ -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()))
}
}

View file

@ -1,3 +1,4 @@
use crate::wasm_host::wit::ToWasmtimeResult;
use crate::wasm_host::WasmState; use crate::wasm_host::WasmState;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use async_compression::futures::bufread::GzipDecoder; use async_compression::futures::bufread::GzipDecoder;
@ -76,37 +77,34 @@ impl HostWorktree for WasmState {
#[async_trait] #[async_trait]
impl ExtensionImports for WasmState { impl ExtensionImports for WasmState {
async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> { async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> {
convert_result( self.host
self.host .node_runtime
.node_runtime .binary_path()
.binary_path() .await
.await .map(|path| path.to_string_lossy().to_string())
.map(|path| path.to_string_lossy().to_string()), .to_wasmtime_result()
)
} }
async fn npm_package_latest_version( async fn npm_package_latest_version(
&mut self, &mut self,
package_name: String, package_name: String,
) -> wasmtime::Result<Result<String, String>> { ) -> wasmtime::Result<Result<String, String>> {
convert_result( self.host
self.host .node_runtime
.node_runtime .npm_package_latest_version(&package_name)
.npm_package_latest_version(&package_name) .await
.await, .to_wasmtime_result()
)
} }
async fn npm_package_installed_version( async fn npm_package_installed_version(
&mut self, &mut self,
package_name: String, package_name: String,
) -> wasmtime::Result<Result<Option<String>, String>> { ) -> wasmtime::Result<Result<Option<String>, String>> {
convert_result( self.host
self.host .node_runtime
.node_runtime .npm_package_installed_version(&self.work_dir(), &package_name)
.npm_package_installed_version(&self.work_dir(), &package_name) .await
.await, .to_wasmtime_result()
)
} }
async fn npm_install_package( async fn npm_install_package(
@ -114,12 +112,11 @@ impl ExtensionImports for WasmState {
package_name: String, package_name: String,
version: String, version: String,
) -> wasmtime::Result<Result<(), String>> { ) -> wasmtime::Result<Result<(), String>> {
convert_result( self.host
self.host .node_runtime
.node_runtime .npm_install_packages(&self.work_dir(), &[(&package_name, &version)])
.npm_install_packages(&self.work_dir(), &[(&package_name, &version)]) .await
.await, .to_wasmtime_result()
)
} }
async fn latest_github_release( async fn latest_github_release(
@ -127,29 +124,28 @@ impl ExtensionImports for WasmState {
repo: String, repo: String,
options: GithubReleaseOptions, options: GithubReleaseOptions,
) -> wasmtime::Result<Result<GithubRelease, String>> { ) -> wasmtime::Result<Result<GithubRelease, String>> {
convert_result( maybe!(async {
maybe!(async { let release = util::github::latest_github_release(
let release = util::github::latest_github_release( &repo,
&repo, options.require_assets,
options.require_assets, options.pre_release,
options.pre_release, self.host.http_client.clone(),
self.host.http_client.clone(), )
) .await?;
.await?; Ok(GithubRelease {
Ok(GithubRelease { version: release.tag_name,
version: release.tag_name, assets: release
assets: release .assets
.assets .into_iter()
.into_iter() .map(|asset| GithubReleaseAsset {
.map(|asset| GithubReleaseAsset { name: asset.name,
name: asset.name, download_url: asset.browser_download_url,
download_url: asset.browser_download_url, })
}) .collect(),
.collect(),
})
}) })
.await, })
) .await
.to_wasmtime_result()
} }
async fn current_platform(&mut self) -> Result<(Os, Architecture)> { async fn current_platform(&mut self) -> Result<(Os, Architecture)> {
@ -199,7 +195,7 @@ impl ExtensionImports for WasmState {
path: String, path: String,
file_type: DownloadedFileType, file_type: DownloadedFileType,
) -> wasmtime::Result<Result<(), String>> { ) -> wasmtime::Result<Result<(), String>> {
let result = maybe!(async { maybe!(async {
let path = PathBuf::from(path); let path = PathBuf::from(path);
let extension_work_dir = self.host.work_dir.join(self.manifest.id.as_ref()); let extension_work_dir = self.host.work_dir.join(self.manifest.id.as_ref());
@ -273,8 +269,8 @@ impl ExtensionImports for WasmState {
Ok(()) Ok(())
}) })
.await; .await
convert_result(result) .to_wasmtime_result()
} }
async fn make_file_executable(&mut self, path: String) -> wasmtime::Result<Result<(), String>> { 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::fs::{self, Permissions};
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
return convert_result( return fs::set_permissions(&path, Permissions::from_mode(0o755))
fs::set_permissions(&path, Permissions::from_mode(0o755)).map_err(|error| { .map_err(|error| anyhow!("failed to set permissions for path {path:?}: {error}"))
anyhow!("failed to set permissions for path {path:?}: {error}") .to_wasmtime_result();
}),
);
} }
#[cfg(not(unix))] #[cfg(not(unix))]
Ok(Ok(())) Ok(Ok(()))
} }
} }
fn convert_result<T>(result: Result<T>) -> wasmtime::Result<Result<T, String>> {
Ok(result.map_err(|error| error.to_string()))
}