Return FfiResult errors as FfiError enum short values
This commit is contained in:
		
							parent
							
								
									2abe7205cb
								
							
						
					
					
						commit
						6361b41f33
					
				
							
								
								
									
										134
									
								
								src/error.rs
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								src/error.rs
									
									
									
									
									
								
							| @ -1,46 +1,94 @@ | |||||||
|  | use ::safer_ffi::prelude::*; | ||||||
| use bdk::Error; | use bdk::Error; | ||||||
| 
 | 
 | ||||||
| pub fn get_name(error: &bdk::Error) -> String { | #[derive_ReprC] | ||||||
|     match error { | #[repr(u16)] | ||||||
|         Error::InvalidU32Bytes(_) => "InvalidU32Bytes", | #[derive(Debug)] | ||||||
|         Error::Generic(_) => "Generic", | pub enum FfiError { | ||||||
|         Error::ScriptDoesntHaveAddressForm => "ScriptDoesntHaveAddressForm", |     None, | ||||||
|         Error::SingleRecipientMultipleOutputs => "SingleRecipientMultipleOutputs", |     InvalidU32Bytes, | ||||||
|         Error::SingleRecipientNoInputs => "SingleRecipientNoInputs", |     Generic, | ||||||
|         Error::NoRecipients => "NoRecipients", |     ScriptDoesntHaveAddressForm, | ||||||
|         Error::NoUtxosSelected => "NoUtxosSelected", |     SingleRecipientMultipleOutputs, | ||||||
|         Error::OutputBelowDustLimit(_) => "OutputBelowDustLimit", |     SingleRecipientNoInputs, | ||||||
|         Error::InsufficientFunds { .. } => "InsufficientFunds", |     NoRecipients, | ||||||
|         Error::BnBTotalTriesExceeded => "BnBTotalTriesExceeded", |     NoUtxosSelected, | ||||||
|         Error::BnBNoExactMatch => "BnBNoExactMatch", |     OutputBelowDustLimit, | ||||||
|         Error::UnknownUtxo => "UnknownUtxo", |     InsufficientFunds, | ||||||
|         Error::TransactionNotFound => "TransactionNotFound", |     BnBTotalTriesExceeded, | ||||||
|         Error::TransactionConfirmed => "TransactionConfirmed", |     BnBNoExactMatch, | ||||||
|         Error::IrreplaceableTransaction => "IrreplaceableTransaction", |     UnknownUtxo, | ||||||
|         Error::FeeRateTooLow { .. } => "FeeRateTooLow", |     TransactionNotFound, | ||||||
|         Error::FeeTooLow { .. } => "FeeTooLow", |     TransactionConfirmed, | ||||||
|         Error::MissingKeyOrigin(_) => "MissingKeyOrigin", |     IrreplaceableTransaction, | ||||||
|         Error::Key(_) => "Key", |     FeeRateTooLow, | ||||||
|         Error::ChecksumMismatch => "ChecksumMismatch", |     FeeTooLow, | ||||||
|         Error::SpendingPolicyRequired(_) => "SpendingPolicyRequired", |     MissingKeyOrigin, | ||||||
|         Error::InvalidPolicyPathError(_) => "InvalidPolicyPathError", |     Key, | ||||||
|         Error::Signer(_) => "Signer", |     ChecksumMismatch, | ||||||
|         Error::InvalidProgressValue(_) => "InvalidProgressValue", |     SpendingPolicyRequired, | ||||||
|         Error::ProgressUpdateError => "ProgressUpdateError", |     InvalidPolicyPathError, | ||||||
|         Error::InvalidOutpoint(_) => "InvalidOutpoint", |     Signer, | ||||||
|         Error::Descriptor(_) => "Descriptor", |     InvalidProgressValue, | ||||||
|         Error::AddressValidator(_) => "AddressValidator", |     ProgressUpdateError, | ||||||
|         Error::Encode(_) => "Encode", |     InvalidOutpoint, | ||||||
|         Error::Miniscript(_) => "Miniscript", |     Descriptor, | ||||||
|         Error::Bip32(_) => "Bip32", |     AddressValidator, | ||||||
|         Error::Secp256k1(_) => "Secp256k1", |     Encode, | ||||||
|         Error::Json(_) => "Json", |     Miniscript, | ||||||
|         Error::Hex(_) => "Hex", |     Bip32, | ||||||
|         Error::Psbt(_) => "Psbt", |     Secp256k1, | ||||||
|         Error::Electrum(_) => "Electrum", |     Json, | ||||||
|         //        Error::Esplora(_) => "Esplora",
 |     Hex, | ||||||
|         //        Error::CompactFilters(_) => "CompactFilters",
 |     Psbt, | ||||||
|         Error::Sled(_) => "Sled", |     Electrum, | ||||||
|     } |     // Esplora,
 | ||||||
|     .to_string() |     // CompactFilters,
 | ||||||
|  |     Sled, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl From<&bdk::Error> for FfiError { | ||||||
|  |     fn from(error: &bdk::Error) -> Self { | ||||||
|  |         match error { | ||||||
|  |             Error::InvalidU32Bytes(_) => FfiError::InvalidU32Bytes, | ||||||
|  |             Error::Generic(_) => FfiError::Generic, | ||||||
|  |             Error::ScriptDoesntHaveAddressForm => FfiError::ScriptDoesntHaveAddressForm, | ||||||
|  |             Error::SingleRecipientMultipleOutputs => FfiError::SingleRecipientMultipleOutputs, | ||||||
|  |             Error::SingleRecipientNoInputs => FfiError::SingleRecipientNoInputs, | ||||||
|  |             Error::NoRecipients => FfiError::NoRecipients, | ||||||
|  |             Error::NoUtxosSelected => FfiError::NoUtxosSelected, | ||||||
|  |             Error::OutputBelowDustLimit(_) => FfiError::OutputBelowDustLimit, | ||||||
|  |             Error::InsufficientFunds { .. } => FfiError::InsufficientFunds, | ||||||
|  |             Error::BnBTotalTriesExceeded => FfiError::BnBTotalTriesExceeded, | ||||||
|  |             Error::BnBNoExactMatch => FfiError::BnBNoExactMatch, | ||||||
|  |             Error::UnknownUtxo => FfiError::UnknownUtxo, | ||||||
|  |             Error::TransactionNotFound => FfiError::TransactionNotFound, | ||||||
|  |             Error::TransactionConfirmed => FfiError::TransactionConfirmed, | ||||||
|  |             Error::IrreplaceableTransaction => FfiError::IrreplaceableTransaction, | ||||||
|  |             Error::FeeRateTooLow { .. } => FfiError::FeeRateTooLow, | ||||||
|  |             Error::FeeTooLow { .. } => FfiError::FeeTooLow, | ||||||
|  |             Error::MissingKeyOrigin(_) => FfiError::MissingKeyOrigin, | ||||||
|  |             Error::Key(_) => FfiError::Key, | ||||||
|  |             Error::ChecksumMismatch => FfiError::ChecksumMismatch, | ||||||
|  |             Error::SpendingPolicyRequired(_) => FfiError::SpendingPolicyRequired, | ||||||
|  |             Error::InvalidPolicyPathError(_) => FfiError::InvalidPolicyPathError, | ||||||
|  |             Error::Signer(_) => FfiError::Signer, | ||||||
|  |             Error::InvalidProgressValue(_) => FfiError::InvalidProgressValue, | ||||||
|  |             Error::ProgressUpdateError => FfiError::ProgressUpdateError, | ||||||
|  |             Error::InvalidOutpoint(_) => FfiError::InvalidOutpoint, | ||||||
|  |             Error::Descriptor(_) => FfiError::Descriptor, | ||||||
|  |             Error::AddressValidator(_) => FfiError::AddressValidator, | ||||||
|  |             Error::Encode(_) => FfiError::Encode, | ||||||
|  |             Error::Miniscript(_) => FfiError::Miniscript, | ||||||
|  |             Error::Bip32(_) => FfiError::Bip32, | ||||||
|  |             Error::Secp256k1(_) => FfiError::Secp256k1, | ||||||
|  |             Error::Json(_) => FfiError::Json, | ||||||
|  |             Error::Hex(_) => FfiError::Hex, | ||||||
|  |             Error::Psbt(_) => FfiError::Psbt, | ||||||
|  |             Error::Electrum(_) => FfiError::Electrum, | ||||||
|  |             //        Error::Esplora(_) => JniError::Esplora,
 | ||||||
|  |             //        Error::CompactFilters(_) => JniError::CompactFilters,
 | ||||||
|  |             Error::Sled(_) => FfiError::Sled, | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,3 +1,4 @@ | |||||||
|  | use crate::error::FfiError; | ||||||
| use ::safer_ffi::prelude::*; | use ::safer_ffi::prelude::*; | ||||||
| use safer_ffi::char_p::char_p_boxed; | use safer_ffi::char_p::char_p_boxed; | ||||||
| 
 | 
 | ||||||
| @ -6,14 +7,14 @@ use safer_ffi::char_p::char_p_boxed; | |||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct FfiResult<T> { | pub struct FfiResult<T> { | ||||||
|     pub ok: T, |     pub ok: T, | ||||||
|     pub err: char_p_boxed, |     pub err: FfiError, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive_ReprC] | #[derive_ReprC] | ||||||
| #[repr(C)] | #[repr(C)] | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct FfiResultVoid { | pub struct FfiResultVoid { | ||||||
|     pub err: char_p_boxed, |     pub err: FfiError, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[ffi_export] | #[ffi_export] | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ use safer_ffi::char_p::{char_p_boxed, char_p_ref}; | |||||||
| use blockchain::BlockchainConfig; | use blockchain::BlockchainConfig; | ||||||
| use database::DatabaseConfig; | use database::DatabaseConfig; | ||||||
| 
 | 
 | ||||||
| use crate::error::get_name; | use crate::error::FfiError; | ||||||
| use crate::types::{FfiResult, FfiResultVoid}; | use crate::types::{FfiResult, FfiResultVoid}; | ||||||
| use std::ffi::CString; | use std::ffi::CString; | ||||||
| 
 | 
 | ||||||
| @ -43,11 +43,11 @@ fn new_wallet_result( | |||||||
|     match wallet_result { |     match wallet_result { | ||||||
|         Ok(w) => FfiResult { |         Ok(w) => FfiResult { | ||||||
|             ok: Some(Box::new(OpaqueWallet { raw: w })), |             ok: Some(Box::new(OpaqueWallet { raw: w })), | ||||||
|             err: char_p_boxed::from(CString::default()), |             err: FfiError::None, | ||||||
|         }, |         }, | ||||||
|         Err(e) => FfiResult { |         Err(e) => FfiResult { | ||||||
|             ok: None, |             ok: None, | ||||||
|             err: char_p_boxed::try_from(get_name(&e)).unwrap(), |             err: FfiError::from(&e), | ||||||
|         }, |         }, | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -81,10 +81,10 @@ fn sync_wallet(opaque_wallet: &OpaqueWallet) -> FfiResultVoid { | |||||||
|     let int_result = opaque_wallet.raw.sync(log_progress(), Some(100)); |     let int_result = opaque_wallet.raw.sync(log_progress(), Some(100)); | ||||||
|     match int_result { |     match int_result { | ||||||
|         Ok(_v) => FfiResultVoid { |         Ok(_v) => FfiResultVoid { | ||||||
|             err: char_p_boxed::from(CString::default()), |             err: FfiError::None, | ||||||
|         }, |         }, | ||||||
|         Err(e) => FfiResultVoid { |         Err(e) => FfiResultVoid { | ||||||
|             err: char_p_boxed::try_from(get_name(&e)).unwrap(), |             err: FfiError::from(&e), | ||||||
|         }, |         }, | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -96,11 +96,11 @@ fn new_address(opaque_wallet: &OpaqueWallet) -> FfiResult<char_p_boxed> { | |||||||
|     match string_result { |     match string_result { | ||||||
|         Ok(a) => FfiResult { |         Ok(a) => FfiResult { | ||||||
|             ok: char_p_boxed::try_from(a).unwrap(), |             ok: char_p_boxed::try_from(a).unwrap(), | ||||||
|             err: char_p_boxed::from(CString::default()), |             err: FfiError::None, | ||||||
|         }, |         }, | ||||||
|         Err(e) => FfiResult { |         Err(e) => FfiResult { | ||||||
|             ok: char_p_boxed::from(CString::default()), |             ok: char_p_boxed::from(CString::default()), | ||||||
|             err: char_p_boxed::try_from(get_name(&e)).unwrap(), |             err: FfiError::from(&e), | ||||||
|         }, |         }, | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -115,11 +115,11 @@ fn list_unspent(opaque_wallet: &OpaqueWallet) -> FfiResult<repr_c::Vec<LocalUtxo | |||||||
|                 let ve: Vec<LocalUtxo> = v.iter().map(|lu| LocalUtxo::from(lu)).collect(); |                 let ve: Vec<LocalUtxo> = v.iter().map(|lu| LocalUtxo::from(lu)).collect(); | ||||||
|                 repr_c::Vec::from(ve) |                 repr_c::Vec::from(ve) | ||||||
|             }, |             }, | ||||||
|             err: char_p_boxed::from(CString::default()), |             err: FfiError::None, | ||||||
|         }, |         }, | ||||||
|         Err(e) => FfiResult { |         Err(e) => FfiResult { | ||||||
|             ok: repr_c::Vec::EMPTY, |             ok: repr_c::Vec::EMPTY, | ||||||
|             err: char_p_boxed::try_from(get_name(&e)).unwrap(), |             err: FfiError::from(&e), | ||||||
|         }, |         }, | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user