diff --git a/src/lib.rs b/src/lib.rs index 1366427..f2e36ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ mod router; mod sn; use cfg_if::cfg_if; -use utils::{Error, MyResult}; +use utils::*; use wasm_bindgen::prelude::*; use web_sys::*; @@ -43,12 +43,10 @@ async fn default_route(_req: Request, _url: Url) -> MyResult { } async fn hello_world(_req: Request, _url: Url) -> MyResult { - internal_err!( - Response::new_with_opt_str_and_init( - Some("Hello, world from Rust"), - ResponseInit::new().status(200) - ) - ) + Response::new_with_opt_str_and_init( + Some("Hello, world from Rust"), + ResponseInit::new().status(200) + ).internal_err() } #[wasm_bindgen] diff --git a/src/sn.rs b/src/sn.rs index c82f609..1024c1e 100644 --- a/src/sn.rs +++ b/src/sn.rs @@ -1,7 +1,7 @@ // Interface for Standard Notes (Actions) use crate::CONFIG; use crate::router::Router; -use crate::utils::{Error, MyResult}; +use crate::utils::*; use serde::{Serialize, Serializer}; use std::vec::Vec; use web_sys::*; @@ -47,21 +47,17 @@ async fn get_actions(_req: Request, url: Url) -> MyResult { actions }; - internal_err!( - Response::new_with_opt_str_and_init( - Some(&internal_err!( - serde_json::to_string(&info) - )?), - ResponseInit::new() - .status(200) - .headers({ - let headers = Headers::new().unwrap(); - headers.set("Content-Type", "application/json").unwrap(); - cors!(headers); - headers - }.as_ref()) - ) - ) + Response::new_with_opt_str_and_init( + Some(&serde_json::to_string(&info).internal_err()?), + ResponseInit::new() + .status(200) + .headers({ + let headers = Headers::new().unwrap(); + headers.set("Content-Type", "application/json").unwrap(); + cors!(headers); + headers + }.as_ref()) + ).internal_err() } pub enum Verb { diff --git a/src/utils.rs b/src/utils.rs index 7009ddf..cac8e10 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -25,12 +25,15 @@ macro_rules! cors { }; } -// Ignore any error and return InternalError for them all -// Used in place of ugly `.unwrap()`. -#[macro_export] -macro_rules! internal_err { - ($result:expr) => { - $result.map_err(|_| crate::utils::Error::InternalError()) +pub trait ResultExt { + // Ignore any error and return InternalError for them all + // Used in place of ugly `.unwrap()`. + fn internal_err(self) -> Result; +} + +impl ResultExt for Result { + fn internal_err(self) -> Result { + self.map_err(|_| crate::utils::Error::InternalError()) } }