Skip to content

Commit 99f6871

Browse files
committed
Add GetBalancesError
Add a error type for GetBalances.
1 parent 4687d2c commit 99f6871

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

types/src/v19/wallet/error.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
use core::fmt;
4+
5+
use bitcoin::amount::ParseAmountError;
6+
7+
use crate::error::write_err;
8+
9+
/// Error when converting a `GetBalances` type into the model type.
10+
#[derive(Debug)]
11+
pub enum GetBalancesError {
12+
/// Conversion of the `mine` field failed.
13+
Mine(ParseAmountError),
14+
/// Conversion of the `watchonly` field failed.
15+
WatchOnly(ParseAmountError),
16+
}
17+
18+
impl fmt::Display for GetBalancesError {
19+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20+
use GetBalancesError as E;
21+
22+
match *self {
23+
E::Mine(ref e) => write_err!(f, "conversion of the `mine` field failed"; e),
24+
E::WatchOnly(ref e) => write_err!(f, "conversion of the `watchonly` field failed"; e),
25+
}
26+
}
27+
}
28+
29+
#[cfg(feature = "std")]
30+
impl std::error::Error for GetBalancesError {
31+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32+
use GetBalancesError as E;
33+
34+
match *self {
35+
E::Mine(ref e) => Some(e),
36+
E::WatchOnly(ref e) => Some(e),
37+
}
38+
}
39+
}

types/src/v19/wallet/into.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@ use bitcoin::amount::ParseAmountError;
44
use bitcoin::consensus::encode;
55
use bitcoin::{Amount, BlockHash, SignedAmount, Transaction, Txid};
66

7-
use super::{GetBalances, GetBalancesMine, GetBalancesWatchOnly, GetTransaction, GetTransactionError};
7+
use super::{
8+
GetBalances, GetBalancesError, GetBalancesMine, GetBalancesWatchOnly, GetTransaction,
9+
GetTransactionError,
10+
};
811
use crate::model;
912

1013
impl GetBalances {
1114
/// Converts version specific type to a version nonspecific, more strongly typed type.
12-
pub fn into_model(self) -> Result<model::GetBalances, ParseAmountError> {
13-
let mine = self.mine.into_model()?;
14-
let watch_only = self.watch_only.map(|watch_only| watch_only.into_model()).transpose()?;
15+
pub fn into_model(self) -> Result<model::GetBalances, GetBalancesError> {
16+
use GetBalancesError as E;
17+
18+
let mine = self.mine.into_model().map_err(E::Mine)?;
19+
let watch_only = self
20+
.watch_only
21+
.map(|watch_only| watch_only.into_model())
22+
.transpose()
23+
.map_err(E::WatchOnly)?;
1524

1625
Ok(model::GetBalances { mine, watch_only })
1726
}

types/src/v19/wallet/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
//!
55
//! Types for methods found under the `== Wallet ==` section of the API docs.
66
7+
mod error;
78
mod into;
89

910
use bitcoin::Transaction;
1011
use serde::{Deserialize, Serialize};
1112

13+
pub use self::error::GetBalancesError;
1214
use super::{Bip125Replaceable, GetTransactionDetail, GetTransactionError};
1315

1416
/// Result of the JSON-RPC method `getbalances`.

0 commit comments

Comments
 (0)