Some simple Option / Result / iterator pattern simplifications (#3205)

This commit is contained in:
François Garillot 2020-01-29 09:20:57 -05:00 committed by GitHub
parent 616dad43fd
commit dcdbdd4bcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 39 additions and 57 deletions

View file

@ -463,10 +463,7 @@ impl KernelHandler {
height,
mmr_index,
});
match kernel {
Some(kernel) => Ok(kernel),
None => Err(ErrorKind::NotFound.into()),
}
kernel.ok_or_else(|| ErrorKind::NotFound.into())
}
}

View file

@ -334,10 +334,10 @@ impl OutputPrintable {
}
pub fn range_proof(&self) -> Result<pedersen::RangeProof, ser::Error> {
let proof_str = match self.proof.clone() {
Some(p) => p,
None => return Err(ser::Error::HexError(format!("output range_proof missing"))),
};
let proof_str = self
.proof
.clone()
.ok_or_else(|| ser::Error::HexError(format!("output range_proof missing")))?;
let p_vec = util::from_hex(proof_str)
.map_err(|_| ser::Error::HexError(format!("invalid output range_proof")))?;

View file

@ -105,10 +105,7 @@ impl QueryParams {
}
pub fn get(&self, name: &str) -> Option<&String> {
match self.params.get(name) {
None => None,
Some(v) => v.first(),
}
self.params.get(name).and_then(|v| v.first())
}
}

View file

@ -65,10 +65,7 @@ pub fn verify(
extra_data: Option<Vec<u8>>,
) -> Result<(), secp::Error> {
let result = secp.verify_bullet_proof(commit, proof, extra_data);
match result {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
result.map(|_| ())
}
/// Rewind a rangeproof to retrieve the amount, derivation path and switch commitment type
@ -228,9 +225,10 @@ where
let id = Identifier::from_serialized_path(depth, &msg[4..]);
let commit_exp = self.keychain.commit(amount, &id, &switch)?;
match commit == &commit_exp {
true => Ok(Some((id, switch))),
false => Ok(None),
if commit == &commit_exp {
Ok(Some((id, switch)))
} else {
Ok(None)
}
}
}
@ -338,9 +336,10 @@ where
let commit_exp = self
.keychain
.commit(amount, &id, &SwitchCommitmentType::Regular)?;
match commit == &commit_exp {
true => Ok(Some((id, SwitchCommitmentType::Regular))),
false => Ok(None),
if commit == &commit_exp {
Ok(Some((id, SwitchCommitmentType::Regular)))
} else {
Ok(None)
}
}
}

View file

@ -107,15 +107,17 @@ impl BIP32GrinHasher {
impl BIP32Hasher for BIP32GrinHasher {
fn network_priv(&self) -> [u8; 4] {
match self.is_floo {
true => [0x03, 0x27, 0x3A, 0x10], // fprv
false => [0x03, 0x3C, 0x04, 0xA4], // gprv
if self.is_floo {
[0x03, 0x27, 0x3A, 0x10]
} else {
[0x03, 0x3C, 0x04, 0xA4]
}
}
fn network_pub(&self) -> [u8; 4] {
match self.is_floo {
true => [0x03, 0x27, 0x3E, 0x4B], // fpub
false => [0x03, 0x3C, 0x08, 0xDF], // gpub
if self.is_floo {
[0x03, 0x27, 0x3E, 0x4B]
} else {
[0x03, 0x3C, 0x08, 0xDF]
}
}
fn master_seed() -> [u8; 12] {
@ -380,10 +382,7 @@ impl ExtendedPrivKey {
passphrase: &str,
is_floo: bool,
) -> Result<ExtendedPrivKey, Error> {
let seed = match mnemonic::to_seed(mnemonic, passphrase) {
Ok(s) => s,
Err(e) => return Err(Error::MnemonicError(e)),
};
let seed = mnemonic::to_seed(mnemonic, passphrase).map_err(|e| Error::MnemonicError(e))?;
let mut hasher = BIP32GrinHasher::new(is_floo);
let key = ExtendedPrivKey::new_master(secp, &mut hasher, &seed)?;
Ok(key)

View file

@ -403,11 +403,7 @@ impl Pool {
// Oldest (based on pool insertion time) will then be prioritized.
tx_buckets.sort_unstable_by_key(|x| (Reverse(x.fee_to_weight), x.age_idx));
tx_buckets
.into_iter()
.map(|x| x.raw_txs)
.flatten()
.collect()
tx_buckets.into_iter().flat_map(|x| x.raw_txs).collect()
}
pub fn find_matching_transactions(&self, kernels: &[TxKernel]) -> Vec<Transaction> {

View file

@ -130,9 +130,10 @@ impl TableViewItem<DiffColumn> for DiffBlock {
fn to_column(&self, column: DiffColumn) -> String {
let naive_datetime = NaiveDateTime::from_timestamp(self.time as i64, 0);
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc);
let pow_type = match self.is_secondary {
true => String::from("Secondary"),
false => String::from("Primary"),
let pow_type = if self.is_secondary {
String::from("Secondary")
} else {
String::from("Primary")
};
match column {

View file

@ -96,8 +96,7 @@ impl LeafSet {
/// Only applicable for the output MMR.
fn unpruned_pre_cutoff(&self, cutoff_pos: u64, prune_list: &PruneList) -> Bitmap {
(1..=cutoff_pos)
.filter(|&x| pmmr::is_leaf(x))
.filter(|&x| !prune_list.is_pruned(x))
.filter(|&x| pmmr::is_leaf(x) && !prune_list.is_pruned(x))
.map(|x| x as u32)
.collect()
}

View file

@ -107,10 +107,7 @@ where
/// Elements can be of variable size (handled internally in the append-only file impl).
///
pub fn read(&self, position: u64) -> Option<T> {
match self.file.read_as_elmt(position - 1) {
Ok(x) => Some(x),
Err(_) => None,
}
self.file.read_as_elmt(position - 1).ok()
}
/// Rewind the backend file to the specified position.

View file

@ -70,15 +70,12 @@ fn copy_to(src: &Path, src_type: &fs::FileType, dst: &Path) -> io::Result<u64> {
/// Retrieve first line from file
pub fn get_first_line(file_path: Option<String>) -> Option<String> {
match file_path {
Some(path) => match fs::File::open(path) {
file_path.and_then(|path| match fs::File::open(path) {
Ok(file) => {
let buf_reader = io::BufReader::new(file);
let mut lines_iter = buf_reader.lines().map(|l| l.unwrap());
lines_iter.next()
}
Err(_) => None,
},
None => None,
}
})
}