use std::collections::BTreeSet as Set;
use std::fs::File;
use std::io::Read;
use glob::{glob, PatternError};
use regex::Regex;
pub fn get_files(root: Option<String>) -> Result<Vec<String>, PatternError> {
let dir = match root {
Some(d) => d,
None => ".".to_string(),
};
let mut files = Vec::new();
let txts = glob(&format!("{}/**/*.txt", dir))?;
let mds = glob(&format!("{}/**/*.md", dir))?;
let orgs = glob(&format!("{}/**/*.org", dir))?;
for filename in txts.chain(mds).chain(orgs).flatten() {
files.push(filename.to_string_lossy().into());
}
Ok(files)
}
pub fn get_tags_for_file(filename: &str) -> Set<String> {
let mut file =
File::open(filename).unwrap_or_else(|_| panic!("Couldn't open file: `{:?}`", filename));
let mut contents = String::new();
file.read_to_string(&mut contents)
.unwrap_or_else(|_| panic!("Couldn't read contents of file: `{:?}`", filename));
get_tags_from_string(&contents)
}
fn get_tags_from_string(contents: &str) -> Set<String> {
lazy_static! {
static ref RE: Regex = Regex::new(r"(?:^|\s)@(?P<keyword>[a-zA-Z_0-9\-]+)")
.expect("Couldn't create keyword regex");
}
let mut keywords = Set::new();
for cap in RE.captures_iter(contents) {
keywords.insert(cap["keyword"].to_string());
}
keywords
}
#[allow(unused_imports)]
mod tests {
use super::get_tags_from_string;
use std::collections::BTreeSet as Set;
#[test]
fn tags_from_string() {
let output = vec!["a", "b", "c"]
.iter()
.cloned()
.map(|x| x.to_string())
.collect::<Set<String>>();
let input = "@a @b @c";
assert_eq!(
get_tags_from_string(input)
.iter()
.cloned()
.collect::<Set<String>>(),
output
);
}
}