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