diff --git a/Cargo.lock b/Cargo.lock index a3fae00..2f96ef4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -321,7 +321,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.48", ] [[package]] @@ -455,7 +455,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd4056f63fce3b82d852c3da92b08ea59959890813a7f4ce9c0ff85b10cf301b" dependencies = [ "quote", - "syn 2.0.15", + "syn 2.0.48", ] [[package]] @@ -482,7 +482,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.15", + "syn 2.0.48", ] [[package]] @@ -499,7 +499,7 @@ checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.48", ] [[package]] @@ -1410,9 +1410,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] @@ -1428,9 +1428,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.26" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1657,7 +1657,7 @@ checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.48", ] [[package]] @@ -1869,6 +1869,7 @@ dependencies = [ "swc_core", "swc_xml", "testing", + "thiserror", ] [[package]] @@ -2724,9 +2725,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.15" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -2801,22 +2802,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.40" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.40" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.48", ] [[package]] diff --git a/crates/binding/src/lib.rs b/crates/binding/src/lib.rs index c2a6e3b..1bb384e 100644 --- a/crates/binding/src/lib.rs +++ b/crates/binding/src/lib.rs @@ -21,7 +21,7 @@ pub async fn transform_node( match transform(code, config, state) { Ok(result) => napi::bindgen_prelude::Result::Ok(result), Err(reason) => { - napi::bindgen_prelude::Result::Err(napi::bindgen_prelude::Error::from_reason(reason)) + napi::bindgen_prelude::Result::Err(napi::bindgen_prelude::Error::from_reason(reason.to_string())) } } } diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index ca4f734..9594612 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -30,6 +30,7 @@ swc_core = { version = "0.74.2", features = [ "ecma_codegen", "__parser", ] } +thiserror = "1.0.56" linked-hash-map = { version = "0.5.6", features = ["serde_impl"] } linked_hash_set = "0.1.4" diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs new file mode 100644 index 0000000..fe7af2e --- /dev/null +++ b/crates/core/src/error.rs @@ -0,0 +1,11 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum SvgrError { + #[error("failed to parse SVG: {0}")] + Parse(String), + #[error("this is invalid SVG")] + InvalidSvg, + #[error("invalid configuration option: {0}")] + Configuration(String), +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 0214f9d..2212766 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -17,6 +17,7 @@ use swc_core::{ use swc_xml::parser::parse_file_as_document; mod add_jsx_attribute; +mod error; mod core; mod hast_to_swc_ast; mod remove_jsx_attribute; @@ -28,6 +29,7 @@ mod transform_svg_component; pub use self::core::config::{Config, ExpandProps, ExportType, Icon, JSXRuntime, JSXRuntimeImport}; pub use self::core::state::{Caller, Config as State}; +pub use error::SvgrError; /// Transform SVG into React components. /// @@ -50,18 +52,19 @@ pub use self::core::state::{Caller, Config as State}; /// Default::default(), /// ); /// ``` -pub fn transform(code: String, config: Config, state: State) -> Result { +pub fn transform(code: String, config: Config, state: State) -> Result { let state = core::state::expand_state(&state); let cm = Arc::::default(); let fm = cm.new_source_file(FileName::Anon, code); let mut errors = vec![]; - let document = parse_file_as_document(fm.borrow(), Default::default(), &mut errors).unwrap(); + let document = parse_file_as_document(fm.borrow(), Default::default(), &mut errors) + .map_err(|e| SvgrError::Parse(e.message().to_string()))?; let jsx_element = hast_to_swc_ast::to_swc_ast(document); if jsx_element.is_none() { - return Err("This is invalid SVG".to_string()); + return Err(SvgrError::InvalidSvg); } let jsx_element = jsx_element.unwrap(); diff --git a/crates/core/src/transform_svg_component/mod.rs b/crates/core/src/transform_svg_component/mod.rs index ee52185..349daa4 100644 --- a/crates/core/src/transform_svg_component/mod.rs +++ b/crates/core/src/transform_svg_component/mod.rs @@ -1,6 +1,6 @@ use swc_core::{common::DUMMY_SP, ecma::ast::*}; -use crate::core; +use crate::{core, SvgrError}; mod variables; @@ -71,7 +71,7 @@ pub fn transform( jsx_element: JSXElement, config: &core::config::Config, state: &core::state::InternalConfig, -) -> Result { +) -> Result { let variables_options = get_variables_options(config); let variables = variables::get_variables(variables_options, state, jsx_element)?; diff --git a/crates/core/src/transform_svg_component/variables.rs b/crates/core/src/transform_svg_component/variables.rs index 4b30b30..3b64fd5 100644 --- a/crates/core/src/transform_svg_component/variables.rs +++ b/crates/core/src/transform_svg_component/variables.rs @@ -6,6 +6,8 @@ use swc_core::{ ecma::{ast::*, parser}, }; +use crate::SvgrError; + use super::core; pub struct TemplateVariables { @@ -58,7 +60,7 @@ pub fn get_variables( opts: Options, state: &core::state::InternalConfig, jsx: JSXElement, -) -> Result { +) -> Result { let mut interfaces = vec![]; let mut props = vec![]; let mut imports = vec![]; @@ -313,7 +315,7 @@ pub fn get_variables( } } } else { - return Err(r#""namedExport" not specified"#.to_string()); + return Err(SvgrError::Configuration(r#""namedExport" not specified"#.to_string())); } } @@ -336,7 +338,7 @@ pub fn get_variables( }) } -fn get_jsx_runtime_import(cfg: &core::config::JSXRuntimeImport) -> Result { +fn get_jsx_runtime_import(cfg: &core::config::JSXRuntimeImport) -> Result { let specifiers = get_jsx_runtime_import_specifiers(cfg)?; Ok(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { @@ -354,7 +356,7 @@ fn get_jsx_runtime_import(cfg: &core::config::JSXRuntimeImport) -> Result Result, String> { +) -> Result, SvgrError> { if let Some(namespace) = cfg.namespace.clone() { let specifier = ImportSpecifier::Namespace(ImportStarAsSpecifier { span: DUMMY_SP, @@ -397,8 +399,8 @@ fn get_jsx_runtime_import_specifiers( } Err( - r#"Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option"# - .to_string(), + SvgrError::Configuration(r#"Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option"# + .to_string()) ) }