Skip to content

Commit be7df15

Browse files
committed
Encapsulate internal error variants in tuple
No need for them to be named
1 parent c275329 commit be7df15

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

payjoin/src/send/error.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ impl std::error::Error for BuildSenderError {
8686
/// This is currently opaque type because we aren't sure which variants will stay.
8787
/// You can only display it.
8888
#[derive(Debug)]
89-
pub struct ValidationError {
90-
internal: InternalValidationError,
91-
}
89+
pub struct ValidationError(InternalValidationError);
9290

9391
#[derive(Debug)]
9492
pub(crate) enum InternalValidationError {
@@ -100,24 +98,22 @@ pub(crate) enum InternalValidationError {
10098
}
10199

102100
impl From<InternalValidationError> for ValidationError {
103-
fn from(value: InternalValidationError) -> Self { ValidationError { internal: value } }
101+
fn from(value: InternalValidationError) -> Self { ValidationError(value) }
104102
}
105103

106104
impl From<crate::psbt::AddressTypeError> for ValidationError {
107105
fn from(value: crate::psbt::AddressTypeError) -> Self {
108-
ValidationError {
109-
internal: InternalValidationError::Proposal(InternalProposalError::InvalidAddressType(
110-
value,
111-
)),
112-
}
106+
ValidationError(InternalValidationError::Proposal(
107+
InternalProposalError::InvalidAddressType(value),
108+
))
113109
}
114110
}
115111

116112
impl fmt::Display for ValidationError {
117113
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118114
use InternalValidationError::*;
119115

120-
match &self.internal {
116+
match &self.0 {
121117
Parse => write!(f, "couldn't decode as PSBT or JSON",),
122118
Io(e) => write!(f, "couldn't read PSBT: {}", e),
123119
Proposal(e) => write!(f, "proposal PSBT error: {}", e),
@@ -131,7 +127,7 @@ impl std::error::Error for ValidationError {
131127
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
132128
use InternalValidationError::*;
133129

134-
match &self.internal {
130+
match &self.0 {
135131
Parse => None,
136132
Io(error) => Some(error),
137133
Proposal(e) => Some(e),
@@ -329,16 +325,12 @@ impl From<WellKnownError> for ResponseError {
329325
}
330326

331327
impl From<InternalValidationError> for ResponseError {
332-
fn from(value: InternalValidationError) -> Self {
333-
Self::Validation(ValidationError { internal: value })
334-
}
328+
fn from(value: InternalValidationError) -> Self { Self::Validation(ValidationError(value)) }
335329
}
336330

337331
impl From<InternalProposalError> for ResponseError {
338332
fn from(value: InternalProposalError) -> Self {
339-
ResponseError::Validation(ValidationError {
340-
internal: InternalValidationError::Proposal(value),
341-
})
333+
ResponseError::Validation(ValidationError(InternalValidationError::Proposal(value)))
342334
}
343335
}
344336

payjoin/src/send/v2/error.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ impl From<ParseReceiverPubkeyParamError> for CreateRequestError {
6363

6464
/// Error returned for v2-specific payload encapsulation errors.
6565
#[derive(Debug)]
66-
pub struct EncapsulationError {
67-
internal: InternalEncapsulationError,
68-
}
66+
pub struct EncapsulationError(InternalEncapsulationError);
6967

7068
#[derive(Debug)]
7169
pub(crate) enum InternalEncapsulationError {
@@ -83,7 +81,7 @@ impl fmt::Display for EncapsulationError {
8381
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8482
use InternalEncapsulationError::*;
8583

86-
match &self.internal {
84+
match &self.0 {
8785
InvalidSize(size) => write!(f, "invalid size: {}", size),
8886
UnexpectedStatusCode(status) => write!(f, "unexpected status code: {}", status),
8987
Ohttp(error) => write!(f, "OHTTP encapsulation error: {}", error),
@@ -96,7 +94,7 @@ impl std::error::Error for EncapsulationError {
9694
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
9795
use InternalEncapsulationError::*;
9896

99-
match &self.internal {
97+
match &self.0 {
10098
InvalidSize(_) => None,
10199
UnexpectedStatusCode(_) => None,
102100
Ohttp(error) => Some(error),
@@ -106,7 +104,7 @@ impl std::error::Error for EncapsulationError {
106104
}
107105

108106
impl From<InternalEncapsulationError> for EncapsulationError {
109-
fn from(value: InternalEncapsulationError) -> Self { EncapsulationError { internal: value } }
107+
fn from(value: InternalEncapsulationError) -> Self { EncapsulationError(value) }
110108
}
111109

112110
impl From<InternalEncapsulationError> for super::ResponseError {

0 commit comments

Comments
 (0)