Skip to content

Commit

Permalink
refactor: Update attribute macros in Rust code to conditionally bind …
Browse files Browse the repository at this point in the history
…to features
  • Loading branch information
S0c5 committed Apr 30, 2024
1 parent 795b964 commit 4ac7d1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
6 changes: 6 additions & 0 deletions pjs-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ features = [
"Window",
]

[features]
default = ["js"]
js = []

[lib]
crate-type = ["cdylib"]


29 changes: 15 additions & 14 deletions pjs-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Account>,
selected: Option<u8>,
}

#[wasm_bindgen]
#[cfg_attr(feature = "js", wasm_bindgen)]
impl PjsExtension {
pub async fn connect(app_name: &str) -> Result<PjsExtension, Error> {
let Some(web3) = web_sys::window().expect("browser").get("injectedWeb3") else {
Expand All @@ -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
Expand All @@ -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<JsValue, Error> {
let sign: Function = get!(^ &self.pjs, "signer", "signRaw");
let account = self
Expand All @@ -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::<Promise>();
Expand All @@ -106,12 +104,12 @@ impl PjsExtension {
Ok(())
}

#[wasm_bindgen(getter)]
#[cfg_attr(feature = "js", wasm_bindgen(getter))]
pub fn accounts(&self) -> Vec<Account> {
self.accounts.clone()
}

#[wasm_bindgen(getter, js_name = selectedAccount)]
#[cfg_attr(feature = "js", wasm_bindgen(js_name = selectedAccount))]
pub fn get_selected(&self) -> Option<Account> {
self.selected
.and_then(|a| self.accounts.get(a as usize))
Expand Down Expand Up @@ -169,25 +167,28 @@ 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(),
address: address.to_string(),
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
}
Expand Down

0 comments on commit 4ac7d1f

Please sign in to comment.