Crates.io | simple-path-match |
lib.rs | simple-path-match |
version | 0.2.0 |
source | src |
created_at | 2023-01-15 18:56:31.998127 |
updated_at | 2023-01-15 18:56:31.998127 |
description | Trivial path matching |
homepage | https://github.com/FrancisRussell/simple-path-match |
repository | https://github.com/FrancisRussell/simple-path-match.git |
max_upload_size | |
id | 759631 |
size | 29,036 |
This is a utility library for another project. It is factored out into a separate repository since this makes it easier to run tests.
Implements the ability to match patterns against paths:
*
being
supported.*
cannot match path separators.*
s cannot appear in a single component.OsStr
s are
supported.std::path
...
instances may appear in the pattern - the library is only intended
for evaluating relative paths below a root path.PathMatch
construction time.This library is used by a project which compiles to WASM (not using WASI)
running on node.js, meaning that the semantics of std::path
are unclear -
there is neither filesystem access and the properties of the host filesystem
are only known at runtime.
This existing glob libraries I found were statically tied to the expected
separator for the host file system and/or the use of std::path
.
In addition, this library doesn't make of the regex
crate and is no_std
compatible.
Matcher against multiple paths:
// We use raw string literals here because we use backslash as a separator
let mut builder = PathMatchBuilder::new(r"\");
builder.add_pattern("./pdfs/*.pdf")?;
builder.add_pattern("./oggs/*.ogg")?;
builder.add_pattern("./folder_a")?;
builder.add_pattern("./folder_b/")?;
builder.add_pattern("./*/*/prefix.*")?;
let matcher = builder.build()?;
assert!(matcher.matches(r".\pdfs\test.pdf"));
assert!(matcher.matches(r"oggs\test.ogg"));
// Will match with or without a trailing slash
assert!(matcher.matches(r"folder_a"));
assert!(matcher.matches(r"folder_a\"));
// This does not match since trailing slashes are required if specified
assert!(!matcher.matches(r"folder_b"));
// But this one will
assert!(matcher.matches(r"folder_b\"));
// Wildcards are fine anywhere in a component, but we can only have one e.g. no *.*
assert!(matcher.matches(r"a\b\prefix.txt"));
Ok(())