diff --git a/__wasm/wit-bindgen-sample/container/src/main.rs b/__wasm/wit-bindgen-sample/container/src/main.rs index b58295f..029bad9 100644 --- a/__wasm/wit-bindgen-sample/container/src/main.rs +++ b/__wasm/wit-bindgen-sample/container/src/main.rs @@ -1,4 +1,5 @@ use std::time::Duration; + use anyhow::Result; use serde::{Deserialize, Serialize}; use wasmtime::{Config, Engine, Instance, Linker, Module, Store}; @@ -11,11 +12,23 @@ struct FetchResult { result: Option, } +impl FetchResult { + fn to_json(&self) -> String { + serde_json::to_string(&self).expect("JSResult to json error") + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] struct JsResult { message: String, } +impl JsResult { + fn to_json(&self) -> String { + serde_json::to_string(&self).expect("JSResult to json error") + } +} + pub fn get(url: &str) -> reqwest::Result { let client = reqwest::blocking::Client::builder() .timeout(Duration::from_secs(8)) @@ -36,21 +49,21 @@ impl container::Container for MyContainer { let url: String = s.chars().skip(1).take(s.len() - 2).collect(); println!("fetch arguments URL: {}", url); let r = match get(&url) { - Err(e) => return serde_json::to_string(&FetchResult { - error: Some(serde_json::to_string(&JsResult { + Err(e) => return FetchResult { + error: Some(JsResult { message: format!("failed: {}", e) - }).expect("to json failed.4")), + }.to_json()), result: None, - }).expect("to json failed.3"), + }.to_json(), Ok(r) => r, }; - serde_json::to_string(&FetchResult { + FetchResult { error: None, - result: Some(serde_json::to_string(&JsResult { + result: Some(JsResult { message: format!("fetched: {:?}", r.text()), - }).expect("to json failed.1")), - }).expect("to json failed.2") + }.to_json()), + }.to_json() } }