Skip to content

Commit

Permalink
review fix: use impl iterator where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
shamardy committed Sep 23, 2024
1 parent aaf9d19 commit de645bd
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions mm2src/mm2_io/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,13 @@ async fn filter_files_by_extension(dir_path: &Path, extension: &str) -> IoResult
}

/// Helper function to extract file names or stems based on the provided extraction function.
fn extract_file_identifiers<F>(entries: Vec<PathBuf>, extractor: F) -> Vec<String>
fn extract_file_identifiers<'a, F>(entries: Vec<PathBuf>, extractor: F) -> impl Iterator<Item = String> + 'a
where
F: Fn(&Path) -> Option<&OsStr>,
F: Fn(&Path) -> Option<&OsStr> + 'a,
{
entries
.into_iter()
.filter_map(|path| extractor(&path).and_then(OsStr::to_str).map(ToOwned::to_owned))
.collect()
.filter_map(move |path| extractor(&path).and_then(OsStr::to_str).map(ToOwned::to_owned))
}

/// Lists files by the specified extension from the given directory path.
Expand All @@ -219,7 +218,7 @@ pub async fn list_files_by_extension(
dir_path: &Path,
extension: &str,
include_extension: bool,
) -> IoResult<Vec<String>> {
) -> IoResult<impl Iterator<Item = String>> {
let entries = filter_files_by_extension(dir_path, extension).await?;
let extractor = if include_extension {
Path::file_name
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ impl From<WalletsDBError> for GetWalletsError {
/// Retrieves all created wallets and the currently activated wallet.
pub async fn get_wallet_names_rpc(ctx: MmArc, _req: Json) -> MmResult<GetWalletNamesResponse, GetWalletsError> {
// We want to return wallet names in the same order for both native and wasm32 targets.
let wallets = read_all_wallet_names(&ctx).await?.into_iter().sorted().collect();
let wallets = read_all_wallet_names(&ctx).await?.sorted().collect();
// Note: `ok_or` is used here on `Constructible<Option<String>>` to handle the case where the wallet name is not set.
// `wallet_name` can be `None` in the case of no-login mode.
let activated_wallet = ctx.wallet_name.ok_or(GetWalletsError::Internal(
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_wallet/mnemonics_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub(super) async fn read_encrypted_passphrase_if_available(ctx: &MmArc) -> Walle
})
}

pub(super) async fn read_all_wallet_names(ctx: &MmArc) -> WalletsStorageResult<Vec<String>> {
pub(super) async fn read_all_wallet_names(ctx: &MmArc) -> WalletsStorageResult<impl Iterator<Item = String>> {
let wallet_names = list_files_by_extension(&ctx.db_root(), "dat", false)
.await
.mm_err(|e| WalletsStorageError::FsReadError(format!("Error reading wallets directory: {}", e)))?;
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_wallet/mnemonics_wasm_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ pub(super) async fn read_encrypted_passphrase_if_available(ctx: &MmArc) -> Walle
.transpose()
}

pub(super) async fn read_all_wallet_names(ctx: &MmArc) -> WalletsDBResult<Vec<String>> {
pub(super) async fn read_all_wallet_names(ctx: &MmArc) -> WalletsDBResult<impl Iterator<Item = String>> {
let wallets_ctx = WalletsContext::from_ctx(ctx).map_to_mm(WalletsDBError::Internal)?;

let db = wallets_ctx.wallets_db().await?;
let transaction = db.transaction().await?;
let table = transaction.table::<MnemonicsTable>().await?;

let all_items = table.get_all_items().await?;
let wallet_names = all_items.into_iter().map(|(_, item)| item.wallet_name).collect();
let wallet_names = all_items.into_iter().map(|(_, item)| item.wallet_name);

Ok(wallet_names)
}

0 comments on commit de645bd

Please sign in to comment.