Skip to content

Commit 9993331

Browse files
authored
FFI: Eliminate string fallbacks in error wrappers (payjoin#1127)
2 parents 2d932a1 + ded2e6d commit 9993331

3 files changed

Lines changed: 17 additions & 23 deletions

File tree

payjoin-ffi/src/ohttp.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
pub use error::OhttpError;
22

33
pub mod error {
4-
#[derive(Debug, PartialEq, Eq, thiserror::Error, uniffi::Object)]
5-
#[error("OHTTP error: {message}")]
6-
pub struct OhttpError {
7-
message: String,
8-
}
9-
impl From<ohttp::Error> for OhttpError {
10-
fn from(value: ohttp::Error) -> Self { OhttpError { message: format!("{value:?}") } }
11-
}
12-
impl From<String> for OhttpError {
13-
fn from(value: String) -> Self { OhttpError { message: value } }
14-
}
4+
#[derive(Debug, thiserror::Error, uniffi::Object)]
5+
#[error(transparent)]
6+
pub struct OhttpError(#[from] ohttp::Error);
157
}
168

179
impl From<payjoin::OhttpKeys> for OhttpKeys {

payjoin-ffi/src/uri/error.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub struct PjParseError {
44
msg: String,
55
}
66

7-
impl From<String> for PjParseError {
8-
fn from(msg: String) -> Self { PjParseError { msg } }
7+
impl PjParseError {
8+
pub(crate) fn from_err(err: impl std::fmt::Display) -> Self { Self { msg: err.to_string() } }
99
}
1010

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

17-
impl From<String> for PjNotSupported {
18-
fn from(msg: String) -> Self { PjNotSupported { msg } }
17+
impl PjNotSupported {
18+
pub(crate) fn from_display(uri: impl std::fmt::Display) -> Self {
19+
Self { msg: uri.to_string() }
20+
}
1921
}
2022

2123
#[derive(Debug, thiserror::Error, uniffi::Object)]

payjoin-ffi/src/uri/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ impl From<payjoin::Uri<'static, NetworkChecked>> for Uri {
2020
impl Uri {
2121
#[uniffi::constructor]
2222
pub fn parse(uri: String) -> Result<Self, PjParseError> {
23-
match payjoin::Uri::from_str(uri.as_str()) {
24-
Ok(e) => Ok(e.assume_checked().into()),
25-
Err(e) => Err(e.to_string().into()),
26-
}
23+
payjoin::Uri::from_str(uri.as_str())
24+
.map(|e| e.assume_checked().into())
25+
.map_err(PjParseError::from_err)
2726
}
2827
pub fn address(&self) -> String { self.clone().0.address.to_string() }
2928
/// Gets the amount in satoshis.
@@ -36,10 +35,11 @@ impl Uri {
3635
}
3736

3837
pub fn check_pj_supported(&self) -> Result<Arc<PjUri>, PjNotSupported> {
39-
match self.0.clone().check_pj_supported() {
40-
Ok(e) => Ok(Arc::new(e.into())),
41-
Err(uri) => Err(uri.to_string().into()),
42-
}
38+
self.0
39+
.clone()
40+
.check_pj_supported()
41+
.map(|uri| Arc::new(uri.into()))
42+
.map_err(PjNotSupported::from_display)
4343
}
4444
pub fn as_string(&self) -> String { self.0.clone().to_string() }
4545
}

0 commit comments

Comments
 (0)