Skip to content
Snippets Groups Projects
Commit e452176c authored by Éloïs's avatar Éloïs
Browse files

[ref] rustify: replace some if let by chain code

use Option::flatten (need rustc >= 1.40)
parent 7cdbf048
Branches
No related tags found
1 merge request!247[ref] rustify: replace some if let by chain code
......@@ -5,7 +5,7 @@
[![Latest Version](https://img.shields.io/badge/latest-v0.1.1--a1-orange.svg)](https://git.duniter.org/nodes/rust/duniter-rs/tags/v0.1.1-a1)
[![docs](https://librelois.fr/img/docs-read%20now-green.svg)](https://nodes.duniter.io/rust/duniter-rs/dunitrust/)
[![build status](https://git.duniter.org/nodes/rust/duniter-rs/badges/dev/build.svg)](https://git.duniter.org/nodes/rust/duniter-rs/commits/dev)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.37.0+-yellow.svg)](https://github.com/rust-lang/rust/blob/master/RELEASES.md)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.40.0+-yellow.svg)](https://github.com/rust-lang/rust/blob/master/RELEASES.md)
## What is Duniter
......
......@@ -73,11 +73,9 @@ pub fn parse_json_block(json_block: &JSONValue<DefaultHasher>) -> Result<BlockDo
"signature",
)?)?)],
hash: Some(BlockHash(Hash::from_hex(get_str(json_block, "hash")?)?)),
parameters: if let Some(params) = get_optional_str_not_empty(json_block, "parameters")? {
Some(BlockV10Parameters::from_str(params)?)
} else {
None
},
parameters: get_optional_str_not_empty(json_block, "parameters")?
.map(BlockV10Parameters::from_str)
.transpose()?,
previous_hash: if block_number == 0 {
None
} else {
......
......@@ -42,11 +42,7 @@ pub fn get_currency_name(
) -> Result<Option<CurrencyName>, CurrencyParamsDbError> {
let db_datas: CurrencyParamsDbDatas = read_currency_params_db(datas_path)?;
if let Some((currency_name, _genesis_block_params)) = db_datas {
Ok(Some(currency_name))
} else {
Ok(None)
}
Ok(db_datas.map(|(currency_name, _genesis_block_params)| currency_name))
}
/// Get currency parameters
......@@ -55,16 +51,14 @@ pub fn get_currency_params(
) -> Result<Option<(CurrencyName, CurrencyParameters)>, CurrencyParamsDbError> {
let db_datas: CurrencyParamsDbDatas = read_currency_params_db(datas_path)?;
if let Some((currency_name, genesis_block_params)) = db_datas {
Ok(db_datas.map(|(currency_name, genesis_block_params)| {
let currency_params = match genesis_block_params {
GenesisBlockParams::V10(genesis_block_v10_params) => {
CurrencyParameters::from((&currency_name, genesis_block_v10_params))
}
};
Ok(Some((currency_name, currency_params)))
} else {
Ok(None)
}
(currency_name, currency_params)
}))
}
fn read_currency_params_db(
......
......@@ -67,11 +67,9 @@ pub fn parse_json_transaction(
.map(|i| TransactionOutput::from_str(i))
.collect::<Result<Vec<TransactionOutput>, TextDocumentParseError>>()?,
comment: &durs_common_tools::fns::str_escape::unescape_str(get_str(json_tx, "comment")?),
hash: if let Some(hash_str) = get_optional_str(json_tx, "hash")? {
Some(Hash::from_hex(hash_str)?)
} else {
None
},
hash: get_optional_str(json_tx, "hash")?
.map(Hash::from_hex)
.transpose()?,
};
Ok(tx_doc_builder.build_with_signature(
......
......@@ -129,13 +129,10 @@ pub fn get_block_hash<DB: BcDbInReadTx>(
db: &DB,
block_number: BlockNumber,
) -> Result<Option<BlockHash>, DbError> {
Ok(
if let Some(block) = get_block_in_local_blockchain(db, block_number)? {
block.hash()
} else {
None
},
)
Ok(get_block_in_local_blockchain(db, block_number)?
.as_ref()
.map(BlockDocument::hash)
.flatten())
}
/// Get block in local blockchain
......
......@@ -199,11 +199,7 @@ impl ForkTree {
/// Get main branch node
#[inline]
pub fn get_main_branch_node_id(&self, block_id: BlockNumber) -> Option<TreeNodeId> {
if let Some(node_id) = self.main_branch.get(&block_id) {
Some(*node_id)
} else {
None
}
self.main_branch.get(&block_id).copied()
}
/// Get main branch block hash
#[inline]
......
......@@ -80,39 +80,33 @@ pub fn get_db_version<DB: DbReadable>(db: &DB) -> Result<usize, DbError> {
/// Get currency name
pub fn get_currency_name<DB: BcDbInReadTx>(db: &DB) -> Result<Option<CurrencyName>, DbError> {
if let Some(v) = db
.db()
db.db()
.get_int_store(CURRENT_METADATA)
.get(db.r(), CurrentMetaDataKey::CurrencyName.to_u32())?
{
.map(|v| {
if let DbValue::Str(curency_name) = v {
Ok(Some(CurrencyName(curency_name.to_owned())))
Ok(CurrencyName(curency_name.to_owned()))
} else {
Err(DbError::DBCorrupted)
}
} else {
Ok(None)
}
})
.transpose()
}
/// Get current blockstamp
pub fn get_current_blockstamp<DB: BcDbInReadTx>(db: &DB) -> Result<Option<Blockstamp>, DbError> {
if let Some(v) = db
.db()
db.db()
.get_int_store(CURRENT_METADATA)
.get(db.r(), CurrentMetaDataKey::CurrentBlockstamp.to_u32())?
{
.map(|v| {
if let DbValue::Blob(current_blockstamp_bytes) = v {
Ok(Some(
Blockstamp::from_bytes(current_blockstamp_bytes)
.map_err(|_| DbError::DBCorrupted)?,
))
Ok(Blockstamp::from_bytes(current_blockstamp_bytes)
.map_err(|_| DbError::DBCorrupted)?)
} else {
Err(DbError::DBCorrupted)
}
} else {
Ok(None)
}
})
.transpose()
}
/// Get current common time (also named "blockchain time")
pub fn get_current_common_time_<DB: BcDbInReadTx>(db: &DB) -> Result<u64, DbError> {
......@@ -164,13 +158,12 @@ pub fn get_greatest_wot_id_<DB: BcDbInReadTx>(db: &DB) -> Result<WotId, DbError>
/// Get current UD
pub fn get_current_ud<DB: BcDbInReadTx>(db: &DB) -> Result<Option<CurrentUdDb>, DbError> {
if let Some(v) = db
Ok(db
.db()
.get_int_store(CURRENT_METADATA)
.get(db.r(), CurrentMetaDataKey::CurrentUd.to_u32())?
{
Ok(from_db_value::<CurrentUdDbInternal>(v)?.into())
} else {
Ok(None)
}
.map(from_db_value::<CurrentUdDbInternal>)
.transpose()?
.map(Into::into)
.flatten())
}
......@@ -140,11 +140,10 @@ pub fn get_identity_by_pubkey<DB: BcDbInReadTx>(
db: &DB,
pubkey: &PubKey,
) -> Result<Option<IdentityDb>, DbError> {
if let Some(wot_id) = get_wot_id(db, pubkey)? {
get_identity_by_wot_id(db, wot_id)
} else {
Ok(None)
}
Ok(get_wot_id(db, pubkey)?
.map(|wot_id| get_identity_by_wot_id(db, wot_id))
.transpose()?
.flatten())
}
/// Get identity by pubkey
......
......@@ -131,11 +131,11 @@ pub fn get_utxo_v10<DB: BcDbInReadTx>(
utxo_id: UniqueIdUTXOv10,
) -> Result<Option<TransactionOutput>, DbError> {
let utxo_id_bytes: Vec<u8> = utxo_id.into();
if let Some(v) = db.db().get_store(UTXOS).get(db.r(), &utxo_id_bytes)? {
Ok(Some(from_db_value(v)?))
} else {
Ok(None)
}
db.db()
.get_store(UTXOS)
.get(db.r(), &utxo_id_bytes)?
.map(from_db_value)
.transpose()
}
/// Get block consumed sources
......@@ -143,13 +143,9 @@ pub fn get_block_consumed_sources_<DB: BcDbInReadTx>(
db: &DB,
block_number: BlockNumber,
) -> Result<Option<HashMap<UniqueIdUTXOv10, TransactionOutput>>, DbError> {
if let Some(v) = db
.db()
db.db()
.get_int_store(CONSUMED_UTXOS)
.get(db.r(), block_number.0)?
{
Ok(Some(from_db_value(v)?))
} else {
Ok(None)
}
.map(from_db_value)
.transpose()
}
......@@ -91,13 +91,10 @@ impl WS2POutgoingOrchestrator {
pub fn connect_to_ws2p_v2_endpoint(
&self,
endpoint: &EndpointEnum,
remote_node_id: Option<NodeId>,
remote_node_id_opt: Option<NodeId>,
) -> Result<(), WsError> {
let expected_remote_full_id = if let Some(remote_node_id) = remote_node_id {
Some(NodeFullId(remote_node_id, endpoint.pubkey()))
} else {
None
};
let expected_remote_full_id =
remote_node_id_opt.map(|remote_node_id| NodeFullId(remote_node_id, endpoint.pubkey()));
match controllers::outgoing_connections::connect_to_ws2p_v2_endpoint(
&self.currency,
&self.sender,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment