rust-embed icon indicating copy to clipboard operation
rust-embed copied to clipboard

How to check if a given path is a directory?

Open markijohn opened this issue 3 years ago • 2 comments
trafficstars

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

markijohn avatar Mar 11 '22 16:03 markijohn

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

markijohn avatar Mar 12 '22 00:03 markijohn

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

AzureMarker avatar Mar 12 '22 20:03 AzureMarker

Fixed in #193

pyrossh avatar Nov 22 '22 17:11 pyrossh

@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.

jaztec avatar Nov 24 '22 23:11 jaztec

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 avatar Nov 24 '22 23:11 AzureMarker

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

markijohn avatar Dec 12 '22 12:12 markijohn