From be7df15881124272a47d5f6e020da80323ba29c4 Mon Sep 17 00:00:00 2001 From: DanGould Date: Mon, 6 Jan 2025 22:26:44 -0500 Subject: [PATCH] Encapsulate internal error variants in tuple No need for them to be named --- payjoin/src/send/error.rs | 26 +++++++++----------------- payjoin/src/send/v2/error.rs | 10 ++++------ 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/payjoin/src/send/error.rs b/payjoin/src/send/error.rs index f6a16e14..ae68c490 100644 --- a/payjoin/src/send/error.rs +++ b/payjoin/src/send/error.rs @@ -86,9 +86,7 @@ impl std::error::Error for BuildSenderError { /// This is currently opaque type because we aren't sure which variants will stay. /// You can only display it. #[derive(Debug)] -pub struct ValidationError { - internal: InternalValidationError, -} +pub struct ValidationError(InternalValidationError); #[derive(Debug)] pub(crate) enum InternalValidationError { @@ -100,16 +98,14 @@ pub(crate) enum InternalValidationError { } impl From for ValidationError { - fn from(value: InternalValidationError) -> Self { ValidationError { internal: value } } + fn from(value: InternalValidationError) -> Self { ValidationError(value) } } impl From for ValidationError { fn from(value: crate::psbt::AddressTypeError) -> Self { - ValidationError { - internal: InternalValidationError::Proposal(InternalProposalError::InvalidAddressType( - value, - )), - } + ValidationError(InternalValidationError::Proposal( + InternalProposalError::InvalidAddressType(value), + )) } } @@ -117,7 +113,7 @@ impl fmt::Display for ValidationError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use InternalValidationError::*; - match &self.internal { + match &self.0 { Parse => write!(f, "couldn't decode as PSBT or JSON",), Io(e) => write!(f, "couldn't read PSBT: {}", e), Proposal(e) => write!(f, "proposal PSBT error: {}", e), @@ -131,7 +127,7 @@ impl std::error::Error for ValidationError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { use InternalValidationError::*; - match &self.internal { + match &self.0 { Parse => None, Io(error) => Some(error), Proposal(e) => Some(e), @@ -329,16 +325,12 @@ impl From for ResponseError { } impl From for ResponseError { - fn from(value: InternalValidationError) -> Self { - Self::Validation(ValidationError { internal: value }) - } + fn from(value: InternalValidationError) -> Self { Self::Validation(ValidationError(value)) } } impl From for ResponseError { fn from(value: InternalProposalError) -> Self { - ResponseError::Validation(ValidationError { - internal: InternalValidationError::Proposal(value), - }) + ResponseError::Validation(ValidationError(InternalValidationError::Proposal(value))) } } diff --git a/payjoin/src/send/v2/error.rs b/payjoin/src/send/v2/error.rs index 002a605f..663b6909 100644 --- a/payjoin/src/send/v2/error.rs +++ b/payjoin/src/send/v2/error.rs @@ -63,9 +63,7 @@ impl From for CreateRequestError { /// Error returned for v2-specific payload encapsulation errors. #[derive(Debug)] -pub struct EncapsulationError { - internal: InternalEncapsulationError, -} +pub struct EncapsulationError(InternalEncapsulationError); #[derive(Debug)] pub(crate) enum InternalEncapsulationError { @@ -83,7 +81,7 @@ impl fmt::Display for EncapsulationError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use InternalEncapsulationError::*; - match &self.internal { + match &self.0 { InvalidSize(size) => write!(f, "invalid size: {}", size), UnexpectedStatusCode(status) => write!(f, "unexpected status code: {}", status), Ohttp(error) => write!(f, "OHTTP encapsulation error: {}", error), @@ -96,7 +94,7 @@ impl std::error::Error for EncapsulationError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { use InternalEncapsulationError::*; - match &self.internal { + match &self.0 { InvalidSize(_) => None, UnexpectedStatusCode(_) => None, Ohttp(error) => Some(error), @@ -106,7 +104,7 @@ impl std::error::Error for EncapsulationError { } impl From for EncapsulationError { - fn from(value: InternalEncapsulationError) -> Self { EncapsulationError { internal: value } } + fn from(value: InternalEncapsulationError) -> Self { EncapsulationError(value) } } impl From for super::ResponseError {