1 个不稳定版本
0.1.2 | 2020 年 7 月 14 日 |
---|
#38 在 #glob-pattern
162 每月下载量
50KB
951 行
包含/排除文件模式匹配。
这实现了类似于 git
在包含/排除文件中进行的匹配的 glob Pattern
,并提供了一些辅助方法来确定文件是否应该被包含,基于其指定的路径和一个包含/排除模式列表。
以下是一个相当长的匹配示例
let file_list: &'static [&'static [u8]] = &[
b"/things",
b"/things/file1.dat",
b"/things/file2.dat",
b"/things/shop",
b"/things/shop/info.txt",
b"/things/shop/apples",
b"/things/shop/apples/gala.txt",
b"/things/shop/apples/golden-delicious.txt",
b"/things/shop/bananas",
b"/things/shop/bananas/straight.txt",
b"/things/shop/bananas/curved.txt",
b"/things/shop/bananas/curved.bak",
b"/things/shop/bananas/other.txt",
];
let mut list = vec![
MatchEntry::include(Pattern::path("shop")?),
MatchEntry::exclude(Pattern::path("bananas")?),
MatchEntry::include(Pattern::path("bananas/curved.*")?),
];
assert_eq!(list.matches("/things", None), None);
assert_eq!(list.matches("/things/shop", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/bananas", None), Some(MatchType::Exclude));
assert_eq!(list.matches("/things/shop/bananas/curved.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/bananas/curved.bak", None), Some(MatchType::Include));
// this will exclude the curved.bak file
list.push(MatchEntry::exclude(Pattern::path("curved.bak")?));
assert_eq!(list.matches("/things/shop/bananas/curved.bak", None), Some(MatchType::Exclude));
list.pop();
// but this will not:
list.push(
MatchEntry::new(Pattern::path("curved.bak")?, MatchType::Exclude)
.flags(MatchFlag::ANCHORED)
);
// or: list.push
assert_eq!(list.matches("/things/shop/bananas/curved.bak", None), Some(MatchType::Include));
list.pop();
// let's check some patterns, anything starting with a 'c', 'f' or 's':
let mut list = vec![MatchEntry::include(Pattern::path("[cfs]*")?)];
assert_eq!(list.matches("/things", None), None);
assert_eq!(list.matches("/things/file1.dat", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/file2.dat", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/info.txt", None), None);
assert_eq!(list.matches("/things/shop/apples", None), None);
assert_eq!(list.matches("/things/shop/apples/gala.txt", None), None);
assert_eq!(list.matches("/things/shop/apples/golden-delicious.txt", None), None);
assert_eq!(list.matches("/things/shop/bananas", None), None);
assert_eq!(list.matches("/things/shop/bananas/straight.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/bananas/curved.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/shop/bananas/curved.bak", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/bananas/other.txt", None), None);
// If we add `**` we end up including the entire `shop/` subtree:
list.push(MatchEntry::include(Pattern::path("[cfs]*/**")?));
assert_eq!(list.matches("/things", None), None);
assert_eq!(list.matches("/things/file1.dat", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/file2.dat", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/info.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/apples", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/apples/gala.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/shop/apples/golden-delicious.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/bananas", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/bananas/straight.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/things/shop/bananas/curved.txt", None), Some(MatchType::Include));
assert_eq!(list.matches("/shop/bananas/curved.bak", None), Some(MatchType::Include));
assert_eq!(list.matches("/shop/bananas/other.txt", None), Some(MatchType::Include));
依赖项
~145KB