Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check if a given path is a directory? #170

Closed
markijohn opened this issue Mar 11, 2022 · 6 comments
Closed

How to check if a given path is a directory? #170

markijohn opened this issue Mar 11, 2022 · 6 comments

Comments

@markijohn
Copy link

Don't support it? Or am I can't to find it?

@markijohn
Copy link
Author

For example :

in directory
/embed
/embed/some_dir
/embed/some_dir/myfile.data

#[derive(RustEmbed)]
#[folder = "embed/"]
struct Asset;

fn is_dir(path:&str) -> bool {
  Asset::is_dir("embed/some_dir")
}

@AzureMarker
Copy link
Collaborator

We don't have that API, but it would be a good addition to https://docs.rs/rust-embed/6.3.0/rust_embed/struct.Metadata.html

@pyrossh
Copy link
Owner

pyrossh commented Nov 22, 2022

Fixed in #193

@pyrossh pyrossh closed this as completed Nov 22, 2022
@jaztec
Copy link
Contributor

jaztec commented Nov 24, 2022

@markijohn Could you please elaborate on your usecase, in what setting would you need to know why an entry is a directory?

The embedded files are known at compile time otherwise they are not included in the binary.

@AzureMarker
Copy link
Collaborator

Reopening since the PR got reverted. @markijohn can you elaborate on your use case? What are you trying to achieve with an is_dir function?

@AzureMarker AzureMarker reopened this Nov 24, 2022
@markijohn
Copy link
Author

I am sorry for the late comment. This was arbitrarily modified a long time ago to the following, but I wanted to know if it could be done without 'iter()' for optimization when there are a lot of embedded files and directories.

embed/somedir/file.txt
embed/somefile
#[macro_use]
extern crate rust_embed;

#[derive(RustEmbed)]
#[folder = "embed/"]
struct Asset;

fn is_exist_dir(path:&str) -> bool {
    // Asset::is_dir( path ) ?
    if path.ends_with("/") {
        Asset::iter().find( |v| v.starts_with(path) ).is_some()
    } else {
        // let added_pathsep = format!("{}/", path);
        // Asset::iter().find( |v| v.starts_with( &added_pathsep ) ).is_some()
        Asset::iter().find( |v| v.starts_with( path ) && v[path.len() ..].starts_with('/') ).is_some()
    }
}

fn main() {
    let user_input = "somedir";
    assert!( is_exist_dir(user_input) );

    let user_input = "somefile";
    assert!( !is_exist_dir(user_input) );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants