Skip to content

Instantly share code, notes, and snippets.

@kejadlen
Created August 8, 2016 04:24
Show Gist options
  • Save kejadlen/22aac86b1cf331ee42090fbbb2e99dad to your computer and use it in GitHub Desktop.
Save kejadlen/22aac86b1cf331ee42090fbbb2e99dad to your computer and use it in GitHub Desktop.
use std::env::args;
use std::fs::{read_dir, File};
use std::io::prelude::*;
use std::path::PathBuf;
fn main() {
let dirs = args().skip(1);
let paths = dirs.flat_map(|dir| {
read_dir(&dir)
.expect(&format!("unable to read dir {}", dir))
.map(|dir_entry| dir_entry.expect("").path())
})
.collect::<Vec<_>>();
let files = filter_md(&paths);
let lints = files.iter()
.flat_map(|path| {
let mut f = File::open(path).expect("");
let mut s = String::new();
f.read_to_string(&mut s).expect("");
lints_in_file(&s).into_iter().map(|x| (path, x))
})
.collect::<Vec<_>>();
// println!("{:?}", lints);
}
fn filter_md<'a>(paths: &'a Vec<PathBuf>) -> Vec<&'a PathBuf> {
paths.iter()
.filter(|path_buf| {
path_buf.extension().map(|ext| ext == "md").unwrap_or(false)
})
.collect()
}
fn lints_in_file<'a>(content: &'a str) -> Vec<(usize, &'a str)> {
content.lines()
.enumerate()
.filter(|&(_, line)| {
let x = line.match_indices("file://")
.map(|(i, _)| i)
.collect::<Vec<_>>();
let y = line.match_indices("file:///projects/")
.map(|(i, _)| i)
.collect::<Vec<_>>();
x != y
})
.collect()
}
#[test]
fn test_filter_md() {
let input = vec!["a.md", "b", "c.txt"]
.iter()
.map(|x| PathBuf::from(x))
.collect::<Vec<_>>();
let output = filter_md(&input);
assert_eq!(output, vec![&PathBuf::from("a.md")]);
}
#[test]
fn test_lints_in_file() {
let input = "foo
file://projects
bar
file://home/steve
baz
file://projects/bindings
qux
file://
";
let output = lints_in_file(input)
.iter()
.map(|&(i, _)| i)
.collect::<Vec<_>>();
assert_eq!(output, vec![2, 4, 8]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment