Skip to content

Commit d15d695

Browse files
committed
Make CreateRequestError v2-only
Its variants only apply to extracting v2 requests.
1 parent 30c2a07 commit d15d695

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

payjoin/src/send/error.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -90,64 +90,54 @@ impl std::error::Error for BuildSenderError {
9090
/// `unwrap()`ing it is thus considered OK in Rust but you may achieve nicer message by displaying
9191
/// it.
9292
#[derive(Debug)]
93+
#[cfg(feature = "v2")]
9394
pub struct CreateRequestError(InternalCreateRequestError);
9495

9596
#[derive(Debug)]
97+
#[cfg(feature = "v2")]
9698
pub(crate) enum InternalCreateRequestError {
9799
Url(url::ParseError),
98-
#[cfg(feature = "v2")]
99100
Hpke(crate::hpke::HpkeError),
100-
#[cfg(feature = "v2")]
101101
OhttpEncapsulation(crate::ohttp::OhttpEncapsulationError),
102-
#[cfg(feature = "v2")]
103102
ParseReceiverPubkey(ParseReceiverPubkeyParamError),
104-
#[cfg(feature = "v2")]
105103
MissingOhttpConfig,
106-
#[cfg(feature = "v2")]
107104
Expired(std::time::SystemTime),
108105
}
109106

107+
#[cfg(feature = "v2")]
110108
impl fmt::Display for CreateRequestError {
111109
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112110
use InternalCreateRequestError::*;
113111

114112
match &self.0 {
115113
Url(e) => write!(f, "cannot parse url: {:#?}", e),
116-
#[cfg(feature = "v2")]
117114
Hpke(e) => write!(f, "v2 error: {}", e),
118-
#[cfg(feature = "v2")]
119115
OhttpEncapsulation(e) => write!(f, "v2 error: {}", e),
120-
#[cfg(feature = "v2")]
121116
ParseReceiverPubkey(e) => write!(f, "cannot parse receiver public key: {}", e),
122-
#[cfg(feature = "v2")]
123117
MissingOhttpConfig =>
124118
write!(f, "no ohttp configuration with which to make a v2 request available"),
125-
#[cfg(feature = "v2")]
126119
Expired(expiry) => write!(f, "session expired at {:?}", expiry),
127120
}
128121
}
129122
}
130123

124+
#[cfg(feature = "v2")]
131125
impl std::error::Error for CreateRequestError {
132126
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
133127
use InternalCreateRequestError::*;
134128

135129
match &self.0 {
136130
Url(error) => Some(error),
137-
#[cfg(feature = "v2")]
138131
Hpke(error) => Some(error),
139-
#[cfg(feature = "v2")]
140132
OhttpEncapsulation(error) => Some(error),
141-
#[cfg(feature = "v2")]
142133
ParseReceiverPubkey(error) => Some(error),
143-
#[cfg(feature = "v2")]
144134
MissingOhttpConfig => None,
145-
#[cfg(feature = "v2")]
146135
Expired(_) => None,
147136
}
148137
}
149138
}
150139

140+
#[cfg(feature = "v2")]
151141
impl From<InternalCreateRequestError> for CreateRequestError {
152142
fn from(value: InternalCreateRequestError) -> Self { CreateRequestError(value) }
153143
}

payjoin/src/send/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ use std::str::FromStr;
2727
use bitcoin::hashes::{sha256, Hash};
2828
use bitcoin::psbt::Psbt;
2929
use bitcoin::{Amount, FeeRate, Script, ScriptBuf, TxOut, Weight};
30-
pub use error::{BuildSenderError, CreateRequestError, ResponseError, ValidationError};
31-
pub(crate) use error::{
32-
InternalBuildSenderError, InternalCreateRequestError, InternalValidationError,
33-
};
30+
#[cfg(feature = "v2")]
31+
pub use error::CreateRequestError;
32+
#[cfg(feature = "v2")]
33+
pub(crate) use error::InternalCreateRequestError;
34+
pub use error::{BuildSenderError, ResponseError, ValidationError};
35+
pub(crate) use error::{InternalBuildSenderError, InternalValidationError};
3436
#[cfg(feature = "v2")]
3537
use serde::{Deserialize, Serialize};
3638
use url::Url;
@@ -255,15 +257,14 @@ pub struct Sender {
255257

256258
impl Sender {
257259
/// Extract serialized V1 Request and Context from a Payjoin Proposal
258-
pub fn extract_v1(&self) -> Result<(Request, V1Context), CreateRequestError> {
260+
pub fn extract_v1(&self) -> Result<(Request, V1Context), url::ParseError> {
259261
let url = serialize_url(
260262
self.endpoint.clone(),
261263
self.disable_output_substitution,
262264
self.fee_contribution,
263265
self.min_fee_rate,
264266
"1", // payjoin version
265-
)
266-
.map_err(InternalCreateRequestError::Url)?;
267+
)?;
267268
let body = self.psbt.to_string().as_bytes().to_vec();
268269
Ok((
269270
Request::new_v1(url, body),

0 commit comments

Comments
 (0)