From 680a986cf66ad14a4a0d147ed34b8aa3d1ffaacd Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Mon, 11 Jan 2021 21:37:47 +0000 Subject: [PATCH] Add get_help function --- Cargo.toml | 2 +- src/shim.rs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd7a6636..b9fbe0a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosaic-tile" -version = "0.3.1" +version = "0.4.0" authors = ["Brooks J Rady "] edition = "2018" description = "A small client-side library for writing mosaic plugins (tiles)" diff --git a/src/shim.rs b/src/shim.rs index 60bdfbd5..6fa8b0e6 100644 --- a/src/shim.rs +++ b/src/shim.rs @@ -1,4 +1,4 @@ -use serde::{Deserialize, Serialize}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::{io, path::Path}; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -24,9 +24,7 @@ pub enum Key { } pub fn get_key() -> Key { - let mut json = String::new(); - io::stdin().read_line(&mut json).unwrap(); - serde_json::from_str(&json).unwrap() + deserialize_from_stdin().unwrap() } pub fn open_file(path: &Path) { @@ -39,8 +37,20 @@ pub fn set_selectable(selectable: bool) { unsafe { host_set_selectable(selectable) }; } +pub fn get_help() -> Vec { + unsafe { host_get_help() }; + deserialize_from_stdin().unwrap_or_default() +} + +fn deserialize_from_stdin() -> Option { + let mut json = String::new(); + io::stdin().read_line(&mut json).unwrap(); + serde_json::from_str(&json).ok() +} + #[link(wasm_import_module = "mosaic")] extern "C" { fn host_open_file(); fn host_set_selectable(selectable: i32); + fn host_get_help(); }