forked from payjoin/rust-payjoin
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse optional parameters Params from query
- Loading branch information
Showing
6 changed files
with
145 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::borrow::Borrow; | ||
use std::fmt; | ||
|
||
use crate::fee_rate::FeeRate; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct Params { | ||
// version | ||
// v: usize, | ||
// disableoutputsubstitution | ||
pub disable_output_substitution: bool, | ||
// maxadditionalfeecontribution, additionalfeeoutputindex | ||
pub additional_fee_contribution: Option<(bitcoin::Amount, usize)>, | ||
// minfeerate | ||
pub min_feerate: FeeRate, | ||
} | ||
|
||
impl Default for Params { | ||
fn default() -> Self { | ||
Params { | ||
disable_output_substitution: false, | ||
additional_fee_contribution: None, | ||
min_feerate: FeeRate::ZERO, | ||
} | ||
} | ||
} | ||
|
||
impl Params { | ||
#[cfg(feature = "receiver")] | ||
pub fn from_query_pairs<K, V, I>(pairs: I) -> Result<Self, ParamsError> | ||
where | ||
I: Iterator<Item = (K, V)>, | ||
K: Borrow<str> + Into<String>, | ||
V: Borrow<str> + Into<String>, | ||
{ | ||
let mut params = Params::default(); | ||
|
||
let mut additional_fee_output_index = None; | ||
let mut max_additional_fee_contribution = None; | ||
|
||
for (k, v) in pairs { | ||
match (k.borrow(), v.borrow()) { | ||
("v", v) => if v != "1" { | ||
return Err(ParamsError::UnknownVersion) | ||
}, | ||
("additionalfeeoutputindex", index) => { | ||
if let Ok(index) = index.parse::<usize>() { | ||
additional_fee_output_index = Some(index); | ||
} | ||
}, | ||
("maxadditionalfeecontribution", fee) => { | ||
max_additional_fee_contribution = bitcoin::Amount::from_str_in(&fee, bitcoin::Denomination::Bitcoin).ok(); | ||
} | ||
("minfeerate", feerate) => { | ||
params.min_feerate = match feerate.parse::<u64>() { | ||
Ok(rate) => FeeRate::from_sat_per_vb(rate), | ||
Err(e) => return Err(ParamsError::FeeRate(e)), | ||
} | ||
} | ||
("disableoutputsubstitution", v) => params.disable_output_substitution = v == "true", // existance is truthy | ||
_ => (), | ||
} | ||
} | ||
if let (Some(amount), Some(index)) = (max_additional_fee_contribution, additional_fee_output_index) { | ||
params.additional_fee_contribution = Some((amount, index)); | ||
} | ||
|
||
Ok(params) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum ParamsError { | ||
UnknownVersion, | ||
FeeRate(std::num::ParseIntError), | ||
} | ||
|
||
impl fmt::Display for ParamsError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
ParamsError::UnknownVersion => write!(f, "unknown version"), | ||
ParamsError::FeeRate(_) => write!(f, "could not parse feerate"), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for ParamsError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
match self { | ||
ParamsError::FeeRate(error) => Some(error), | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::collections::HashMap; | ||
|
||
pub struct MockHeaders(HashMap<String, String>); | ||
|
||
impl crate::receiver::Headers for MockHeaders { | ||
fn get_header(&self, key: &str) -> Option<&str> { | ||
self.0.get(key).map(|e| e.as_str()) | ||
} | ||
} | ||
|
||
impl MockHeaders { | ||
pub fn from_slice(body: &[u8]) -> MockHeaders { | ||
let mut h = HashMap::new(); | ||
h.insert("content-type".to_string(), "text/plain".to_string()); | ||
h.insert("content-length".to_string(), body.len().to_string()); | ||
MockHeaders(h) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters