diff options
-rw-r--r-- | example/src/main.rs | 5 | ||||
-rw-r--r-- | lib/src/lib.rs | 21 |
2 files changed, 25 insertions, 1 deletions
diff --git a/example/src/main.rs b/example/src/main.rs index 9ca782c..e1a4931 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -4,7 +4,10 @@ extern crate phf; include!(concat!(env!("OUT_DIR"), "/data.rs")); fn main() { - println!("{:?}", FILES.get("data/foo")) + println!("{:?}", FILES.get("data/foo")); + for name in FILES.file_names() { + println!("Found: {}", name); + } // for (k, v) in FILES.entries() { // println!("{}: {} bytes", k, v.len()); // } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index ce1e2ec..57d3610 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -37,6 +37,12 @@ impl Files { self.files.contains_key(path) } + /// Returns an iterator over all available file names. Does not + /// decompress any compressed data. + pub fn file_names(&self) -> FileNames { + FileNames { iter: self.files.keys() } + } + pub fn get(&self, path: &str) -> io::Result<Cow<'static, [u8]>> { let key = as_key(path); match self.files.get(key.borrow() as &str) { @@ -83,3 +89,18 @@ impl Files { } } } + +/// Iterates over the file names available for `Files` object. +pub struct FileNames<'a> { + /// Our internal iterator. We wrap this in a nice struct so our + /// caller doesn't need to know the details. + iter: phf::map::Keys<'a, &'static str, (Compression, &'static [u8])>, +} + +impl<'a> Iterator for FileNames<'a> { + type Item = &'static str; + + fn next(&mut self) -> Option<Self::Item> { + self.iter.next().map(|s| *s) + } +} |