main
Elnu 1 year ago
parent f5f353803a
commit 33447c13d0

@ -1,8 +1,10 @@
use leptos::{*, html::Input}; use leptos::{html::Input, *};
use web_sys::File; use web_sys::File;
use crate::{components::ResultMessageData, utils::read_file, error::ReadKleError, models::Colorway};
use super::ResultMessage; use super::ResultMessage;
use crate::{
components::ResultMessageData, error::ReadKleError, models::Colorway, utils::read_file,
};
#[component] #[component]
pub fn KeyboardFromFile( pub fn KeyboardFromFile(
@ -61,4 +63,4 @@ async fn read_kle_from_file(
file: ReadSignal<Option<File>>, file: ReadSignal<Option<File>>,
) -> Result<kle_serial::Keyboard, ReadKleError> { ) -> Result<kle_serial::Keyboard, ReadKleError> {
Ok(serde_json::from_str(&read_file(file).await?)?) Ok(serde_json::from_str(&read_file(file).await?)?)
} }

@ -1,8 +1,8 @@
use leptos::{*, html::Input}; use leptos::{html::Input, *};
use web_sys::SubmitEvent; use web_sys::SubmitEvent;
use crate::{error::FetchKleError, utils::fetch_file, models::Colorway};
use super::{ResultMessage, ResultMessageData}; use super::{ResultMessage, ResultMessageData};
use crate::{error::FetchKleError, models::Colorway, utils::fetch_file};
async fn fetch_layout(url: &str) -> Result<kle_serial::Keyboard, FetchKleError> { async fn fetch_layout(url: &str) -> Result<kle_serial::Keyboard, FetchKleError> {
let layout_string = fetch_file(url).await?; let layout_string = fetch_file(url).await?;
@ -31,7 +31,7 @@ pub fn KeyboardFromWeb(
colorway: Colorway::Ok, colorway: Colorway::Ok,
})); }));
set_keyboard(Some(keyboard)); set_keyboard(Some(keyboard));
}, }
Err(err) => { Err(err) => {
set_result_message(Some(ResultMessageData { set_result_message(Some(ResultMessageData {
message: view! { cx, message: view! { cx,
@ -56,4 +56,4 @@ pub fn KeyboardFromWeb(
<input type="submit" value="Load" /> <input type="submit" value="Load" />
</form> </form>
} }
} }

@ -5,4 +5,4 @@ mod keyboardfromfile;
pub use keyboardfromfile::KeyboardFromFile; pub use keyboardfromfile::KeyboardFromFile;
mod keyboardfromweb; mod keyboardfromweb;
pub use keyboardfromweb::KeyboardFromWeb; pub use keyboardfromweb::KeyboardFromWeb;

@ -1,5 +1,5 @@
use leptos::*;
use class_list::class_list; use class_list::class_list;
use leptos::*;
use crate::models::Colorway; use crate::models::Colorway;
@ -17,15 +17,17 @@ pub fn ResultMessage(cx: Scope, message: ReadSignal<Option<ResultMessageData>>)
message.track(); message.track();
set_open(true); set_open(true);
}); });
move || view! { cx, move || {
<Show when=open fallback=|_| ()> view! { cx,
{move || message().map(|ResultMessageData { title, message, colorway }| view! { cx, <Show when=open fallback=|_| ()>
<div class=class_list!["box", colorway] style="position: relative"> {move || message().map(|ResultMessageData { title, message, colorway }| view! { cx,
<strong class="block titlebar">{title}</strong> <div class=class_list!["box", colorway] style="position: relative">
<button class="iconbutton" on:click=move |_| set_open(false) style="position: absolute; bottom: 0.5em; right: 0.5em">"×"</button> <strong class="block titlebar">{title}</strong>
<p>{message}</p> <button class="iconbutton" on:click=move |_| set_open(false) style="position: absolute; bottom: 0.5em; right: 0.5em">"×"</button>
</div> <p>{message}</p>
})} </div>
</Show> })}
</Show>
}
} }
} }

@ -16,6 +16,7 @@ impl ToString for FetchFileError {
match self { match self {
Request(error) => error.as_string(), Request(error) => error.as_string(),
_ => None, _ => None,
}.unwrap_or_else(|| "".to_string()) }
.unwrap_or_else(|| "".to_string())
} }
} }

