Skip to content

Create directory if not exists, and return Dir #372

Description

@ambiso

Is there a simple way to create a directory if it does not yet exist and to return the Dir?

Something along the lines of:

async fn create_dir(parent: &Dir, path: &str) -> anyhow::Result<Dir> {
    match parent.create_dir(path) {
        Ok(_) => {}
        Err(e) => match e.kind() {
            std::io::ErrorKind::AlreadyExists => {}
            _ => {
                Err(e)?;
            }
        },
    }
    Ok(parent.open_dir(path).await?) // could maybe be a TOCTOU?
}

I couldn't find a create_dir_with that would allow specifying e.g. OpenOptions, and all the create_dir functions don't return the Dir itself.

If not, would the project be open to contributions on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions