Skip to content

Commit

Permalink
Remove generics
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 21, 2024
1 parent d88b646 commit 2dc418d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 38 deletions.
7 changes: 3 additions & 4 deletions webln-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

extern crate alloc;

use alloc::string::String;
use core::ops::Deref;

use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -107,7 +106,7 @@ impl JsWebLN {

/// Request that the user sends a payment for an invoice.
#[wasm_bindgen(js_name = sendPayment)]
pub async fn send_payment(&self, invoice: String) -> Result<JsSendPaymentResponse> {
pub async fn send_payment(&self, invoice: &str) -> Result<JsSendPaymentResponse> {
Ok(self
.inner
.send_payment(invoice)
Expand All @@ -121,7 +120,7 @@ impl JsWebLN {
/// This is useful when paying HOLD Invoices. There is no guarantee that the payment will be successfully sent to the receiver.
/// It's up to the receiver to check whether or not the invoice has been paid.
#[wasm_bindgen(js_name = sendPaymentAsync)]
pub async fn send_payment_async(&self, invoice: String) -> Result<()> {
pub async fn send_payment_async(&self, invoice: &str) -> Result<()> {
self.inner
.send_payment_async(invoice)
.await
Expand All @@ -130,7 +129,7 @@ impl JsWebLN {

/// Request that the user signs an arbitrary string message.
#[wasm_bindgen(js_name = signMessage)]
pub async fn sign_message(&self, message: String) -> Result<JsSignMessageResponse> {
pub async fn sign_message(&self, message: &str) -> Result<JsSignMessageResponse> {
Ok(self
.inner
.sign_message(message)
Expand Down
44 changes: 10 additions & 34 deletions webln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,9 @@ pub enum GetInfoMethod {
Other(String),
}

impl<S> From<S> for GetInfoMethod
where
S: AsRef<str>,
{
fn from(method: S) -> Self {
match method.as_ref() {
impl From<&str> for GetInfoMethod {
fn from(method: &str) -> Self {
match method {
IS_ENABLED => Self::IsEnabled,
ENABLE => Self::Enable,
GET_INFO => Self::GetInfo,
Expand Down Expand Up @@ -246,11 +243,8 @@ impl RequestInvoiceArgs {
}

/// Set default memo
pub fn default_memo<S>(mut self, default_memo: S) -> Self
where
S: Into<String>,
{
self.default_memo = Some(default_memo.into());
pub fn default_memo(mut self, default_memo: String) -> Self {
self.default_memo = Some(default_memo);
self
}
}
Expand Down Expand Up @@ -349,11 +343,7 @@ impl WebLN {
Ok(Self { webln_obj })
}

fn get_func<S>(&self, obj: &Object, name: S) -> Result<Function, Error>
where
S: AsRef<str>,
{
let name: &str = name.as_ref();
fn get_func(&self, obj: &Object, name: &str) -> Result<Function, Error> {
let val: JsValue = Reflect::get(obj, &JsValue::from_str(name))
.map_err(|_| Error::NamespaceNotFound(name.to_string()))?;
val.dyn_into()
Expand Down Expand Up @@ -407,7 +397,7 @@ impl WebLN {
let methods: Vec<GetInfoMethod> = methods_array
.into_iter()
.filter_map(|m| m.as_string())
.map(GetInfoMethod::from)
.map(|m| GetInfoMethod::from(m.as_str()))
.collect();

Ok(GetInfoResponse {
Expand Down Expand Up @@ -474,12 +464,7 @@ impl WebLN {
}

/// Request that the user sends a payment for an invoice.
pub async fn send_payment<S>(&self, invoice: S) -> Result<SendPaymentResponse, Error>
where
S: AsRef<str>,
{
let invoice: &str = invoice.as_ref();

pub async fn send_payment(&self, invoice: &str) -> Result<SendPaymentResponse, Error> {
// `lightning-invoice` increase too much the WASM binary size
// For now just check if invoice is not empty
if invoice.is_empty() {
Expand All @@ -502,12 +487,7 @@ impl WebLN {
/// The payment will only be initiated and will not wait for a preimage to be returned.
/// This is useful when paying HOLD Invoices. There is no guarantee that the payment will be successfully sent to the receiver.
/// It's up to the receiver to check whether or not the invoice has been paid.
pub async fn send_payment_async<S>(&self, invoice: S) -> Result<(), Error>
where
S: AsRef<str>,
{
let invoice: &str = invoice.as_ref();

pub async fn send_payment_async(&self, invoice: &str) -> Result<(), Error> {
// `lightning-invoice` increase too much the WASM binary size
// For now just check if invoice is not empty
if invoice.is_empty() {
Expand All @@ -526,11 +506,7 @@ impl WebLN {
}

/// Request that the user signs an arbitrary string message.
pub async fn sign_message<S>(&self, message: S) -> Result<SignMessageResponse, Error>
where
S: AsRef<str>,
{
let message: &str = message.as_ref();
pub async fn sign_message(&self, message: &str) -> Result<SignMessageResponse, Error> {
let func: Function = self.get_func(&self.webln_obj, SIGN_MESSAGE)?;
let promise: Promise = Promise::resolve(&func.call1(&self.webln_obj, &message.into())?);
let result: JsValue = JsFuture::from(promise).await?;
Expand Down

0 comments on commit 2dc418d

Please sign in to comment.