Skip to content

Commit

Permalink
feat(wasm+fetcher): actually derive Error in a better way to handle…
Browse files Browse the repository at this point in the history
… `FetcherError`
  • Loading branch information
Vexcited committed Jan 3, 2025
1 parent c3e7b3c commit 5504a1b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 37 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fetcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde-wasm-bindgen = "0.6"
serde_bytes = "0.11"
wasm-bindgen = "0.2"
js-sys = "0.3"
thiserror = "2.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = "0.12"
32 changes: 18 additions & 14 deletions fetcher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
#[cfg(target_arch = "wasm32")]
use serde::{Deserialize, Serialize, Serializer};

pub use http::{HeaderMap, HeaderName, Method};
pub use url::Url;

#[cfg(not(target_arch = "wasm32"))]
pub use reqwest::Error as FetcherError;

#[cfg(target_arch = "wasm32")]
pub struct FetcherError(String);
use serde::{Deserialize, Serialize, Serializer};

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = liUtilsFetcher)]
type FetcherError;
#[wasm_bindgen(js_namespace = liUtilsFetcher, js_name = FetcherError)]
type JsFetcherError;

#[wasm_bindgen(constructor, js_namespace = liUtilsFetcher)]
fn new(message: &str) -> FetcherError;
#[wasm_bindgen(constructor, js_namespace = liUtilsFetcher, js_name = FetcherError)]
fn new(message: &str) -> JsFetcherError;
}

#[cfg(target_arch = "wasm32")]
impl From<Error> for wasm_bindgen::JsValue {
fn from(error: Error) -> Self {
let error_msg = error.to_string();
#[derive(thiserror::Error, Debug)]
pub struct FetcherError(String);

#[cfg(target_arch = "wasm32")]
impl std::fmt::Display for FetcherError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

match error {
Error::FetcherError() => FetcherError::new(&error_msg).into(),
}
#[cfg(target_arch = "wasm32")]
impl From<FetcherError> for wasm_bindgen::JsValue {
fn from(error: FetcherError) -> Self {
JsFetcherError::new(&error.0).into()
}
}

Expand Down
1 change: 1 addition & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ convert_case = "0.6.0"
wasm-bindgen = "0.2"
thiserror = "2"
js-sys = "0.3"
fetcher = { package = "literateink-fetcher", version = "0.1", path = "../fetcher" }

[lib]
proc-macro = true
7 changes: 5 additions & 2 deletions wasm/examples/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use literateink_wasm as wasm;
use fetcher::FetcherError;

#[derive(thiserror::Error, wasm::Error, Debug)]
#[derive(thiserror::Error, Debug)]
#[cfg_attr(target_arch = "wasm32", derive(literateink_wasm::Error))]
pub enum Error {
#[error("no redirection was made, make sure the instance URL is correct")]
InvalidRedirection(),
#[error("the response was not successful, status code: {0}")]
UnsuccessfulResponse(u16),
#[error(transparent)]
FetcherError(#[from] FetcherError),
}

fn main() {
Expand Down
52 changes: 31 additions & 21 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,33 +184,43 @@ pub fn derive_wasm_error(input: TokenStream) -> TokenStream {
_ => panic!("#[derive(Error)] is only valid for enums"),
};

let extern_block = data_enum.variants.iter().map(|variant| {
let variant_ident = &variant.ident;
quote! {
#[wasm_bindgen::prelude::wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = exports)]
type #variant_ident;

#[wasm_bindgen(constructor, js_namespace = exports)]
fn new(message: &str) -> #variant_ident;
let extern_block = data_enum
.variants
.iter()
.filter(|variant| variant.ident != "FetcherError")
.map(|variant| {
let variant_ident = &variant.ident;
quote! {
#[wasm_bindgen::prelude::wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = exports)]
type #variant_ident;

#[wasm_bindgen(constructor, js_namespace = exports)]
fn new(message: &str) -> #variant_ident;
}
}
}
});
});

let match_arms = data_enum.variants.iter().map(|variant| {
let variant_ident = &variant.ident;

// adjust pattern based on the variant’s fields
let pattern = match &variant.fields {
syn::Fields::Named(_) => quote! { #name::#variant_ident { .. } },
syn::Fields::Unnamed(_) => quote! { #name::#variant_ident(..) },
syn::Fields::Unit => quote! { #name::#variant_ident },
};
if variant.ident == "FetcherError" {
quote! {
#name::FetcherError(error) => error.into()
}
} else {
// adjust pattern based on the variant’s fields
let pattern = match &variant.fields {
syn::Fields::Named(_) => quote! { #name::#variant_ident { .. } },
syn::Fields::Unnamed(_) => quote! { #name::#variant_ident(..) },
syn::Fields::Unit => quote! { #name::#variant_ident },
};

quote! {
#pattern => {
#variant_ident::new(&error_msg).into()
quote! {
#pattern => {
#variant_ident::new(&error_msg).into()
}
}
}
});
Expand Down

0 comments on commit 5504a1b

Please sign in to comment.