From 07d86cf9a12aefa921b6f04db5409a27adf9bb5b Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 4 Nov 2024 16:40:33 -0800 Subject: [PATCH] Json --- src-tauri/src/lib.rs | 14 ++++++++++---- src/main.js | 4 +++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 3cdab1d..77f1054 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -19,12 +19,18 @@ fn os_info() -> String { //TODO make this return a JS object #[tauri::command] -async fn quote() -> String { - let Ok(res) = reqwest::get("https://perl.is/random").await else { return "ERROR".to_string() }; +async fn quote() -> serde_json::Value { + let Ok(res) = reqwest::get("https://perl.is/random").await else { return serde_json::json!({"error": "ERROR".to_string()}) }; println!("Status: {}", res.status()); - let Ok(text) = res.text().await else { return "TEXT ERROR".to_string() }; + let Ok(text) = res.text().await else { return serde_json::json!({ "error": "TEXT ERROR".to_string() }) }; - text + let value: serde_json::Value = serde_json::from_str(&text).unwrap(); + let output = value.get("quote").and_then(|quote| quote.as_str()).unwrap(); + + serde_json::json!({ + "response": output, + "code": 33 + }) } #[tauri::command] diff --git a/src/main.js b/src/main.js index 1523aaf..0888a8f 100644 --- a/src/main.js +++ b/src/main.js @@ -14,7 +14,9 @@ async function os_info(elem) { } async function get_quote(elem) { - elem.textContent = await invoke("quote", {}); + const output = await invoke("quote", {}); + console.log(output); + elem.textContent = output.response; }