use thiserror

main
Elnu 1 year ago
parent 9009ae9e24
commit 2c72d5fc15

42
Cargo.lock generated

@ -410,12 +410,6 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "convert_case"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]] [[package]]
name = "convert_case" name = "convert_case"
version = "0.6.0" version = "0.6.0"
@ -464,19 +458,6 @@ dependencies = [
"syn 2.0.28", "syn 2.0.28",
] ]
[[package]]
name = "derive_more"
version = "0.99.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
dependencies = [
"convert_case 0.4.0",
"proc-macro2",
"quote",
"rustc_version",
"syn 1.0.109",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -504,7 +485,7 @@ name = "dyesub"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"askama", "askama",
"derive_more", "err-derive",
"kle-serial", "kle-serial",
"lazy_static", "lazy_static",
"serde_json", "serde_json",
@ -526,7 +507,6 @@ name = "dyesub-tool"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"class_list", "class_list",
"derive_more",
"kle-serial", "kle-serial",
"lazy_static", "lazy_static",
"leptos", "leptos",
@ -534,6 +514,7 @@ dependencies = [
"strum", "strum",
"strum_macros", "strum_macros",
"svg-units", "svg-units",
"thiserror",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys", "web-sys",
@ -1111,7 +1092,7 @@ checksum = "0bdd7a21d20ca21bb12d67d050d4b0ad9973b156bce98f499f8b1789f11959dd"
dependencies = [ dependencies = [
"attribute-derive", "attribute-derive",
"cfg-if", "cfg-if",
"convert_case 0.6.0", "convert_case",
"html-escape", "html-escape",
"itertools", "itertools",
"leptos_hot_reload", "leptos_hot_reload",
@ -1712,15 +1693,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.8" version = "0.38.8"
@ -1767,12 +1739,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c309e515543e67811222dbc9e3dd7e1056279b782e1dacffe4242b718734fb6" checksum = "4c309e515543e67811222dbc9e3dd7e1056279b782e1dacffe4242b718734fb6"
[[package]]
name = "semver"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.183" version = "1.0.183"
@ -1978,11 +1944,11 @@ dependencies = [
name = "svg-units" name = "svg-units"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"err-derive",
"lazy_static", "lazy_static",
"regex", "regex",
"strum", "strum",
"strum_macros", "strum_macros",
"thiserror",
] ]
[[package]] [[package]]

@ -6,7 +6,6 @@ edition = "2021"
[dependencies] [dependencies]
svg-units = { path = "../svg-units" } svg-units = { path = "../svg-units" }
class_list = "0.1.1" class_list = "0.1.1"
derive_more = "0.99.17"
kle-serial = "0.2.2" kle-serial = "0.2.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
leptos = { version = "0.4.8", features = ["csr", "nightly"] } leptos = { version = "0.4.8", features = ["csr", "nightly"] }
@ -15,6 +14,7 @@ strum = "0.25.0"
strum_macros = "0.25.2" strum_macros = "0.25.2"
wasm-bindgen = "0.2.87" wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4.37" wasm-bindgen-futures = "0.4.37"
thiserror = "1.0.47"
[dependencies.web-sys] [dependencies.web-sys]
version = "0.3.64" version = "0.3.64"

@ -1,3 +1,5 @@
use std::error::Error;
use leptos::{html::Input, *}; use leptos::{html::Input, *};
use web_sys::File; use web_sys::File;
@ -20,24 +22,15 @@ pub fn KeyboardFromFile(
Ok(keyboard) => { Ok(keyboard) => {
set_result_message(Some(ResultMessageData { set_result_message(Some(ResultMessageData {
title: "Success".to_owned(), title: "Success".to_owned(),
message: view! { cx, message: format!("Loaded KLE layout <b>{}</b> successfully!", &keyboard.metadata.name),
"Loaded KLE layout "<b>{&keyboard.metadata.name}</b>" successfully!"
}
.into_view(cx),
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({
message: view! { cx, let error: Box<dyn Error> = Box::new(err);
{ error.into()
err.to_string()
}
}
.into_view(cx),
title: <ReadKleError as Into<&str>>::into(err).to_string(),
colorway: Colorway::Bad,
})); }));
file_input().unwrap().set_value(""); file_input().unwrap().set_value("");
} }

@ -1,3 +1,5 @@
use std::error::Error;
use leptos::{html::Input, *}; use leptos::{html::Input, *};
use web_sys::SubmitEvent; use web_sys::SubmitEvent;
@ -24,24 +26,15 @@ pub fn KeyboardFromWeb(
Ok(keyboard) => { Ok(keyboard) => {
set_result_message(Some(ResultMessageData { set_result_message(Some(ResultMessageData {
title: "Success".to_owned(), title: "Success".to_owned(),
message: view! { cx, message: format!("Loaded KLE layout <b>{}</b> successfully!", &keyboard.metadata.name),
"Loaded KLE layout "<b>{&keyboard.metadata.name}</b>" successfully!"
}
.into_view(cx),
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({
message: view! { cx, let error: Box<dyn Error> = Box::new(err);
{ error.into()
err.to_string()
}
}
.into_view(cx),
title: <FetchKleError as Into<&str>>::into(err).to_string(),
colorway: Colorway::Bad,
})); }));
input_element().unwrap().set_value(""); input_element().unwrap().set_value("");
} }

@ -1,15 +1,29 @@
use class_list::class_list; use class_list::class_list;
use leptos::*; use leptos::*;
use std::error::Error;
use crate::models::Colorway; use crate::models::Colorway;
#[derive(Clone)] #[derive(Clone)]
pub struct ResultMessageData { pub struct ResultMessageData {
pub title: String, pub title: String,
pub message: View, pub message: String,
pub colorway: Colorway, pub colorway: Colorway,
} }
impl From<Box<dyn Error>> for ResultMessageData {
fn from(value: Box<dyn Error>) -> Self {
Self {
title: value.to_string(),
message: value
.source()
.map(|source| source.to_string())
.unwrap_or_else(|| "".to_string()),
colorway: Colorway::Bad,
}
}
}
#[component] #[component]
pub fn ResultMessage(cx: Scope, message: ReadSignal<Option<ResultMessageData>>) -> impl IntoView { pub fn ResultMessage(cx: Scope, message: ReadSignal<Option<ResultMessageData>>) -> impl IntoView {
let (open, set_open) = create_signal(cx, true); let (open, set_open) = create_signal(cx, true);
@ -24,7 +38,7 @@ pub fn ResultMessage(cx: Scope, message: ReadSignal<Option<ResultMessageData>>)
<div class=class_list!["box", colorway] style="position: relative"> <div class=class_list!["box", colorway] style="position: relative">
<strong class="block titlebar">{title}</strong> <strong class="block titlebar">{title}</strong>
<button class="iconbutton" on:click=move |_| set_open(false) style="position: absolute; bottom: 0.5em; right: 0.5em">"×"</button> <button class="iconbutton" on:click=move |_| set_open(false) style="position: absolute; bottom: 0.5em; right: 0.5em">"×"</button>
<p>{message}</p> <p inner_html=message />
</div> </div>
})} })}
</Show> </Show>

@ -1,22 +1,18 @@
use derive_more::From; use thiserror::Error;
use strum_macros::IntoStaticStr;
use wasm_bindgen::JsValue; use wasm_bindgen::JsValue;
#[derive(From, IntoStaticStr)] use super::JsError;
#[derive(Error, Debug)]
pub enum FetchFileError { pub enum FetchFileError {
#[strum(serialize = "Failed to fetch file")] #[error("Failed to fetch file")]
Request(JsValue), Request(#[from] JsError),
#[strum(serialize = "Failed to read fetched file to string")] #[error("Failed to read fetched file to string")]
ReadToString, ReadToString,
} }
impl ToString for FetchFileError { impl From<JsValue> for FetchFileError {
fn to_string(&self) -> String { fn from(value: JsValue) -> Self {
use FetchFileError::*; Self::Request(value.into())
match self {
Request(error) => error.as_string(),
_ => None,
}
.unwrap_or_else(|| "".to_string())
} }
} }

@ -1,38 +1,13 @@
use derive_more::From; use thiserror::Error;
use strum_macros::IntoStaticStr;
use super::FetchFileError; use super::FetchFileError;
#[derive(From, IntoStaticStr)] #[derive(Error, Debug)]
pub enum FetchKleError { pub enum FetchKleError {
#[strum( #[error("Invalid source. Must be KLE layout, gist URL, gist ID, or direct URL to JSON file")]
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")] #[error("Failed to fetch file")]
FetchFile(FetchFileError), FetchFile(#[from] FetchFileError),
#[strum(serialize = "Failed to parse KLE JSON")] #[error("Failed to parse KLE JSON")]
Serde(serde_json::Error), Serde(#[from] serde_json::Error),
}
impl ToString for FetchKleError {
fn to_string(&self) -> String {
use FetchKleError::*;
match self {
FetchFile(error) => Some(match error {
FetchFileError::Request(error_info) => {
let mut full_error = <&FetchFileError as Into<&str>>::into(error).to_string();
if let Some(error_info) = error_info.as_string() {
full_error.push_str(": ");
full_error.push_str(&error_info);
}
full_error
}
FetchFileError::ReadToString => error.to_string(),
}),
Serde(error) => Some(error.to_string()),
_ => None,
}
.unwrap_or_else(|| "".to_string())
}
} }

@ -0,0 +1,12 @@
use thiserror::Error;
use wasm_bindgen::JsValue;
#[derive(Error, Debug)]
#[error("Error reported from JavaScript")]
pub struct JsError(JsValue);
impl From<JsValue> for JsError {
fn from(value: JsValue) -> Self {
Self(value)
}
}

@ -9,3 +9,6 @@ pub use fetchkle::FetchKleError;
mod readkle; mod readkle;
pub use readkle::ReadKleError; pub use readkle::ReadKleError;
mod jserror;
pub use jserror::JsError;

@ -1,24 +1,20 @@
use derive_more::From; use thiserror::Error;
use strum_macros::IntoStaticStr;
use wasm_bindgen::JsValue; use wasm_bindgen::JsValue;
#[derive(From, IntoStaticStr)] use super::JsError;
#[derive(Error, Debug)]
pub enum ReadFileError { pub enum ReadFileError {
#[strum(serialize = "No file chosen")] #[error("No file chosen")]
NoFile, NoFile,
#[strum(serialize = "Failed to open file")] #[error("Failed to open file")]
TextAwait(JsValue), TextAwait(#[from] JsError),
#[strum(serialize = "Failed to parse file to string")] #[error("Failed to parse file to string")]
ParseToString, ParseToString,
} }
impl ToString for ReadFileError { impl From<JsValue> for ReadFileError {
fn to_string(&self) -> String { fn from(value: JsValue) -> Self {
use ReadFileError::*; Self::TextAwait(value.into())
match self {
TextAwait(error) => error.as_string(),
_ => None,
}
.unwrap_or_else(|| "".to_string())
} }
} }

@ -1,21 +1,11 @@
use derive_more::From; use thiserror::Error;
use strum_macros::IntoStaticStr;
use super::ReadFileError; use super::ReadFileError;
#[derive(From, IntoStaticStr)] #[derive(Error, Debug)]
pub enum ReadKleError { pub enum ReadKleError {
ReadFile(ReadFileError), #[error("Failed to read file")]
#[strum(serialize = "Failed to parse KLE JSON")] ReadFile(#[from] ReadFileError),
Serde(serde_json::Error), #[error("Failed to parse KLE JSON")]
} Serde(#[from] serde_json::Error),
impl ToString for ReadKleError {
fn to_string(&self) -> String {
use ReadKleError::*;
match self {
ReadFile(error) => error.to_string(),
Serde(error) => error.to_string(),
}
}
} }

@ -8,7 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
svg-units = { path = "../svg-units" } svg-units = { path = "../svg-units" }
askama = "0.12.0" askama = "0.12.0"
derive_more = "0.99.17"
kle-serial = "0.2.2" kle-serial = "0.2.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
serde_json = "1.0.105" serde_json = "1.0.105"
err-derive = "0.3.1"

@ -1,7 +1,7 @@
use std::path::Path; use std::path::Path;
use askama::Template; use askama::Template;
use derive_more::From; use err_derive::Error;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use svg_units::{SVGMeasure, SVGUnit}; use svg_units::{SVGMeasure, SVGUnit};
@ -38,11 +38,14 @@ mod filters {
} }
} }
#[derive(From, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
Io(std::io::Error), #[error(display = "I/O error")]
Template(askama::Error), Io(#[cause] std::io::Error),
LayoutParse(serde_json::Error), #[error(display = "Templating error")]
Template(#[cause] askama::Error),
#[error(display = "Failed to parse KLE JSON")]
LayoutParse(#[cause] serde_json::Error),
} }
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;

@ -66,7 +66,7 @@ Updating `cargoHash`:
pname = "dyesub-cli"; pname = "dyesub-cli";
version = "0.1.0"; version = "0.1.0";
buildAndTestSubdir = "dyesub-cli"; buildAndTestSubdir = "dyesub-cli";
cargoHash = "sha256-6nbUjVjroFp2KZEk0wDisCUw2e9GtVxFgsGZm8xEe2A="; cargoHash = "sha256-3i+msqsUkNtVAx1eOZFftYRCDg+nVFZDhbdOIC8xvxM=";
meta = meta // { meta = meta // {
description = "A tool for generating dye sublimation transfer sheet SVGs for Japanese thumb shift keycaps."; description = "A tool for generating dye sublimation transfer sheet SVGs for Japanese thumb shift keycaps.";
}; };

@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
err-derive = "0.3.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"
regex = "1.9.3" regex = "1.9.3"
strum = "0.25.0" strum = "0.25.0"
strum_macros = "0.25.2" strum_macros = "0.25.2"
thiserror = "1.0.47"

@ -1,7 +1,7 @@
use super::SVGUnit; use super::SVGUnit;
use err_derive::Error;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use thiserror::Error;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
use std::num::ParseFloatError; use std::num::ParseFloatError;
@ -35,13 +35,13 @@ impl SVGMeasure {
} }
} }
#[derive(Debug, Error)] #[derive(Error, Debug)]
pub enum SVGMeasureParseError { pub enum SVGMeasureParseError {
#[error(display = "Failed to parse measure number")] #[error("Failed to parse measure number")]
ParseMeasure(#[source] ParseFloatError), ParseMeasure(#[from] ParseFloatError),
#[error(display = "Failed to parse measure unit")] #[error("Failed to parse measure unit")]
ParseUnit(#[source] ParseError), ParseUnit(#[from] ParseError),
#[error(display = "Invalid measure format")] #[error("Invalid measure format")]
Invalid, Invalid,
} }

Loading…
Cancel
Save