@ -5,7 +5,9 @@ use super::FetchFileError;
#[derive(From, IntoStaticStr)] #[derive(From, IntoStaticStr)]
pub enum FetchKleError { pub enum FetchKleError {
#[strum(serialize = "Invalid source. Must be KLE layout, gist URL, gist ID, or direct URL to JSON file")] #[strum(
serialize = "Invalid source. Must be KLE layout, gist URL, gist ID, or direct URL to JSON file"
)]
InvalidSource, InvalidSource,
#[strum(serialize = "Failed to fetch file")] #[strum(serialize = "Failed to fetch file")]
FetchFile(FetchFileError), FetchFile(FetchFileError),
@ -25,11 +27,12 @@ impl ToString for FetchKleError {
full_error.push_str(&error_info); full_error.push_str(&error_info);
} }
full_error full_error
}, }
FetchFileError::ReadToString => error.to_string(), FetchFileError::ReadToString => error.to_string(),
}), }),
Serde(error) => Some(error.to_string()), Serde(error) => Some(error.to_string()),
_ => None, _ => None,
}.unwrap_or_else(|| "".to_string()) }
.unwrap_or_else(|| "".to_string())
} }
} }

@ -8,4 +8,4 @@ mod fetchkle;
pub use fetchkle::FetchKleError; pub use fetchkle::FetchKleError;
mod readkle; mod readkle;
pub use readkle::ReadKleError; pub use readkle::ReadKleError;

@ -17,7 +17,8 @@ impl ToString for ReadFileError {
use ReadFileError::*; use ReadFileError::*;
match self { match self {
TextAwait(error) => error.as_string(), TextAwait(error) => error.as_string(),
_ => None _ => None,
}.unwrap_or_else(|| "".to_string()) }
.unwrap_or_else(|| "".to_string())
} }
} }

@ -18,4 +18,4 @@ impl ToString for ReadKleError {
Serde(error) => error.to_string(), Serde(error) => error.to_string(),
} }
} }
} }

@ -1,13 +1,13 @@
#![feature(async_closure)] #![feature(async_closure)]
#[allow(dead_code)]
mod render;
mod error;
mod components; mod components;
mod utils; mod error;
mod models; mod models;
#[allow(dead_code)] #[allow(dead_code)]
mod render;
#[allow(dead_code)]
pub mod svg; pub mod svg;
mod utils;
use leptos::*; use leptos::*;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;

@ -22,4 +22,4 @@ impl ClassList for Colorway {
let class: &str = self.into(); let class: &str = self.into();
class.to_string() class.to_string()
} }
} }

@ -1,2 +1,2 @@
mod colorway; mod colorway;
pub use colorway::Colorway; pub use colorway::Colorway;

@ -1,24 +1,19 @@
use leptos::*; use leptos::*;
use wasm_bindgen_futures::JsFuture; use wasm_bindgen_futures::JsFuture;
use web_sys::{File, Window, Request}; use web_sys::{File, Request, Window};
use crate::error::{ReadFileError, FetchFileError}; use crate::error::{FetchFileError, ReadFileError};
pub fn window() -> Window { pub fn window() -> Window {
web_sys::window().unwrap() web_sys::window().unwrap()
} }
pub async fn read_file( pub async fn read_file(file: ReadSignal<Option<File>>) -> Result<String, ReadFileError> {
file: ReadSignal<Option<File>>,
) -> Result<String, ReadFileError> {
let file = match file() { let file = match file() {
Some(file) => file, Some(file) => file,
None => return Err(ReadFileError::NoFile), None => return Err(ReadFileError::NoFile),
}; };
match JsFuture::from(file.text()) match JsFuture::from(file.text()).await?.as_string() {
.await?
.as_string()
{
Some(contents) => Ok(contents), Some(contents) => Ok(contents),
None => Err(ReadFileError::ParseToString), None => Err(ReadFileError::ParseToString),
} }
@ -31,4 +26,4 @@ pub async fn fetch_file(url: &str) -> Result<String, FetchFileError> {
Some(string) => Ok(string), Some(string) => Ok(string),
None => Err(FetchFileError::ReadToString), None => Err(FetchFileError::ReadToString),
} }
} }

Loading…
Cancel
Save