Skip to content

Commit fbd5ad9

Browse files
committed
🧑‍💻 rust中增加read_folder_recursive函数
1 parent 704810d commit fbd5ad9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

app/src-tauri/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tauri_plugin_updater::UpdaterExt;
1515
fn exists(path: String) -> bool {
1616
std::path::Path::new(&path).exists()
1717
}
18+
1819
/// 读取文件夹中的文件列表
1920
/// 如果文件夹不存在,返回空列表
2021
#[tauri::command]
@@ -34,6 +35,31 @@ fn read_folder(path: String) -> Vec<String> {
3435
files
3536
}
3637

38+
/// 读取一个文件夹中的全部文件,递归的读取
39+
/// 如果文件夹不存在,返回空列表
40+
/// fileExts: 要读取的文件扩展名列表,例如:[".txt", ".md"]
41+
#[tauri::command]
42+
fn read_folder_recursive(path: String, fileExts: Vec<String>) -> Vec<String> {
43+
let mut files = Vec::new();
44+
if let Ok(entries) = std::fs::read_dir(path) {
45+
for entry in entries {
46+
if let Ok(entry) = entry {
47+
if entry.path().is_file() {
48+
if let Some(file_name) = entry.file_name().to_str() {
49+
if fileExts.iter().any(|ext| file_name.ends_with(ext)) {
50+
files.push(file_name.to_string());
51+
}
52+
}
53+
} else if entry.path().is_dir() {
54+
let mut sub_files = read_folder_recursive(entry.path().to_str().unwrap().to_string(), fileExts.clone());
55+
files.append(&mut sub_files);
56+
}
57+
}
58+
}
59+
}
60+
files
61+
}
62+
3763

3864
/// 删除文件
3965
#[tauri::command]

docs-pg/REDME_FOR_AI.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
使用 monorepo 管理项目,主要分为 app(应用程序本体) 和 docs(软件官网) 两个部分。
1212

1313
使用 pnpm 作为包管理工具。
14+
15+
## 代码要求
16+
17+
rust中的函数提供给前端调用,函数在运行中绝对不能出现报错,必须要保证函数内部捕获所有可能出现的错误,函数的健壮性。否则会导致程序直接闪退

0 commit comments

Comments
 (0)