Code Monkey home page Code Monkey logo

Comments (6)

markijohn avatar markijohn commented on June 27, 2024

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

from rust-embed.

AzureMarker avatar AzureMarker commented on June 27, 2024

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

from rust-embed.

pyrossh avatar pyrossh commented on June 27, 2024

Fixed in #193

from rust-embed.

jaztec avatar jaztec commented on June 27, 2024

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

from rust-embed.

AzureMarker avatar AzureMarker commented on June 27, 2024

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?

from rust-embed.

markijohn avatar markijohn commented on June 27, 2024

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

from rust-embed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.