5 releases

0.1.4 Oct 20, 2022
0.1.3 Jul 19, 2021
0.1.2 Jul 19, 2021
0.1.1 Jul 17, 2021
0.1.0 Jul 16, 2021

#870 in Filesystem

Download history 75/week @ 2024-06-16 123/week @ 2024-06-23 149/week @ 2024-06-30 17/week @ 2024-07-07 44/week @ 2024-07-14 44/week @ 2024-07-21 45/week @ 2024-07-28 102/week @ 2024-08-04 72/week @ 2024-08-11 8/week @ 2024-08-18 90/week @ 2024-08-25 53/week @ 2024-09-01 40/week @ 2024-09-08 6/week @ 2024-09-15 19/week @ 2024-09-22 49/week @ 2024-09-29

116 downloads per month
Used in 6 crates (4 directly)

MIT license

18KB
204 lines

Crate search_path

Provides a very simple search path file finder.

MIT License Minimum Rust Version crates.io docs.rs Build Audit GitHub stars


The SearchPath type allows for the finding of files using a series of search directories. This is akin to the mechanism a shell uses to find executables using the PATH environment variable. It can be constructed with a search path from an environment variable, from a string, or from a list of either string or Path/PathBuf values. Typically the find methods return the first matching file or directory, but the find_all method specifically collects and returns a list of all matching paths.

Constructors

The SearchPath type also has From<> implementations for PathBuf, Vec<PathBuf>, Vec<&Path>, Vec<&str>, String, and &str. In the case of vector values, or a single PathBuf, each path value will be used as-is without trying to split it into components. In the case of individual String and &str values the value will be split using the platform specific path separator into individual paths components.

Example

The following example shows the common pattern of finding an executable command on the command line.

use search_path::SearchPath;
use std::path::PathBuf;

fn which_command(cmd_name: &str) -> Option<PathBuf> {
    let search_path = SearchPath::new("PATH").unwrap();
    search_path.find_file(&PathBuf::from(cmd_name))
}

Changes

Version 0.1.4

  • Added new constructor path as a simple shortcut.

Version 0.1.3

  • Added implementation of IntoIterator to extract paths.

Version 0.1.2

  • Added a dedup method.

Version 0.1.1

  • Completed documentation.
  • Added Github builds.
  • Fixed bug in test cases for Windows builds.

Version 0.1.0

  • Initial commit.

No runtime deps