Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature/REST] Pagination by index for address|scripthash txs/chain endpoint #50

Open
wants to merge 12 commits into
base: new-index
Choose a base branch
from
Prev Previous commit
Fix: Avoid expensive from_hex if possible.
junderw committed May 30, 2023
commit 2494a028e42fdc24aa3471f02e61c1a0c87f1b4d
26 changes: 16 additions & 10 deletions src/rest.rs
Original file line number Diff line number Diff line change
@@ -615,16 +615,22 @@ impl FromStr for AddressPaginator {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Txid::from_hex(s)
.ok()
.and_then(|txid| Some(Self::Txid(txid)))
.or_else(|| {
s.parse::<usize>()
.ok()
.filter(|&skip| skip != 0) // Don't allow 0 for Skip
.and_then(|skip| Some(Self::Skip(skip)))
})
.ok_or("Invalid AddressPaginator".to_string())
// 1) Deal with Options in if else statement
// to utilize filter for usize.
// 2) 64 length usize doesn't exist,
// and from_hex is expensive.
if s.len() == 64 {
Txid::from_hex(s)
.ok()
.and_then(|txid| Some(Self::Txid(txid)))
} else {
s.parse::<usize>()
.ok()
.filter(|&skip| skip != 0) // Don't allow 0 for Skip
.and_then(|skip| Some(Self::Skip(skip)))
}
// 3) Convert the return value of the if else statement into a Result.
.ok_or("Invalid AddressPaginator".to_string())
}
}