Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions src/uri/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
#[error("Error parsing the payjoin URI: {msg}")]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct PjParseError {
msg: String,
}

impl From<String> for PjParseError {
fn from(msg: String) -> Self {
PjParseError { msg }
}
}
Comment on lines +8 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very smelly, address it in the audit. If we need this because we depend on bitcoin_uri errors in the public API, maybe we should reconsider those rather than making arbitrary string conversions part of the API.

Manual map_err seems a lot more appropriate in this circumstance.


#[derive(Debug, PartialEq, Eq, thiserror::Error)]
#[error("URI doesn't support payjoin: {msg}")]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct PjNotSupported {
msg: String,
}

impl From<String> for PjNotSupported {
fn from(msg: String) -> Self {
PjNotSupported { msg }
}
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
#[error("Error parsing URL: {msg}")]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct UrlParseError {
msg: String,
}

impl From<payjoin::ParseError> for UrlParseError {
fn from(value: payjoin::ParseError) -> Self {
UrlParseError { msg: format!("{:?}", value) }
}
}
30 changes: 10 additions & 20 deletions src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::str::FromStr;
#[cfg(feature = "uniffi")]
use std::sync::Arc;

pub use error::{PjNotSupported, PjParseError, UrlParseError};
use payjoin::bitcoin::address::NetworkChecked;
use payjoin::UriExt;

use crate::error::PayjoinError;
pub mod error;
#[derive(Clone)]
pub struct Uri(payjoin::Uri<'static, NetworkChecked>);
impl From<Uri> for payjoin::Uri<'static, NetworkChecked> {
Expand All @@ -21,10 +22,10 @@ impl From<payjoin::Uri<'static, NetworkChecked>> for Uri {
}

impl Uri {
pub fn parse(uri: String) -> Result<Self, PayjoinError> {
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(PayjoinError::PjParseError { msg: e.to_string() }),
Err(e) => Err(e.to_string().into()),
}
}
pub fn address(&self) -> String {
Expand All @@ -41,25 +42,17 @@ impl Uri {
self.0.message.clone().and_then(|x| String::try_from(x).ok())
}
#[cfg(not(feature = "uniffi"))]
pub fn check_pj_supported(&self) -> Result<PjUri, PayjoinError> {
pub fn check_pj_supported(&self) -> Result<PjUri, PjNotSupported> {
match self.0.clone().check_pj_supported() {
Ok(e) => Ok(e.into()),
Err(_) => {
Err(PayjoinError::PjNotSupported {
msg: "Uri doesn't support payjoin".to_string(),
})
}
Err(uri) => Err(uri.to_string().into()),
}
}
#[cfg(feature = "uniffi")]
pub fn check_pj_supported(&self) -> Result<Arc<PjUri>, PayjoinError> {
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(_) => {
Err(PayjoinError::PjNotSupported {
msg: "Uri doesn't support payjoin".to_string(),
})
}
Err(uri) => Err(uri.to_string().into()),
}
}
pub fn as_string(&self) -> String {
Expand Down Expand Up @@ -121,11 +114,8 @@ pub struct Url(payjoin::Url);
#[cfg_attr(feature = "uniffi", uniffi::export)]
impl Url {
#[cfg_attr(feature = "uniffi", uniffi::constructor)]
pub fn parse(input: String) -> Result<Url, PayjoinError> {
match payjoin::Url::parse(input.as_str()) {
Ok(e) => Ok(Self(e)),
Err(e) => Err(PayjoinError::UnexpectedError { msg: e.to_string() }),
}
pub fn parse(input: String) -> Result<Url, UrlParseError> {
payjoin::Url::parse(input.as_str()).map_err(Into::into).map(Self)
}
pub fn query(&self) -> Option<String> {
self.0.query().map(|x| x.to_string())
Expand Down