Skip to content

Commit

Permalink
review fix: refactor read_file_names_with_extension and `read_file_…
Browse files Browse the repository at this point in the history
…stems_with_extension` into a single `list_files_by_extension` function
  • Loading branch information
shamardy committed Sep 19, 2024
1 parent 874b294 commit bea5479
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
29 changes: 16 additions & 13 deletions mm2src/mm2_io/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ where
json::from_slice(&content).map_to_mm(FsJsonError::Deserializing)
}

async fn filter_files_with_extension(dir_path: &Path, extension: &str) -> IoResult<Vec<PathBuf>> {
async fn filter_files_by_extension(dir_path: &Path, extension: &str) -> IoResult<Vec<PathBuf>> {
let ext = Some(OsStr::new(extension).to_ascii_lowercase());
let entries = read_dir_async(dir_path)
.await?
Expand All @@ -213,17 +213,20 @@ where
.collect()
}

/// Reads the file names with the specified extension from the given directory path.
pub async fn read_file_names_with_extension(dir_path: &Path, extension: &str) -> IoResult<Vec<String>> {
let entries = filter_files_with_extension(dir_path, extension).await?;
Ok(extract_file_identifiers(entries, Path::file_name))
}

/// Reads the file stems with the specified extension from the given directory path.
/// The stem is the file name without the extension.
pub async fn read_file_stems_with_extension(dir_path: &Path, extension: &str) -> IoResult<Vec<String>> {
let entries = filter_files_with_extension(dir_path, extension).await?;
Ok(extract_file_identifiers(entries, Path::file_stem))
/// Lists files by the specified extension from the given directory path.
/// If include_extension is true, returns full file names; otherwise, returns file stems.
pub async fn list_files_by_extension(
dir_path: &Path,
extension: &str,
include_extension: bool,
) -> IoResult<Vec<String>> {
let entries = filter_files_by_extension(dir_path, extension).await?;
let extractor = if include_extension {
Path::file_name
} else {
Path::file_stem
};
Ok(extract_file_identifiers(entries, extractor))
}

/// Read the `dir_path` entries trying to deserialize each as the `T` type,
Expand All @@ -233,7 +236,7 @@ pub async fn read_files_with_extension<T>(dir_path: &Path, extension: &str) -> F
where
T: DeserializeOwned,
{
let entries = filter_files_with_extension(dir_path, extension)
let entries = filter_files_by_extension(dir_path, extension)
.await
.mm_err(FsJsonError::IoReading)?;
let type_name = std::any::type_name::<T>();
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_wallet/mnemonics_storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crypto::EncryptedData;
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use mm2_io::fs::{ensure_file_is_writable, read_file_stems_with_extension};
use mm2_io::fs::{ensure_file_is_writable, list_files_by_extension};

type WalletsStorageResult<T> = Result<T, MmError<WalletsStorageError>>;

Expand Down Expand Up @@ -63,7 +63,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>> {
let wallet_names = read_file_stems_with_extension(&ctx.db_root(), "dat")
let wallet_names = list_files_by_extension(&ctx.db_root(), "dat", false)
.await
.mm_err(|e| WalletsStorageError::FsReadError(format!("Error reading wallets directory: {}", e)))?;
Ok(wallet_names)
Expand Down

0 comments on commit bea5479

Please sign in to comment.