rust fs::create_dir_all()不会抛出错误,但也不起作用

gcuhipw9  于 2023-05-22  发布在  其他
关注(0)|答案(1)|浏览(226)

我是Rust的新手,所以也许我没有正确地理解这一点。我正在使用Tauri + Yew创建一个多平台应用程序。在我的后端代码中,当我设置应用程序时,我想在$DOCUMENT目录中创建一个目录,用于在我的应用程序中创建的项目。
fs::create_dir_all("$DOCUMENT/QueryIOProjects");的结果是Ok(),它打印Projects Directory created at /Users/username/Documents/QueryIOProjects,但我在Finder或通过终端看不到任何文件夹。为了调试,我使用read_dir方法打印所有的DirEntry,这只打印出一个输出dirEntry = QueryIOProjects,这让我更加困惑。

src-tauri/src/main.rs:

tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .menu(menu)
        .on_menu_event(menu_item_handler)
        .setup(|app| {
            let path_str = match path::parse(
                &app.config(),
                app.package_info(),
                &app.env(),
                "$DOCUMENT/QueryIOProjects",
            ) {
                Ok(path) => path.to_string_lossy().to_string(),
                Err(_) => "Path creation failed".to_string(),
            };
            {
                let result = fs::create_dir_all("$DOCUMENT/QueryIOProjects");
                match result {
                    Ok(_) => println!("Projects Directory created at {}", path_str),
                    Err(_) => println!("Failed to create project directory at {}", path_str),
                }
            }
            {
                let result = fs::read_dir("$DOCUMENT/");
                match result {
                    Ok(readDir) => {
                        readDir.for_each(|dirEntryResult| match dirEntryResult {
                            Ok(dirEntry) => println!(
                                "dirEntry = {}",
                                dirEntry
                                    .file_name()
                                    .into_string()
                                    .expect("dir entry string conversion failed")
                            ),
                            Err(err) => println!("error in dirEntry"),
                        });
                    }
                    Err(_) => println!("Failed to read directory at {}", "$DOCUMENT/"),
                }
            }
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");

tauri.conf.json:

{
  "tauri": {
    "allowlist": {
      "all": false,
      "shell": {
        "all": false,
        "open": true
      },
      "fs": {
        "scope": [
          "$DOCUMENT",
          "$DOCUMENT/*"
        ],
        "all": true,
        "readFile": true,
        "writeFile": true,
        "readDir": true,
        "copyFile": true,
        "createDir": true,
        "removeFile": true,
        "renameFile": true,
        "exists": true
      },
      "path": {
        "all": true
      },
      "dialog": {
        "open": true,
        "save": true
      }
    }
  }
}
0dxa2lsx

0dxa2lsx1#

您正在文字路径"$DOCUMENT/QueryIOProjects"而不是path_str处创建文件夹。在你的电脑上有一个文件夹,字面意思是$DOCUMENT

相关问题