main
Elnu 1 year ago
parent 09d93fac79
commit 62fe54b807

@ -3,11 +3,11 @@
mod render; mod render;
pub mod svg; pub mod svg;
use leptos::{*, ev::SubmitEvent, html::Input}; use derive_more::From;
use leptos::{ev::SubmitEvent, html::Input, *};
use strum_macros::IntoStaticStr;
use wasm_bindgen::{JsCast, JsValue}; use wasm_bindgen::{JsCast, JsValue};
use web_sys::File; use web_sys::File;
use strum_macros::IntoStaticStr;
use derive_more::From;
#[derive(Clone)] #[derive(Clone)]
struct ResultMessageData { struct ResultMessageData {
@ -55,7 +55,7 @@ enum ReadKleError {
#[strum(serialize = "Failed to parse file to string")] #[strum(serialize = "Failed to parse file to string")]
ParseToString, ParseToString,
#[strum(serialize = "Failed to parse KLE JSON")] #[strum(serialize = "Failed to parse KLE JSON")]
Serde(serde_json::Error) Serde(serde_json::Error),
} }
impl ToString for ReadKleError { impl ToString for ReadKleError {
@ -71,12 +71,17 @@ impl ToString for ReadKleError {
} }
} }
async fn read_kle_from_file(file: &ReadSignal<Option<File>>) -> Result<kle_serial::Keyboard, ReadKleError> { async fn read_kle_from_file(
file: &ReadSignal<Option<File>>,
) -> Result<kle_serial::Keyboard, ReadKleError> {
let file = match file() { let file = match file() {
Some(file) => file, Some(file) => file,
None => return Err(ReadKleError::NoFile), None => return Err(ReadKleError::NoFile),
}; };
let file_contents = match wasm_bindgen_futures::JsFuture::from(file.text()).await?.as_string() { let file_contents = match wasm_bindgen_futures::JsFuture::from(file.text())
.await?
.as_string()
{
Some(contents) => contents, Some(contents) => contents,
None => return Err(ReadKleError::ParseToString), None => return Err(ReadKleError::ParseToString),
}; };
@ -85,7 +90,10 @@ async fn read_kle_from_file(file: &ReadSignal<Option<File>>) -> Result<kle_seria
} }
#[component] #[component]
fn KeyboardFromFile(cx: Scope, set_keyboard: WriteSignal<Option<kle_serial::Keyboard>>) -> impl IntoView { fn KeyboardFromFile(
cx: Scope,
set_keyboard: WriteSignal<Option<kle_serial::Keyboard>>,
) -> impl IntoView {
let file_input = create_node_ref::<Input>(cx); let file_input = create_node_ref::<Input>(cx);
let (file, set_file) = create_signal(cx, Option::<File>::None); let (file, set_file) = create_signal(cx, Option::<File>::None);
let (result_message, set_result_message) = create_signal(cx, Option::<ResultMessageData>::None); let (result_message, set_result_message) = create_signal(cx, Option::<ResultMessageData>::None);
@ -98,20 +106,22 @@ fn KeyboardFromFile(cx: Scope, set_keyboard: WriteSignal<Option<kle_serial::Keyb
title: "Success".to_owned(), title: "Success".to_owned(),
message: view! { cx, message: view! { cx,
"Loaded KLE layout "<b>{&keyboard.metadata.name}</b>" successfully!" "Loaded KLE layout "<b>{&keyboard.metadata.name}</b>" successfully!"
}.into_view(cx), }
.into_view(cx),
colorway: Colorway::Ok, colorway: Colorway::Ok,
})); }));
set_keyboard(Some(keyboard)); set_keyboard(Some(keyboard));
}, }
Err(err) => set_result_message(Some(ResultMessageData { Err(err) => set_result_message(Some(ResultMessageData {
message: view! { cx, message: view! { cx,
{ {
err.to_string() err.to_string()
} }
}.into_view(cx), }
.into_view(cx),
title: <ReadKleError as Into<&str>>::into(err).to_string(), title: <ReadKleError as Into<&str>>::into(err).to_string(),
colorway: Colorway::Bad, colorway: Colorway::Bad,
})) })),
} }
}); });
}; };

Loading…
Cancel
Save