rust-embed
rust-embed copied to clipboard
How to check if a given path is a directory?
Don't support it? Or am I can't to find it?
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")
}
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
Fixed in #193
@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.
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?
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) );
}