Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions payjoin-ffi/src/ohttp.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
pub use error::OhttpError;

pub mod error {
#[derive(Debug, PartialEq, Eq, thiserror::Error, uniffi::Object)]
#[error("OHTTP error: {message}")]
pub struct OhttpError {
message: String,
}
impl From<ohttp::Error> for OhttpError {
fn from(value: ohttp::Error) -> Self { OhttpError { message: format!("{value:?}") } }
}
impl From<String> for OhttpError {
fn from(value: String) -> Self { OhttpError { message: value } }
}
#[derive(Debug, thiserror::Error, uniffi::Object)]
#[error(transparent)]
pub struct OhttpError(#[from] ohttp::Error);
}

impl From<payjoin::OhttpKeys> for OhttpKeys {
Expand Down
10 changes: 6 additions & 4 deletions payjoin-ffi/src/uri/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub struct PjParseError {
msg: String,
}

impl From<String> for PjParseError {
fn from(msg: String) -> Self { PjParseError { msg } }
impl PjParseError {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like what you did with OhttpError, changing it to:

pub struct OhttpError(#[from] ohttp::Error);

Is there something preventing us from doing the same thing for PjParseError and PjNotSupported? If yes it would be good to document why we take a different approach for these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went string-backed here because the core payjoin::PjParseError/PjNotSupported don’t currently derive PartialEq/Eq; our fixtures check these errors by comparing values, so swapping to a transparent wrapper would break those comparisons unless we taught the core types how to compare or wrote custom equality glue.
Without those traits, the generated bindings—and our existing tests that compare errors—would break once we stop stringifying.

pub(crate) fn from_err(err: impl std::fmt::Display) -> Self { Self { msg: err.to_string() } }
}

#[derive(Debug, PartialEq, Eq, thiserror::Error, uniffi::Object)]
Expand All @@ -14,8 +14,10 @@ pub struct PjNotSupported {
msg: String,
}

impl From<String> for PjNotSupported {
fn from(msg: String) -> Self { PjNotSupported { msg } }
impl PjNotSupported {
pub(crate) fn from_display(uri: impl std::fmt::Display) -> Self {
Self { msg: uri.to_string() }
}
}

#[derive(Debug, thiserror::Error, uniffi::Object)]
Expand Down
16 changes: 8 additions & 8 deletions payjoin-ffi/src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ impl From<payjoin::Uri<'static, NetworkChecked>> for Uri {
impl Uri {
#[uniffi::constructor]
pub fn parse(uri: String) -> Result<Self, PjParseError> {
match payjoin::Uri::from_str(uri.as_str()) {
Ok(e) => Ok(e.assume_checked().into()),
Err(e) => Err(e.to_string().into()),
}
payjoin::Uri::from_str(uri.as_str())
.map(|e| e.assume_checked().into())
.map_err(PjParseError::from_err)
}
pub fn address(&self) -> String { self.clone().0.address.to_string() }
/// Gets the amount in satoshis.
Expand All @@ -36,10 +35,11 @@ impl Uri {
}

pub fn check_pj_supported(&self) -> Result<Arc<PjUri>, PjNotSupported> {
match self.0.clone().check_pj_supported() {
Ok(e) => Ok(Arc::new(e.into())),
Err(uri) => Err(uri.to_string().into()),
}
self.0
.clone()
.check_pj_supported()
.map(|uri| Arc::new(uri.into()))
.map_err(PjNotSupported::from_display)
}
pub fn as_string(&self) -> String { self.0.clone().to_string() }
}
Expand Down