fs::create_dir_all() doesn't throw an error, but also does not work

huangapple go评论65阅读模式
英文:

fs::create_dir_all() doesn't throw an error, but also does not work

问题

fs::create_dir_all("$DOCUMENT/QueryIOProjects"); 的结果是 Ok(),并且打印输出 Projects Directory created at /Users/username/Documents/QueryIOProjects,但我无法在Finder或终端中看到任何文件夹。为了调试,我使用了read_dir方法来打印所有的DirEntry,它只输出了一个结果 dirEntry = QueryIOProjects,这让我更加困惑。

    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": {
    "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
      }
    }
  }
}
英文:

I'm new to Rust, so maybe I'm not understanding this correctly. I am creating a multiplatform application using Tauri + Yew. In my backend code when I'm setting up the app, I wanted to create a directory in the $DOCUMENT directory, for projects made inside my application.

The result for fs::create_dir_all("$DOCUMENT/QueryIOProjects"); is Ok() and it prints Projects Directory created at /Users/username/Documents/QueryIOProjects, but I cant see any folder in Finder or via terminal. To debug, I used the read_dir method to print all the DirEntrys,which prints out only one output dirEntry = QueryIOProjects, leaving me even more confused.

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
      }
    }
  }
}

答案1

得分: 2

你正在创建文件夹,位于字面路径"$DOCUMENT/QueryIOProjects",而不是path_str。在你的计算机上有一个名为$DOCUMENT的文件夹,字面上就是这个名字。

英文:

You are creating the folder at the literal path "$DOCUMENT/QueryIOProjects", not path_str. Somewhere on your computer is a folder called, literally, $DOCUMENT.

huangapple
  • 本文由 发表于 2023年5月22日 02:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定