diff options
| author | Teddy Wing | 2016-04-17 21:32:53 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-04-17 21:32:53 -0400 |
| commit | ad2d9e0e1fa965ef94fcbf6e239729008cc34ba2 (patch) | |
| tree | a09d63f4c19165cb3fb6a3fd66f05f4f38cffd6c | |
| parent | 9240db56a7202b238b7880da9c82b1e73089f02e (diff) | |
| download | mutt-alias-auto-add-ad2d9e0e1fa965ef94fcbf6e239729008cc34ba2.tar.bz2 | |
Create a function that will check whether an alias is in a file
This will allow us to pass an alias and grep for it in a given file.
Just a rough outline to start. Added a super basic test and a file in
"testdata" to operate on for testing.
| -rw-r--r-- | src/main.rs | 24 | ||||
| -rw-r--r-- | src/tests.rs | 11 | ||||
| -rw-r--r-- | testdata/aliases | 1 |
3 files changed, 34 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 5e02b38..1677e5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use std::io::{self, BufRead}; +use std::io::{self, BufRead, BufReader}; +use std::fs::File; +use std::path::Path; #[cfg(test)] mod tests; @@ -34,6 +36,26 @@ fn build_alias(s: &str) -> String { alias_line } +fn is_alias_in_file(alias: &str, file: &str) -> Result<(), io::Error> { + let alias_parts: Vec<&str> = alias.split_whitespace().collect(); + + let f = try!(File::open(file)); + let mut file = BufReader::new(&f); + for line in file.lines() { + let line = line.unwrap(); + let split: Vec<&str> = line.split_whitespace().collect(); + + // if email is in alias file + // return true + + if split[1].starts_with(alias_parts[1]) { + println!("booya"); + } + } + + Ok(()) +} + fn main() { let stdin = io::stdin(); let input: Vec<String> = stdin.lock().lines().map(|line| line.unwrap()).collect(); diff --git a/src/tests.rs b/src/tests.rs index 2e43d35..712b41e 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,4 @@ -use super::build_alias; +use super::{build_alias, is_alias_in_file}; #[test] fn build_alias_with_only_email() { @@ -39,3 +39,12 @@ fn build_alias_with_special_characters() { build_alias("From: \"O'Strulson, Celty\" <celty@dollars.co>") ); } + + +#[test] +fn is_alias_in_file_finds_a_match() { + is_alias_in_file( + "alias farnsworth-hubert Hubert Farnsworth <professor@planetexpress.com>", + "./testdata/aliases" + ); +} diff --git a/testdata/aliases b/testdata/aliases new file mode 100644 index 0000000..a8a08a8 --- /dev/null +++ b/testdata/aliases @@ -0,0 +1 @@ +alias farnsworth-hubert Hubert Farnsworth <professor@planetexpress.com> |
