1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use glob::{Pattern, PatternError};
use std::{path::PathBuf, str::FromStr};
use crate::{Content, ContentMap};
impl ContentMap {
/// Return an iterator that produces all the contents in the archive
/// that match the given pattern.
///
/// # Errors
///
/// This may return an error if the pattern is invalid.
///
/// # Examples
///
/// ```rust
/// use std::path::Path;
/// use warpalib::RenpyArchive;
///
/// // Create a new archive and add sample files.
/// let mut archive = RenpyArchive::new();
/// archive.content.insert_raw("silk.png", vec![]);
/// archive.content.insert_raw("cherry.png", vec![]);
/// archive.content.insert_raw("yucca.jpg", vec![]);
///
/// // Retrieve files with png extension.
/// let paths = archive.content
/// .glob("*.png")
/// .expect("Failed to compile pattern")
/// .map(|(path, _)| path.as_ref())
/// .collect::<Vec<_>>();
///
/// assert!(paths.contains(&Path::new("silk.png")));
/// assert!(paths.contains(&Path::new("cherry.png")));
/// ```
pub fn glob(
&self,
pattern: &str,
) -> Result<impl Iterator<Item = (&PathBuf, &Content)>, PatternError> {
let pattern = Pattern::from_str(pattern)?;
let iter = self
.iter()
.filter(move |(path, _)| pattern.matches_path(path));
Ok(iter)
}
/// Consumes the content map and returns an iterator with owned contents
/// that matches the given glob pattern.
pub fn into_glob(
self,
pattern: &str,
) -> Result<impl Iterator<Item = (PathBuf, Content)>, PatternError> {
let pattern = Pattern::from_str(pattern)?;
let iter = self
.into_iter()
.filter(move |(path, _)| pattern.matches_path(path));
Ok(iter)
}
}