This works fine when the number of items is small (less than 1000 or so) but once the number of items is larger (above several thousand) it's incredibly slow.
#[tauri::command]
async fn get_all_collection_entries(root_path: &str) -> Result<Vec<serde_json::Value>, String> {
use std::fs;
use serde_json::{json};
// https://stackoverflow.com/a/43395633/1971662
// https://stackoverflow.com/a/26084812/1971662
// https://www.dotnetperls.com/read-dir-rust
struct ResultEntry {
filepath: String,
content: String
}
let mut files = Vec::new();
for entry_raw in fs::read_dir(root_path).expect("Unable to read file") {
let entry = entry_raw.expect("error");
let entry_json: serde_json::Value = json!({
"path": entry.path().to_str().unwrap(),
"name": entry.path().file_name().expect("error").to_str(),
"ext": entry.path().extension().expect("error").to_str(),
"stem": entry.path().file_stem().expect("error").to_str(),
"content": std::fs::read_to_string(entry.path().to_str().unwrap()).expect("Unable to read file")
});
files.push(entry_json)
}
return Ok(files);
}
I'm a total Rust newbie... what am I doing wrong here? Why does this not work with a large number of files? What's a better way to approach this?