globwalk-0.9.1/.cargo_vcs_info.json0000644000000001360000000000100126450ustar { "git": { "sha1": "8973fa2bc560be54c91448131238fa50d56ee121" }, "path_in_vcs": "" }globwalk-0.9.1/.github/workflows/ci.yml000064400000000000000000000024351046102023000161540ustar 00000000000000name: CI on: [push, pull_request] jobs: test: name: Test Suite runs-on: ubuntu-latest strategy: matrix: rust: - stable - beta - nightly steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} - name: Run cargo test run: cargo test test-msrv: name: Test Suite runs-on: [ubuntu-latest, windows-latest] strategy: matrix: rust: - 1.70.0 - stable steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} - name: Run cargo test # Exclude doctests here, as we don't want to clutter docs themselves # with backwards compatibility workarounds. run: cargo test --lib style: name: lints and formatting runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: 1.70.0 # pin a version for reproducible results components: rustfmt - name: Check warnings run: RUSTFLAGS="-D warnings" cargo check --all-targets - name: Check formatting run: cargo fmt -- --checkglobwalk-0.9.1/.gitignore000064400000000000000000000000701046102023000134220ustar 00000000000000.idea/ cmake-build-debug/ /target **/*.rs.bk Cargo.lock globwalk-0.9.1/Cargo.lock0000644000000271130000000000100106240ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "aho-corasick" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" dependencies = [ "memchr", ] [[package]] name = "backtrace" version = "0.3.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1371048253fa3bac6704bfd6bbfc922ee9bdcee8881330d40f308b81cc5adc55" dependencies = [ "backtrace-sys", "cfg-if 0.1.10", "libc", "rustc-demangle", ] [[package]] name = "backtrace-sys" version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18fbebbe1c9d1f383a9cc7e8ccdb471b91c8d024ee9c2ca5b5346121fe8b4399" dependencies = [ "cc", "libc", ] [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" [[package]] name = "bstr" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "542f33a8835a0884b006a0c3df3dadd99c0c3f296ed26c2fdc8028e01ad6230c" dependencies = [ "memchr", "serde", ] [[package]] name = "cc" version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ "libc", ] [[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "docmatic" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d58ebceaf539cb79420fb3046da9396e11021c536ae953be6ba5363842da8c3" dependencies = [ "which", ] [[package]] name = "errno" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", "windows-sys", ] [[package]] name = "failure" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" dependencies = [ "backtrace", ] [[package]] name = "fastrand" version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "globset" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1" dependencies = [ "aho-corasick", "bstr", "log", "regex-automata", "regex-syntax", ] [[package]] name = "globwalk" version = "0.9.1" dependencies = [ "bitflags 2.4.1", "docmatic", "ignore", "tempfile", "walkdir", ] [[package]] name = "ignore" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbe7873dab538a9a44ad79ede1faf5f30d49f9a5c883ddbab48bce81b64b7492" dependencies = [ "globset", "lazy_static", "log", "memchr", "regex", "same-file", "thread_local", "walkdir", "winapi-util", ] [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" version = "0.2.151" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" [[package]] name = "linux-raw-sys" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" [[package]] name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "proc-macro2" version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] [[package]] name = "redox_syscall" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" dependencies = [ "bitflags 1.3.2", ] [[package]] name = "regex" version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rustc-demangle" version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" version = "0.38.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" dependencies = [ "bitflags 2.4.1", "errno", "libc", "linux-raw-sys", "windows-sys", ] [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "serde" version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "syn" version = "2.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tempfile" version = "3.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01ce4141aa927a6d1bd34a041795abd0db1cccba5d5f24b009f694bdf3a1f3fa" dependencies = [ "cfg-if 1.0.0", "fastrand", "redox_syscall", "rustix", "windows-sys", ] [[package]] name = "thread_local" version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" dependencies = [ "cfg-if 1.0.0", "once_cell", ] [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "walkdir" version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "which" version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b57acb10231b9493c8472b20cb57317d0679a49e0bdbee44b3b803a6473af164" dependencies = [ "failure", "libc", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" [[package]] name = "windows_aarch64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" [[package]] name = "windows_i686_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" [[package]] name = "windows_i686_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" [[package]] name = "windows_x86_64_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" [[package]] name = "windows_x86_64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" globwalk-0.9.1/Cargo.toml0000644000000020410000000000100106400ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "globwalk" version = "0.9.1" authors = ["Gilad Naaman "] description = "Glob-matched recursive file system walking." readme = "README.md" keywords = [ "regex", "glob", "pattern", "walk", "iterator", ] categories = ["filesystem"] license = "MIT" repository = "https://github.com/gilnaa/globwalk" resolver = "2" [dependencies.bitflags] version = "2" [dependencies.ignore] version = "0.4.11" [dependencies.walkdir] version = "2" [dev-dependencies.docmatic] version = "0.1.2" [dev-dependencies.tempfile] version = "3" globwalk-0.9.1/Cargo.toml.orig000064400000000000000000000007331046102023000143270ustar 00000000000000[package] name = "globwalk" version = "0.9.1" description = "Glob-matched recursive file system walking." authors = ["Gilad Naaman "] readme = "README.md" license = "MIT" repository = "https://github.com/gilnaa/globwalk" categories = ["filesystem"] keywords = ["regex", "glob", "pattern", "walk", "iterator"] edition = "2021" resolver = "2" [dependencies] walkdir = "2" ignore = "0.4.11" bitflags = "2" [dev-dependencies] tempfile = "3" docmatic = "0.1.2" globwalk-0.9.1/LICENSE000064400000000000000000000020371046102023000124440ustar 00000000000000Copyright (c) 2017 Gilad Naaman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.globwalk-0.9.1/README.md000064400000000000000000000031211046102023000127110ustar 00000000000000# GlobWalk # [![](https://docs.rs/globwalk/badge.svg)](https://docs.rs/globwalk/) ![License](https://img.shields.io/crates/l/globwalk.svg) [![crates.io](https://img.shields.io/crates/v/globwalk.svg)](https://crates.io/crates/globwalk) Recursively find files in a directory using globs. This crate is now in a perpetual maintnance mode and new users should probably cosider using [`glob`](https://crates.io/crates/glob/). ### Comparison to the `glob` crate ### This crate was origially written years ago, when [`glob`](https://crates.io/crates/glob/) was a very differet crate, before it was adopted by the rust-lang org. Nowadays `glob` is much better, and overall better maintained, but there are a few features that it does not seem to have (based on [glob 0.3.1](https://docs.rs/glob/0.3.1/src/glob/lib.rs.html#466)): - The `glob` crate does not support having `{a,b}` in patterns. - `globwalk` can match several glob-patterns at the same time. - `globwalk` supports excluding results with `!`. (negative patterns) - `glob` searches for files in the current working directory, whereas `globwalk` starts at a specified base-dir. ### Usage ### To use this crate, add `globwalk` as a dependency to your project's `Cargo.toml`: ```toml [dependencies] globwalk = "0.9.1" ``` The following piece of code recursively find all `png`, `jpg`, or `gif` files: ```rust extern crate globwalk; use std::fs; for img in globwalk::glob("*.{png,jpg,gif}").unwrap() { if let Ok(img) = img { println!("{:?}", img.path()); } } ``` See the [documentation](https://docs.rs/globwalk/) for more details. globwalk-0.9.1/examples/list.rs000064400000000000000000000025611046102023000146000ustar 00000000000000// Copyright (c) 2018 Gilad Naaman // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. use globwalk::GlobWalkerBuilder; use std::env::args; fn main() { let patterns = args().skip(1).collect::>(); for f in GlobWalkerBuilder::from_patterns(".", &patterns[..]) .build() .unwrap() { println!("{:?}", f.unwrap().path()); } } globwalk-0.9.1/src/doctests.rs000064400000000000000000000007251046102023000144260ustar 00000000000000use std::error::Error; use std::fs::{create_dir_all, File}; use std::path::PathBuf; use tempfile::TempDir; fn create_files(files: &[&str]) -> Result> { let tmp_dir = TempDir::new()?; for f in files { let file_path = PathBuf::from(f); if let Some(dir) = file_path.parent() { create_dir_all(tmp_dir.path().join(dir))?; } File::create(tmp_dir.path().join(file_path))?; } Ok(tmp_dir) } globwalk-0.9.1/src/lib.rs000064400000000000000000000660501046102023000133470ustar 00000000000000// Copyright (c) 2017 Gilad Naaman // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //! Recursively find files in a directory using globs. //! //! Features include //! - [`gitignore`'s extended glob syntax][gitignore] //! - Control over symlink behavior //! - Control depth walked //! - Control order results are returned //! //! [gitignore]: https://git-scm.com/docs/gitignore#_pattern_format //! //! # Examples //! //! ## Finding image files in the current directory. //! //! ```rust //! extern crate globwalk; //! # include!("doctests.rs"); //! //! use std::fs; //! # fn run() -> Result<(), Box> { //! # let temp_dir = create_files(&["cow.jog", "cat.gif"])?; //! # ::std::env::set_current_dir(&temp_dir)?; //! //! for img in globwalk::glob("*.{png,jpg,gif}")? { //! if let Ok(img) = img { //! fs::remove_file(img.path())?; //! } //! } //! # Ok(()) } //! # fn main() { run().unwrap() } //! ``` //! //! ## Advanced Globbing ### //! //! By using one of the constructors of `globwalk::GlobWalker`, it is possible to alter the //! base-directory or add multiple patterns. //! //! ```rust //! extern crate globwalk; //! # include!("doctests.rs"); //! //! use std::fs; //! //! # fn run() -> Result<(), Box> { //! # let temp_dir = create_files(&["cow.jog", "cat.gif"])?; //! # let BASE_DIR = &temp_dir; //! let walker = globwalk::GlobWalkerBuilder::from_patterns( //! BASE_DIR, //! &["*.{png,jpg,gif}", "!Pictures/*"], //! ) //! .max_depth(4) //! .follow_links(true) //! .build()? //! .into_iter() //! .filter_map(Result::ok); //! //! for img in walker { //! fs::remove_file(img.path())?; //! } //! # Ok(()) } //! # fn main() { run().unwrap() } //! ``` // Our doctests need main to compile; AFAICT this is a false positive generated by clippy #![allow(clippy::needless_doctest_main)] #![warn(missing_docs)] use ignore::overrides::{Override, OverrideBuilder}; use ignore::Match; use std::cmp::Ordering; use std::path::Path; use std::path::PathBuf; use walkdir::WalkDir; /// Error from parsing globs. #[derive(Debug)] pub struct GlobError(ignore::Error); /// Error from iterating on files. pub type WalkError = walkdir::Error; /// A directory entry. /// /// This is the type of value that is yielded from the iterators defined in this crate. pub type DirEntry = walkdir::DirEntry; impl From for GlobError { fn from(e: std::io::Error) -> Self { GlobError(e.into()) } } impl From for std::io::Error { fn from(e: GlobError) -> Self { if let ignore::Error::Io(e) = e.0 { e } else { std::io::ErrorKind::Other.into() } } } impl std::fmt::Display for GlobError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { self.0.fmt(f) } } impl std::error::Error for GlobError {} bitflags::bitflags! { /// Possible file type filters. /// Constants can be OR'd to filter for several types at a time. /// /// Note that not all files are represented in this enum. /// For example, a char-device is neither a file, a directory, nor a symlink. pub struct FileType: u32 { #[allow(missing_docs)] const FILE = 0b001; #[allow(missing_docs)] const DIR = 0b010; #[allow(missing_docs)] const SYMLINK = 0b100; } } /// An iterator for recursively yielding glob matches. /// /// The order of elements yielded by this iterator is unspecified. pub struct GlobWalkerBuilder { root: PathBuf, patterns: Vec, walker: WalkDir, case_insensitive: bool, file_type: Option, } impl GlobWalkerBuilder { /// Construct a new `GlobWalker` with a glob pattern. /// /// When iterated, the `base` directory will be recursively searched for paths /// matching `pattern`. pub fn new(base: P, pattern: S) -> Self where P: AsRef, S: AsRef, { GlobWalkerBuilder::from_patterns(base, &[pattern]) } /// Construct a new `GlobWalker` from a list of patterns. /// /// When iterated, the `base` directory will be recursively searched for paths /// matching `patterns`. pub fn from_patterns(base: P, patterns: &[S]) -> Self where P: AsRef, S: AsRef, { fn normalize_pattern>(pattern: S) -> String { // Either `ignore` or our iteration code treat a single asterisk pretty strangely, matching everything, even // paths that are inside a sub-direcrtory. if pattern.as_ref() == "*" { String::from("/*") } else { pattern.as_ref().to_owned() } } GlobWalkerBuilder { root: base.as_ref().into(), patterns: patterns.iter().map(normalize_pattern).collect::<_>(), walker: WalkDir::new(base), case_insensitive: false, file_type: None, } } /// Set the minimum depth of entries yielded by the iterator. /// /// The smallest depth is `0` and always corresponds to the path given /// to the `new` function on this type. Its direct descendents have depth /// `1`, and their descendents have depth `2`, and so on. pub fn min_depth(mut self, depth: usize) -> Self { self.walker = self.walker.min_depth(depth); self } /// Set the maximum depth of entries yield by the iterator. /// /// The smallest depth is `0` and always corresponds to the path given /// to the `new` function on this type. Its direct descendents have depth /// `1`, and their descendents have depth `2`, and so on. /// /// Note that this will not simply filter the entries of the iterator, but /// it will actually avoid descending into directories when the depth is /// exceeded. pub fn max_depth(mut self, depth: usize) -> Self { self.walker = self.walker.max_depth(depth); self } /// Follow symbolic links. By default, this is disabled. /// /// When `yes` is `true`, symbolic links are followed as if they were /// normal directories and files. If a symbolic link is broken or is /// involved in a loop, an error is yielded. /// /// When enabled, the yielded [`DirEntry`] values represent the target of /// the link while the path corresponds to the link. See the [`DirEntry`] /// type for more details. /// /// [`DirEntry`]: struct.DirEntry.html pub fn follow_links(mut self, yes: bool) -> Self { self.walker = self.walker.follow_links(yes); self } /// Set the maximum number of simultaneously open file descriptors used /// by the iterator. /// /// `n` must be greater than or equal to `1`. If `n` is `0`, then it is set /// to `1` automatically. If this is not set, then it defaults to some /// reasonably low number. /// /// This setting has no impact on the results yielded by the iterator /// (even when `n` is `1`). Instead, this setting represents a trade off /// between scarce resources (file descriptors) and memory. Namely, when /// the maximum number of file descriptors is reached and a new directory /// needs to be opened to continue iteration, then a previous directory /// handle is closed and has its unyielded entries stored in memory. In /// practice, this is a satisfying trade off because it scales with respect /// to the *depth* of your file tree. Therefore, low values (even `1`) are /// acceptable. /// /// Note that this value does not impact the number of system calls made by /// an exhausted iterator. /// /// # Platform behavior /// /// On Windows, if `follow_links` is enabled, then this limit is not /// respected. In particular, the maximum number of file descriptors opened /// is proportional to the depth of the directory tree traversed. pub fn max_open(mut self, n: usize) -> Self { self.walker = self.walker.max_open(n); self } /// Set a function for sorting directory entries. /// /// If a compare function is set, the resulting iterator will return all /// paths in sorted order. The compare function will be called to compare /// entries from the same directory. pub fn sort_by(mut self, cmp: F) -> Self where F: FnMut(&DirEntry, &DirEntry) -> Ordering + Send + Sync + 'static, { self.walker = self.walker.sort_by(cmp); self } /// Yield a directory's contents before the directory itself. By default, /// this is disabled. /// /// When `yes` is `false` (as is the default), the directory is yielded /// before its contents are read. This is useful when, e.g. you want to /// skip processing of some directories. /// /// When `yes` is `true`, the iterator yields the contents of a directory /// before yielding the directory itself. This is useful when, e.g. you /// want to recursively delete a directory. pub fn contents_first(mut self, yes: bool) -> Self { self.walker = self.walker.contents_first(yes); self } /// Toggle whether the globs should be matched case insensitively or not. /// /// This is disabled by default. pub fn case_insensitive(mut self, yes: bool) -> Self { self.case_insensitive = yes; self } /// Toggle filtering by file type. /// `FileType` can be an OR of several types. /// /// Note that not all file-types can be whitelisted by this filter (e.g. char-devices, fifos, etc.) pub fn file_type(mut self, file_type: FileType) -> Self { self.file_type = Some(file_type); self } /// Finalize and build a `GlobWalker` instance. pub fn build(self) -> Result { let mut builder = OverrideBuilder::new(self.root); builder .case_insensitive(self.case_insensitive) .map_err(GlobError)?; for pattern in self.patterns { builder.add(pattern.as_ref()).map_err(GlobError)?; } Ok(GlobWalker { ignore: builder.build().map_err(GlobError)?, walker: self.walker.into_iter(), file_type_filter: self.file_type, }) } } /// An iterator which emits glob-matched patterns. /// /// An instance of this type must be constructed through `GlobWalker`, /// which uses a builder-style pattern. /// /// The order of the yielded paths is undefined, unless specified by the user /// using `GlobWalker::sort_by`. pub struct GlobWalker { ignore: Override, walker: walkdir::IntoIter, file_type_filter: Option, } impl Iterator for GlobWalker { type Item = Result; // Possible optimization - Do not descend into directory that will never be a match fn next(&mut self) -> Option { let mut skip_dir = false; // The outer loop allows us to avoid multiple mutable borrows on `self.walker` when // we want to skip. 'skipper: loop { if skip_dir { self.walker.skip_current_dir(); } // The inner loop just advances the iterator until a match is found. for entry in &mut self.walker { match entry { Ok(e) => { let is_dir = e.file_type().is_dir(); let file_type = if e.file_type().is_dir() { Some(FileType::DIR) } else if e.file_type().is_file() { Some(FileType::FILE) } else if e.file_type().is_symlink() { Some(FileType::SYMLINK) } else { None }; let file_type_matches = match (self.file_type_filter.as_ref(), file_type) { (None, _) => true, (Some(_), None) => false, (Some(filter), Some(actual)) => filter.contains(actual), }; // Strip the common base directory so that the matcher will be // able to recognize the file name. // `unwrap` here is safe, since walkdir returns the files with relation // to the given base-dir. let path = e.path().strip_prefix(self.ignore.path()).unwrap(); // The path might be empty after stripping if the current base-directory is matched. if path.as_os_str().is_empty() { continue 'skipper; } match self.ignore.matched(path, is_dir) { Match::Whitelist(_) if file_type_matches => return Some(Ok(e)), // If the directory is ignored, quit the iterator loop and // skip-out of this directory. Match::Ignore(_) if is_dir => { skip_dir = true; continue 'skipper; } _ => {} } } Err(e) => { return Some(Err(e)); } } } break; } None } } /// Construct a new `GlobWalkerBuilder` with a glob pattern. /// /// When iterated, the current directory will be recursively searched for paths /// matching `pattern`, unless the pattern specifies an absolute path. pub fn glob_builder>(pattern: S) -> GlobWalkerBuilder { // Check to see if the pattern starts with an absolute path let path_pattern: PathBuf = pattern.as_ref().into(); if path_pattern.is_absolute() { // If the pattern is an absolute path, split it into the longest base and a pattern. let mut base = PathBuf::new(); let mut pattern = PathBuf::new(); let mut globbing = false; // All `to_str().unwrap()` calls should be valid since the input is a string. for c in path_pattern.components() { let os = c.as_os_str().to_str().unwrap(); for c in &["*", "{", "}"][..] { if os.contains(c) { globbing = true; break; } } if globbing { pattern.push(c); } else { base.push(c); } } let pat = pattern.to_str().unwrap(); if cfg!(windows) { GlobWalkerBuilder::new(base.to_str().unwrap(), pat.replace('\\', "/")) } else { GlobWalkerBuilder::new(base.to_str().unwrap(), pat) } } else { // If the pattern is relative, start searching from the current directory. GlobWalkerBuilder::new(".", pattern) } } /// Construct a new `GlobWalker` with a glob pattern. /// /// When iterated, the current directory will be recursively searched for paths /// matching `pattern`, unless the pattern specifies an absolute path. pub fn glob>(pattern: S) -> Result { glob_builder(pattern).build() } #[cfg(test)] mod tests { use super::*; use std::fs::{create_dir_all, File}; use tempfile::TempDir; fn touch(dir: &TempDir, names: &[&str]) { for name in names { let name = normalize_path_sep(name); File::create(dir.path().join(name)).expect("Failed to create a test file"); } } fn normalize_path_sep>(s: S) -> String { s.as_ref() .replace("[/]", if cfg!(windows) { "\\" } else { "/" }) } fn equate_to_expected(g: GlobWalker, mut expected: Vec, dir_path: &Path) { for matched_file in g.into_iter().filter_map(Result::ok) { let path = matched_file .path() .strip_prefix(dir_path) .unwrap() .to_str() .unwrap(); let path = normalize_path_sep(path); let del_idx = if let Some(idx) = expected.iter().position(|n| &path == n) { idx } else { panic!("Iterated file is unexpected: {}", path); }; expected.remove(del_idx); } // Not equating `.len() == 0` so that the assertion output // will contain the extra files let empty: &[&str] = &[][..]; assert_eq!(expected, empty); } #[test] fn test_absolute_path() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path().canonicalize().unwrap(); touch(&dir, &["a.rs", "a.jpg", "a.png", "b.docx"][..]); let expected = ["a.jpg", "a.png"].iter().map(ToString::to_string).collect(); let mut cwd = dir_path.clone(); cwd.push("*.{png,jpg,gif}"); let glob = glob(cwd.to_str().unwrap().to_owned()).unwrap(); equate_to_expected(glob, expected, &dir_path); } #[test] fn test_new() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); touch(&dir, &["a.rs", "a.jpg", "a.png", "b.docx"][..]); let expected = ["a.jpg", "a.png"].iter().map(ToString::to_string).collect(); let g = GlobWalkerBuilder::new(dir_path, "*.{png,jpg,gif}") .build() .unwrap(); equate_to_expected(g, expected, dir_path); } #[test] fn test_from_patterns() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); create_dir_all(dir_path.join("src/some_mod")).expect(""); create_dir_all(dir_path.join("tests")).expect(""); create_dir_all(dir_path.join("contrib")).expect(""); touch( &dir, &[ "a.rs", "b.rs", "avocado.rs", "lib.c", "src[/]hello.rs", "src[/]world.rs", "src[/]some_mod[/]unexpected.rs", "src[/]cruel.txt", "contrib[/]README.md", "contrib[/]README.rst", "contrib[/]lib.rs", ][..], ); let expected: Vec<_> = [ "src[/]some_mod[/]unexpected.rs", "src[/]world.rs", "src[/]hello.rs", "lib.c", "contrib[/]lib.rs", "contrib[/]README.md", "contrib[/]README.rst", ] .iter() .map(normalize_path_sep) .collect(); let patterns = ["src/**/*.rs", "*.c", "**/lib.rs", "**/*.{md,rst}"]; let glob = GlobWalkerBuilder::from_patterns(dir_path, &patterns) .build() .unwrap(); equate_to_expected(glob, expected, dir_path); } #[test] fn test_case_insensitive_matching() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); create_dir_all(dir_path.join("src/some_mod")).expect(""); create_dir_all(dir_path.join("tests")).expect(""); create_dir_all(dir_path.join("contrib")).expect(""); touch( &dir, &[ "a.rs", "b.rs", "avocado.RS", "lib.c", "src[/]hello.RS", "src[/]world.RS", "src[/]some_mod[/]unexpected.rs", "src[/]cruel.txt", "contrib[/]README.md", "contrib[/]README.rst", "contrib[/]lib.rs", ][..], ); let expected: Vec<_> = [ "src[/]some_mod[/]unexpected.rs", "src[/]hello.RS", "src[/]world.RS", "lib.c", "contrib[/]lib.rs", "contrib[/]README.md", "contrib[/]README.rst", ] .iter() .map(normalize_path_sep) .collect(); let patterns = ["src/**/*.rs", "*.c", "**/lib.rs", "**/*.{md,rst}"]; let glob = GlobWalkerBuilder::from_patterns(dir_path, &patterns) .case_insensitive(true) .build() .unwrap(); equate_to_expected(glob, expected, dir_path); } #[test] fn test_match_dir() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); create_dir_all(dir_path.join("mod")).expect(""); touch( &dir, &[ "a.png", "b.png", "c.png", "mod[/]a.png", "mod[/]b.png", "mod[/]c.png", ][..], ); let expected: Vec<_> = ["mod"].iter().map(normalize_path_sep).collect(); let glob = GlobWalkerBuilder::new(dir_path, "mod").build().unwrap(); equate_to_expected(glob, expected, dir_path); } #[test] fn test_blacklist() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); create_dir_all(dir_path.join("src/some_mod")).expect(""); create_dir_all(dir_path.join("tests")).expect(""); create_dir_all(dir_path.join("contrib")).expect(""); touch( &dir, &[ "a.rs", "b.rs", "avocado.rs", "lib.c", "src[/]hello.rs", "src[/]world.rs", "src[/]some_mod[/]unexpected.rs", "src[/]cruel.txt", "contrib[/]README.md", "contrib[/]README.rst", "contrib[/]lib.rs", ][..], ); let expected: Vec<_> = [ "src[/]some_mod[/]unexpected.rs", "src[/]hello.rs", "lib.c", "contrib[/]lib.rs", "contrib[/]README.md", "contrib[/]README.rst", ] .iter() .map(normalize_path_sep) .collect(); let patterns = [ "src/**/*.rs", "*.c", "**/lib.rs", "**/*.{md,rst}", "!world.rs", ]; let glob = GlobWalkerBuilder::from_patterns(dir_path, &patterns) .build() .unwrap(); equate_to_expected(glob, expected, dir_path); } #[test] fn test_blacklist_dir() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); create_dir_all(dir_path.join("Pictures")).expect(""); touch( &dir, &[ "a.png", "b.png", "c.png", "Pictures[/]a.png", "Pictures[/]b.png", "Pictures[/]c.png", ][..], ); let expected: Vec<_> = ["a.png", "b.png", "c.png"] .iter() .map(normalize_path_sep) .collect(); let patterns = ["*.{png,jpg,gif}", "!Pictures"]; let glob = GlobWalkerBuilder::from_patterns(dir_path, &patterns) .build() .unwrap(); equate_to_expected(glob, expected, dir_path); } #[test] fn test_glob_with_double_star_pattern() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path().canonicalize().unwrap(); touch(&dir, &["a.rs", "a.jpg", "a.png", "b.docx"][..]); let expected = ["a.jpg", "a.png"].iter().map(ToString::to_string).collect(); let mut cwd = dir_path.clone(); cwd.push("**"); cwd.push("*.{png,jpg,gif}"); let glob = glob(cwd.to_str().unwrap().to_owned()).unwrap(); equate_to_expected(glob, expected, &dir_path); } #[test] fn test_glob_single_star() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); create_dir_all(dir_path.join("Pictures")).expect(""); create_dir_all(dir_path.join("Pictures").join("b")).expect(""); touch( &dir, &[ "a.png", "b.png", "c.png", "Pictures[/]a.png", "Pictures[/]b.png", "Pictures[/]c.png", "Pictures[/]b[/]c.png", "Pictures[/]b[/]c.png", "Pictures[/]b[/]c.png", ][..], ); let glob = GlobWalkerBuilder::new(dir_path, "*") .sort_by(|a, b| a.path().cmp(b.path())) .build() .unwrap(); let expected = ["Pictures", "a.png", "b.png", "c.png"] .iter() .map(ToString::to_string) .collect(); equate_to_expected(glob, expected, dir_path); } #[test] fn test_file_type() { let dir = TempDir::new().expect("Failed to create temporary folder"); let dir_path = dir.path(); create_dir_all(dir_path.join("Pictures")).expect(""); create_dir_all(dir_path.join("Pictures").join("b")).expect(""); touch( &dir, &[ "a.png", "b.png", "c.png", "Pictures[/]a.png", "Pictures[/]b.png", "Pictures[/]c.png", "Pictures[/]b[/]c.png", "Pictures[/]b[/]c.png", "Pictures[/]b[/]c.png", ][..], ); let glob = GlobWalkerBuilder::new(dir_path, "*") .sort_by(|a, b| a.path().cmp(b.path())) .file_type(FileType::DIR) .build() .unwrap(); let expected = ["Pictures"].iter().map(ToString::to_string).collect(); equate_to_expected(glob, expected, dir_path); let glob = GlobWalkerBuilder::new(dir_path, "*") .sort_by(|a, b| a.path().cmp(b.path())) .file_type(FileType::FILE) .build() .unwrap(); let expected = ["a.png", "b.png", "c.png"] .iter() .map(ToString::to_string) .collect(); equate_to_expected(glob, expected, dir_path); } } globwalk-0.9.1/tests/docs.rs000064400000000000000000000004031046102023000140720ustar 00000000000000#[test] fn readme_test() { let readme = std::path::Path::new(file!()).canonicalize().unwrap(); let readme = readme.parent().unwrap().parent().unwrap().join("README.md"); let readme = readme.to_str().unwrap(); docmatic::assert_file(readme); }