feat: update to sjon

This commit is contained in:
2022-07-17 12:55:15 +08:00
parent 0741768d29
commit 30ac42001b

View File

@@ -1,4 +1,5 @@
use std::time::Duration; use std::time::Duration;
use anyhow::Result; use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmtime::{Config, Engine, Instance, Linker, Module, Store}; use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
@@ -11,11 +12,23 @@ struct FetchResult {
result: Option<String>, result: Option<String>,
} }
impl FetchResult {
fn to_json(&self) -> String {
serde_json::to_string(&self).expect("JSResult to json error")
}
}
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
struct JsResult { struct JsResult {
message: String, 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<reqwest::blocking::Response> { pub fn get(url: &str) -> reqwest::Result<reqwest::blocking::Response> {
let client = reqwest::blocking::Client::builder() let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(8)) .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(); let url: String = s.chars().skip(1).take(s.len() - 2).collect();
println!("fetch arguments URL: {}", url); println!("fetch arguments URL: {}", url);
let r = match get(&url) { let r = match get(&url) {
Err(e) => return serde_json::to_string(&FetchResult { Err(e) => return FetchResult {
error: Some(serde_json::to_string(&JsResult { error: Some(JsResult {
message: format!("failed: {}", e) message: format!("failed: {}", e)
}).expect("to json failed.4")), }.to_json()),
result: None, result: None,
}).expect("to json failed.3"), }.to_json(),
Ok(r) => r, Ok(r) => r,
}; };
serde_json::to_string(&FetchResult { FetchResult {
error: None, error: None,
result: Some(serde_json::to_string(&JsResult { result: Some(JsResult {
message: format!("fetched: {:?}", r.text()), message: format!("fetched: {:?}", r.text()),
}).expect("to json failed.1")), }.to_json()),
}).expect("to json failed.2") }.to_json()
} }
} }