Giter Site home page Giter Site logo

globmatch's Introduction

globmatch

Build status

Rust crate for resolving globs relative to a specified directory. Based on globset and walkdir.

Documentation

https://docs.rs/globmatch

For the documentation of the glob format please refer to

https://docs.rs/globset

Usage

Add this to your Cargo.toml:

[dependencies]
globmatch = "0.3"

Examples and concept

For CLI utilities it can be a common pattern to operate on a set of files. Such a set of files is either provided directly, as parameter to the tool - or via configuration files.

The use of a configuration file makes it easier to determine the location of a file since the path can be specified relative to the configuration. Consider, e.g., the following .json input:

{
  "globs": [
    "../../../some/text-files/**/*.txt",
    "other/inputs/*.md",
    "paths/from/dir[0-9]/*.*"
  ]
}

Specifying these paths in a dedicated configuration file allows to resolve the paths independent of the invocation of the script operating on these files, the location of the configuration file is used as base directory. This crate combines the features of the existing crates globset and walkdir to implement a relative glob matcher.

Example: A simple match.

The following example uses the files stored in the test-files/c-simple folder, we're trying to match all the .txt files using the glob test-files/c-simple/**/*.txt (where test-files/c-simple is the only relative path component).

/*
    Example files:
    globmatch/test-files/c-simple/.hidden
    globmatch/test-files/c-simple/.hidden/h_1.txt
    globmatch/test-files/c-simple/.hidden/h_0.txt
    globmatch/test-files/c-simple/a/a2/a2_0.txt
    globmatch/test-files/c-simple/a/a0/a0_0.txt
    globmatch/test-files/c-simple/a/a0/a0_1.txt
    globmatch/test-files/c-simple/a/a0/A0_3.txt
    globmatch/test-files/c-simple/a/a0/a0_2.md
    globmatch/test-files/c-simple/a/a1/a1_0.txt
    globmatch/test-files/c-simple/some_file.txt
    globmatch/test-files/c-simple/b/b_0.txt
 */

use globmatch;

fn example_a() -> Result<(), String> {
    let builder = globmatch::Builder::new("test-files/c-simple/**/*.txt")
        .build(env!("CARGO_MANIFEST_DIR"))?;

    let paths: Vec<_> = builder.into_iter()
        .flatten()
        .collect();

    println!(
        "paths:\n{}",
        paths
            .iter()
            .map(|p| format!("{}", p.to_string_lossy()))
            .collect::<Vec<_>>()
            .join("\n")
    );

    assert_eq!(6 + 2 + 1, paths.len());
    Ok(())
}

example_a().unwrap();

Example: Specifying options and using .filter_entry.

Similar to the builder pattern in globset when using globset::GlobBuilder, this crate allows to pass options (currently just case sensitivity) to the builder.

In addition, the filter_entry function from walkdir is accessible, but only as a single call (this crate does not implement a recursive iterator). This function allows filter files and folders before matching against the provided glob and therefore to efficiently exclude files and folders, e.g., hidden folders:

use globmatch;

fn example_b() -> Result<(), String> {
   let root = env!("CARGO_MANIFEST_DIR");
   let pattern = "test-files/c-simple/**/[ah]*.txt";

   let builder = globmatch::Builder::new(pattern)
       .case_sensitive(true)
       .build(root)?;

   let paths: Vec<_> = builder
       .into_iter()
       .filter_entry(|p| !globmatch::is_hidden_entry(p))
       .flatten()
       .collect();

   assert_eq!(4, paths.len());
   Ok(())
}

example_b().unwrap();

globmatch's People

Contributors

lmapii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

globmatch's Issues

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.