diff --git a/Cargo.lock b/Cargo.lock index 7cd75c2..e95d879 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -410,12 +410,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "convert_case" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" - [[package]] name = "convert_case" version = "0.6.0" @@ -464,19 +458,6 @@ dependencies = [ "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]] name = "digest" version = "0.10.7" @@ -504,7 +485,7 @@ name = "dyesub" version = "0.1.0" dependencies = [ "askama", - "derive_more", + "err-derive", "kle-serial", "lazy_static", "serde_json", @@ -526,7 +507,6 @@ name = "dyesub-tool" version = "0.1.0" dependencies = [ "class_list", - "derive_more", "kle-serial", "lazy_static", "leptos", @@ -534,6 +514,7 @@ dependencies = [ "strum", "strum_macros", "svg-units", + "thiserror", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", @@ -1111,7 +1092,7 @@ checksum = "0bdd7a21d20ca21bb12d67d050d4b0ad9973b156bce98f499f8b1789f11959dd" dependencies = [ "attribute-derive", "cfg-if", - "convert_case 0.6.0", + "convert_case", "html-escape", "itertools", "leptos_hot_reload", @@ -1712,15 +1693,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "rustix" version = "0.38.8" @@ -1767,12 +1739,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c309e515543e67811222dbc9e3dd7e1056279b782e1dacffe4242b718734fb6" -[[package]] -name = "semver" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" - [[package]] name = "serde" version = "1.0.183" @@ -1978,11 +1944,11 @@ dependencies = [ name = "svg-units" version = "0.1.0" dependencies = [ - "err-derive", "lazy_static", "regex", "strum", "strum_macros", + "thiserror", ] [[package]] diff --git a/dyesub-tool/Cargo.toml b/dyesub-tool/Cargo.toml index 0002f49..a913e7d 100644 --- a/dyesub-tool/Cargo.toml +++ b/dyesub-tool/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" [dependencies] svg-units = { path = "../svg-units" } class_list = "0.1.1" -derive_more = "0.99.17" kle-serial = "0.2.2" lazy_static = "1.4.0" leptos = { version = "0.4.8", features = ["csr", "nightly"] } @@ -15,6 +14,7 @@ strum = "0.25.0" strum_macros = "0.25.2" wasm-bindgen = "0.2.87" wasm-bindgen-futures = "0.4.37" +thiserror = "1.0.47" [dependencies.web-sys] version = "0.3.64" diff --git a/dyesub-tool/src/components/keyboardfromfile.rs b/dyesub-tool/src/components/keyboardfromfile.rs index 9a5ff38..ddb507c 100644 --- a/dyesub-tool/src/components/keyboardfromfile.rs +++ b/dyesub-tool/src/components/keyboardfromfile.rs @@ -1,3 +1,5 @@ +use std::error::Error; + use leptos::{html::Input, *}; use web_sys::File; @@ -20,24 +22,15 @@ pub fn KeyboardFromFile( Ok(keyboard) => { set_result_message(Some(ResultMessageData { title: "Success".to_owned(), - message: view! { cx, - "Loaded KLE layout "{&keyboard.metadata.name}" successfully!" - } - .into_view(cx), + message: format!("Loaded KLE layout {} successfully!", &keyboard.metadata.name), colorway: Colorway::Ok, })); set_keyboard(Some(keyboard)); } Err(err) => { - set_result_message(Some(ResultMessageData { - message: view! { cx, - { - err.to_string() - } - } - .into_view(cx), - title: >::into(err).to_string(), - colorway: Colorway::Bad, + set_result_message(Some({ + let error: Box = Box::new(err); + error.into() })); file_input().unwrap().set_value(""); } diff --git a/dyesub-tool/src/components/keyboardfromweb.rs b/dyesub-tool/src/components/keyboardfromweb.rs index 0be3101..620a020 100644 --- a/dyesub-tool/src/components/keyboardfromweb.rs +++ b/dyesub-tool/src/components/keyboardfromweb.rs @@ -1,3 +1,5 @@ +use std::error::Error; + use leptos::{html::Input, *}; use web_sys::SubmitEvent; @@ -24,24 +26,15 @@ pub fn KeyboardFromWeb( Ok(keyboard) => { set_result_message(Some(ResultMessageData { title: "Success".to_owned(), - message: view! { cx, - "Loaded KLE layout "{&keyboard.metadata.name}" successfully!" - } - .into_view(cx), + message: format!("Loaded KLE layout {} successfully!", &keyboard.metadata.name), colorway: Colorway::Ok, })); set_keyboard(Some(keyboard)); } Err(err) => { - set_result_message(Some(ResultMessageData { - message: view! { cx, - { - err.to_string() - } - } - .into_view(cx), - title: >::into(err).to_string(), - colorway: Colorway::Bad, + set_result_message(Some({ + let error: Box = Box::new(err); + error.into() })); input_element().unwrap().set_value(""); } diff --git a/dyesub-tool/src/components/resultmessage.rs b/dyesub-tool/src/components/resultmessage.rs index 6e13e02..85460c3 100644 --- a/dyesub-tool/src/components/resultmessage.rs +++ b/dyesub-tool/src/components/resultmessage.rs @@ -1,15 +1,29 @@ use class_list::class_list; use leptos::*; +use std::error::Error; use crate::models::Colorway; #[derive(Clone)] pub struct ResultMessageData { pub title: String, - pub message: View, + pub message: String, pub colorway: Colorway, } +impl From> for ResultMessageData { + fn from(value: Box) -> Self { + Self { + title: value.to_string(), + message: value + .source() + .map(|source| source.to_string()) + .unwrap_or_else(|| "".to_string()), + colorway: Colorway::Bad, + } + } +} + #[component] pub fn ResultMessage(cx: Scope, message: ReadSignal>) -> impl IntoView { let (open, set_open) = create_signal(cx, true); @@ -24,7 +38,7 @@ pub fn ResultMessage(cx: Scope, message: ReadSignal>)
{title} -

{message}

+

})} diff --git a/dyesub-tool/src/error/fetchfile.rs b/dyesub-tool/src/error/fetchfile.rs index ed7bfc7..f7f9552 100644 --- a/dyesub-tool/src/error/fetchfile.rs +++ b/dyesub-tool/src/error/fetchfile.rs @@ -1,22 +1,18 @@ -use derive_more::From; -use strum_macros::IntoStaticStr; +use thiserror::Error; use wasm_bindgen::JsValue; -#[derive(From, IntoStaticStr)] +use super::JsError; + +#[derive(Error, Debug)] pub enum FetchFileError { - #[strum(serialize = "Failed to fetch file")] - Request(JsValue), - #[strum(serialize = "Failed to read fetched file to string")] + #[error("Failed to fetch file")] + Request(#[from] JsError), + #[error("Failed to read fetched file to string")] ReadToString, } -impl ToString for FetchFileError { - fn to_string(&self) -> String { - use FetchFileError::*; - match self { - Request(error) => error.as_string(), - _ => None, - } - .unwrap_or_else(|| "".to_string()) +impl From for FetchFileError { + fn from(value: JsValue) -> Self { + Self::Request(value.into()) } -} +} \ No newline at end of file diff --git a/dyesub-tool/src/error/fetchkle.rs b/dyesub-tool/src/error/fetchkle.rs index 2844f22..8c3c28d 100644 --- a/dyesub-tool/src/error/fetchkle.rs +++ b/dyesub-tool/src/error/fetchkle.rs @@ -1,38 +1,13 @@ -use derive_more::From; -use strum_macros::IntoStaticStr; +use thiserror::Error; use super::FetchFileError; -#[derive(From, IntoStaticStr)] +#[derive(Error, Debug)] pub enum FetchKleError { - #[strum( - serialize = "Invalid source. Must be KLE layout, gist URL, gist ID, or direct URL to JSON file" - )] + #[error("Invalid source. Must be KLE layout, gist URL, gist ID, or direct URL to JSON file")] InvalidSource, - #[strum(serialize = "Failed to fetch file")] - FetchFile(FetchFileError), - #[strum(serialize = "Failed to parse KLE JSON")] - Serde(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()) - } -} + #[error("Failed to fetch file")] + FetchFile(#[from] FetchFileError), + #[error("Failed to parse KLE JSON")] + Serde(#[from] serde_json::Error), +} \ No newline at end of file diff --git a/dyesub-tool/src/error/jserror.rs b/dyesub-tool/src/error/jserror.rs new file mode 100644 index 0000000..f634a1b --- /dev/null +++ b/dyesub-tool/src/error/jserror.rs @@ -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 for JsError { + fn from(value: JsValue) -> Self { + Self(value) + } +} \ No newline at end of file diff --git a/dyesub-tool/src/error/mod.rs b/dyesub-tool/src/error/mod.rs index 7e1ac15..12bfc01 100644 --- a/dyesub-tool/src/error/mod.rs +++ b/dyesub-tool/src/error/mod.rs @@ -9,3 +9,6 @@ pub use fetchkle::FetchKleError; mod readkle; pub use readkle::ReadKleError; + +mod jserror; +pub use jserror::JsError; \ No newline at end of file diff --git a/dyesub-tool/src/error/readfile.rs b/dyesub-tool/src/error/readfile.rs index 50a5955..8ab8744 100644 --- a/dyesub-tool/src/error/readfile.rs +++ b/dyesub-tool/src/error/readfile.rs @@ -1,24 +1,20 @@ -use derive_more::From; -use strum_macros::IntoStaticStr; +use thiserror::Error; use wasm_bindgen::JsValue; -#[derive(From, IntoStaticStr)] +use super::JsError; + +#[derive(Error, Debug)] pub enum ReadFileError { - #[strum(serialize = "No file chosen")] + #[error("No file chosen")] NoFile, - #[strum(serialize = "Failed to open file")] - TextAwait(JsValue), - #[strum(serialize = "Failed to parse file to string")] + #[error("Failed to open file")] + TextAwait(#[from] JsError), + #[error("Failed to parse file to string")] ParseToString, } -impl ToString for ReadFileError { - fn to_string(&self) -> String { - use ReadFileError::*; - match self { - TextAwait(error) => error.as_string(), - _ => None, - } - .unwrap_or_else(|| "".to_string()) +impl From for ReadFileError { + fn from(value: JsValue) -> Self { + Self::TextAwait(value.into()) } -} +} \ No newline at end of file diff --git a/dyesub-tool/src/error/readkle.rs b/dyesub-tool/src/error/readkle.rs index dc3f5f1..8ec4caa 100644 --- a/dyesub-tool/src/error/readkle.rs +++ b/dyesub-tool/src/error/readkle.rs @@ -1,21 +1,11 @@ -use derive_more::From; -use strum_macros::IntoStaticStr; +use thiserror::Error; use super::ReadFileError; -#[derive(From, IntoStaticStr)] +#[derive(Error, Debug)] pub enum ReadKleError { - ReadFile(ReadFileError), - #[strum(serialize = "Failed to parse KLE JSON")] - Serde(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(), - } - } -} + #[error("Failed to read file")] + ReadFile(#[from] ReadFileError), + #[error("Failed to parse KLE JSON")] + Serde(#[from] serde_json::Error), +} \ No newline at end of file diff --git a/dyesub/Cargo.toml b/dyesub/Cargo.toml index 28d731b..d589883 100644 --- a/dyesub/Cargo.toml +++ b/dyesub/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] svg-units = { path = "../svg-units" } askama = "0.12.0" -derive_more = "0.99.17" kle-serial = "0.2.2" lazy_static = "1.4.0" serde_json = "1.0.105" +err-derive = "0.3.1" diff --git a/dyesub/src/lib.rs b/dyesub/src/lib.rs index d68aef9..cd964eb 100644 --- a/dyesub/src/lib.rs +++ b/dyesub/src/lib.rs @@ -1,7 +1,7 @@ use std::path::Path; use askama::Template; -use derive_more::From; +use err_derive::Error; use lazy_static::lazy_static; use svg_units::{SVGMeasure, SVGUnit}; @@ -38,11 +38,14 @@ mod filters { } } -#[derive(From, Debug)] +#[derive(Error, Debug)] pub enum Error { - Io(std::io::Error), - Template(askama::Error), - LayoutParse(serde_json::Error), + #[error(display = "I/O error")] + Io(#[cause] std::io::Error), + #[error(display = "Templating error")] + Template(#[cause] askama::Error), + #[error(display = "Failed to parse KLE JSON")] + LayoutParse(#[cause] serde_json::Error), } pub type Result = std::result::Result; diff --git a/flake.nix b/flake.nix index 3f929a3..5aae05d 100644 --- a/flake.nix +++ b/flake.nix @@ -66,7 +66,7 @@ Updating `cargoHash`: pname = "dyesub-cli"; version = "0.1.0"; buildAndTestSubdir = "dyesub-cli"; - cargoHash = "sha256-6nbUjVjroFp2KZEk0wDisCUw2e9GtVxFgsGZm8xEe2A="; + cargoHash = "sha256-3i+msqsUkNtVAx1eOZFftYRCDg+nVFZDhbdOIC8xvxM="; meta = meta // { description = "A tool for generating dye sublimation transfer sheet SVGs for Japanese thumb shift 拇指シフト keycaps."; }; diff --git a/svg-units/Cargo.toml b/svg-units/Cargo.toml index 7891f26..1c29948 100644 --- a/svg-units/Cargo.toml +++ b/svg-units/Cargo.toml @@ -6,8 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -err-derive = "0.3.1" lazy_static = "1.4.0" regex = "1.9.3" strum = "0.25.0" strum_macros = "0.25.2" +thiserror = "1.0.47" diff --git a/svg-units/src/measure.rs b/svg-units/src/measure.rs index 902af66..6c09fc2 100644 --- a/svg-units/src/measure.rs +++ b/svg-units/src/measure.rs @@ -1,7 +1,7 @@ use super::SVGUnit; -use err_derive::Error; use lazy_static::lazy_static; use regex::Regex; +use thiserror::Error; use std::cmp::Ordering; use std::fmt; use std::num::ParseFloatError; @@ -35,13 +35,13 @@ impl SVGMeasure { } } -#[derive(Debug, Error)] +#[derive(Error, Debug)] pub enum SVGMeasureParseError { - #[error(display = "Failed to parse measure number")] - ParseMeasure(#[source] ParseFloatError), - #[error(display = "Failed to parse measure unit")] - ParseUnit(#[source] ParseError), - #[error(display = "Invalid measure format")] + #[error("Failed to parse measure number")] + ParseMeasure(#[from] ParseFloatError), + #[error("Failed to parse measure unit")] + ParseUnit(#[from] ParseError), + #[error("Invalid measure format")] Invalid, }