Skip to content

Commit

Permalink
Some simple Option / Result / iterator pattern simplifications (#3205)
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker authored Jan 29, 2020
1 parent 616dad4 commit dcdbdd4
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 57 deletions.
5 changes: 1 addition & 4 deletions api/src/handlers/chain_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
8 changes: 4 additions & 4 deletions api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))?;
Expand Down
5 changes: 1 addition & 4 deletions api/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
19 changes: 9 additions & 10 deletions core/src/libtx/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions keychain/src/extkey_bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
7 changes: 4 additions & 3 deletions src/bin/tui/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions store/src/leaf_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
5 changes: 1 addition & 4 deletions store/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 8 additions & 11 deletions util/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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,
}
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,
})
}

0 comments on commit dcdbdd4

Please sign in to comment.