From 4ac7d1fc41080ce21ab75ab9454a077f29c4c8bf Mon Sep 17 00:00:00 2001 From: david barinas Date: Tue, 30 Apr 2024 12:54:55 -0500 Subject: [PATCH] refactor: Update attribute macros in Rust code to conditionally bind to features --- pjs-rs/Cargo.toml | 6 ++++++ pjs-rs/src/lib.rs | 29 +++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/pjs-rs/Cargo.toml b/pjs-rs/Cargo.toml index bed4478..6bbb4ae 100644 --- a/pjs-rs/Cargo.toml +++ b/pjs-rs/Cargo.toml @@ -13,5 +13,11 @@ features = [ "Window", ] +[features] +default = ["js"] +js = [] + [lib] crate-type = ["cdylib"] + + diff --git a/pjs-rs/src/lib.rs b/pjs-rs/src/lib.rs index d82596e..19a4006 100644 --- a/pjs-rs/src/lib.rs +++ b/pjs-rs/src/lib.rs @@ -19,14 +19,14 @@ macro_rules! get { const NULL: JsValue = JsValue::null(); -#[wasm_bindgen] +#[cfg_attr(feature = "js", wasm_bindgen)] pub struct PjsExtension { pjs: JsValue, accounts: Vec, selected: Option, } -#[wasm_bindgen] +#[cfg_attr(feature = "js", wasm_bindgen)] impl PjsExtension { pub async fn connect(app_name: &str) -> Result { let Some(web3) = web_sys::window().expect("browser").get("injectedWeb3") else { @@ -49,7 +49,7 @@ impl PjsExtension { }) } - #[wasm_bindgen(js_name = selectAccount)] + #[cfg_attr(feature = "js", wasm_bindgen(js_name = selectAccount))] pub fn select_account(&mut self, idx: u8) { self.selected = self .accounts @@ -58,8 +58,7 @@ impl PjsExtension { .map(|i| idx.min(i.min(u8::MAX as usize) as u8)); } - /// - #[wasm_bindgen(js_name = sign)] + #[cfg_attr(feature = "js", wasm_bindgen(js_name = sign))] pub async fn js_sign(&self, payload: &str, cb: &Function) -> Result { let sign: Function = get!(^ &self.pjs, "signer", "signRaw"); let account = self @@ -83,8 +82,7 @@ impl PjsExtension { Ok(get!(&res, "signature")) } - /// - #[wasm_bindgen(js_name = fetchAccounts)] + #[cfg_attr(feature = "js", wasm_bindgen(js_name = fetchAccounts))] pub async fn fetch_accounts(&mut self) -> Result<(), Error> { let accounts: Function = get!(^ &self.pjs, "accounts", "get"); let p = accounts.call0(&NULL).unwrap().unchecked_into::(); @@ -106,12 +104,12 @@ impl PjsExtension { Ok(()) } - #[wasm_bindgen(getter)] + #[cfg_attr(feature = "js", wasm_bindgen(getter))] pub fn accounts(&self) -> Vec { self.accounts.clone() } - #[wasm_bindgen(getter, js_name = selectedAccount)] + #[cfg_attr(feature = "js", wasm_bindgen(js_name = selectedAccount))] pub fn get_selected(&self) -> Option { self.selected .and_then(|a| self.accounts.get(a as usize)) @@ -169,9 +167,9 @@ pub struct Account { net: Network, } -#[wasm_bindgen] +#[cfg_attr(feature = "js", wasm_bindgen)] impl Account { - #[wasm_bindgen(constructor)] + #[cfg_attr(feature = "js", wasm_bindgen(constructor))] pub fn new(name: &str, address: &str, net: Network) -> Self { Account { name: name.to_string(), @@ -179,15 +177,18 @@ impl Account { net, } } - #[wasm_bindgen(getter)] + + #[cfg_attr(feature = "js", wasm_bindgen(getter))] pub fn name(&self) -> String { self.name.clone() } - #[wasm_bindgen(getter)] + + #[cfg_attr(feature = "js", wasm_bindgen(getter))] pub fn address(&self) -> String { self.address.clone() } - #[wasm_bindgen(getter)] + + #[cfg_attr(feature = "js", wasm_bindgen(getter))] pub fn network(&self) -> Network { self.net }