diff --git a/rust/src/api/io.rs b/rust/src/api/io.rs index 9d5dfa6..ce91426 100644 --- a/rust/src/api/io.rs +++ b/rust/src/api/io.rs @@ -45,13 +45,7 @@ pub async fn fetch_ohttp_keys_with_cert( } pub mod error { - use crate::frb_generated::RustAutoOpaque; + use crate::ffi_opaque_wrapper; - pub struct FfiIoError(pub(crate) RustAutoOpaque); - - impl From for FfiIoError { - fn from(value: payjoin_ffi::io::IoError) -> Self { - Self(RustAutoOpaque::new(value)) - } - } + ffi_opaque_wrapper!(FfiIoError, payjoin_ffi::io::IoError); } diff --git a/rust/src/api/mod.rs b/rust/src/api/mod.rs index 865885c..66b4916 100644 --- a/rust/src/api/mod.rs +++ b/rust/src/api/mod.rs @@ -4,26 +4,28 @@ pub mod receive; pub mod send; pub mod uri; -use crate::frb_generated::RustAutoOpaque; - -pub struct FfiSerdeJsonError(pub(crate) RustAutoOpaque); - -impl From for FfiSerdeJsonError { - fn from(value: payjoin_ffi::error::SerdeJsonError) -> Self { - Self(RustAutoOpaque::new(value)) - } +#[macro_export] +macro_rules! ffi_opaque_wrapper { + ($wrapper:ident, $inner:path) => { + pub struct $wrapper(pub(crate) crate::frb_generated::RustAutoOpaque<$inner>); + impl From<$inner> for $wrapper { + fn from(value: $inner) -> Self { + Self(crate::frb_generated::RustAutoOpaque::new(value)) + } + } + impl $wrapper { + #[flutter_rust_bridge::frb(sync)] + pub fn message(&self) -> String { + self.0.blocking_read().to_string() + } + } + }; } +ffi_opaque_wrapper!(FfiSerdeJsonError, payjoin_ffi::error::SerdeJsonError); + pub mod ohttp { pub mod error { - use crate::frb_generated::RustAutoOpaque; - - pub struct FfiOhttpError(pub(crate) RustAutoOpaque); - - impl From for FfiOhttpError { - fn from(value: payjoin_ffi::ohttp::error::OhttpError) -> Self { - Self(RustAutoOpaque::new(value)) - } - } + ffi_opaque_wrapper!(FfiOhttpError, payjoin_ffi::ohttp::error::OhttpError); } } diff --git a/rust/src/api/receive/error.rs b/rust/src/api/receive/error.rs index c1ca51a..bebe2d8 100644 --- a/rust/src/api/receive/error.rs +++ b/rust/src/api/receive/error.rs @@ -1,82 +1,20 @@ -use crate::frb_generated::RustAutoOpaque; +use crate::ffi_opaque_wrapper; -pub struct FfiError(pub(crate) RustAutoOpaque); +ffi_opaque_wrapper!(FfiError, payjoin_ffi::receive::Error); +ffi_opaque_wrapper!(FfiImplementationError, payjoin_ffi::receive::ImplementationError); +ffi_opaque_wrapper!(FfiSessionError, payjoin_ffi::receive::SessionError); +ffi_opaque_wrapper!(FfiInputContributionError, payjoin_ffi::receive::InputContributionError); +ffi_opaque_wrapper!(FfiOutputSubstitutionError, payjoin_ffi::receive::OutputSubstitutionError); +ffi_opaque_wrapper!(FfiPsbtInputError, payjoin_ffi::receive::PsbtInputError); +ffi_opaque_wrapper!(FfiReplyableError, payjoin_ffi::receive::ReplyableError); +ffi_opaque_wrapper!(FfiSelectionError, payjoin_ffi::receive::SelectionError); -impl From for FfiError { - fn from(value: payjoin_ffi::receive::Error) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiImplementationError( - pub(crate) RustAutoOpaque, -); - -impl From for FfiImplementationError { - fn from(value: payjoin_ffi::receive::ImplementationError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiSessionError(pub(crate) RustAutoOpaque); - -impl From for FfiSessionError { - fn from(value: payjoin_ffi::receive::SessionError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiInputContributionError( - pub RustAutoOpaque, -); - -impl From for FfiInputContributionError { - fn from(value: payjoin_ffi::receive::InputContributionError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiOutputSubstitutionError( - pub(crate) RustAutoOpaque, -); - -impl From for FfiOutputSubstitutionError { - fn from(value: payjoin_ffi::receive::OutputSubstitutionError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiPsbtInputError(pub(crate) RustAutoOpaque); - -impl From for FfiPsbtInputError { - fn from(value: payjoin_ffi::receive::PsbtInputError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiReplyableError(pub(crate) RustAutoOpaque); - -impl From for FfiReplyableError { - fn from(value: payjoin_ffi::receive::ReplyableError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiSelectionError(pub(crate) RustAutoOpaque); - -impl From for FfiSelectionError { - fn from(value: payjoin_ffi::receive::SelectionError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} pub struct FfiJsonReply(pub(crate) payjoin_ffi::receive::JsonReply); - impl From for FfiJsonReply { fn from(value: payjoin_ffi::receive::JsonReply) -> Self { Self(value) } } - impl From for payjoin_ffi::receive::JsonReply { fn from(value: FfiJsonReply) -> Self { value.0 diff --git a/rust/src/api/send/error.rs b/rust/src/api/send/error.rs index 03f3bdd..0ca678f 100644 --- a/rust/src/api/send/error.rs +++ b/rust/src/api/send/error.rs @@ -1,4 +1,4 @@ -use crate::frb_generated::RustAutoOpaque; +use crate::ffi_opaque_wrapper; pub struct FfiBuildSenderError { pub(crate) msg: String, @@ -30,20 +30,6 @@ impl From for FfiEncapsulationError { } } -pub struct FfiValidationError(pub(crate) RustAutoOpaque); - -impl From for FfiValidationError { - fn from(value: payjoin_ffi::send::error::ValidationError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiWellKnownError(pub(crate) RustAutoOpaque); - -pub struct FfiResponseError(pub(crate) RustAutoOpaque); - -impl From for FfiResponseError { - fn from(value: payjoin_ffi::send::error::ResponseError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} +ffi_opaque_wrapper!(FfiValidationError, payjoin_ffi::send::error::ValidationError); +ffi_opaque_wrapper!(FfiWellKnownError, payjoin_ffi::send::error::WellKnownError); +ffi_opaque_wrapper!(FfiResponseError, payjoin_ffi::send::error::ResponseError); diff --git a/rust/src/api/uri/error.rs b/rust/src/api/uri/error.rs index b3c17a4..ec1fcc8 100644 --- a/rust/src/api/uri/error.rs +++ b/rust/src/api/uri/error.rs @@ -1,33 +1,6 @@ -use crate::frb_generated::RustAutoOpaque; +use crate::ffi_opaque_wrapper; -pub struct FfiIntoUrlError(pub(crate) RustAutoOpaque); - -impl From for FfiIntoUrlError { - fn from(value: payjoin_ffi::uri::error::IntoUrlError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiPjParseError(pub(crate) RustAutoOpaque); - -impl From for FfiPjParseError { - fn from(value: payjoin_ffi::uri::PjParseError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiPjNotSupported(pub(crate) RustAutoOpaque); - -impl From for FfiPjNotSupported { - fn from(value: payjoin_ffi::uri::PjNotSupported) -> Self { - Self(RustAutoOpaque::new(value)) - } -} - -pub struct FfiUrlParseError(pub(crate) RustAutoOpaque); - -impl From for FfiUrlParseError { - fn from(value: payjoin_ffi::uri::UrlParseError) -> Self { - Self(RustAutoOpaque::new(value)) - } -} +ffi_opaque_wrapper!(FfiIntoUrlError, payjoin_ffi::uri::error::IntoUrlError); +ffi_opaque_wrapper!(FfiPjParseError, payjoin_ffi::uri::PjParseError); +ffi_opaque_wrapper!(FfiPjNotSupported, payjoin_ffi::uri::PjNotSupported); +ffi_opaque_wrapper!(FfiUrlParseError, payjoin_ffi::uri::UrlParseError);