diff options
| author | Teddy Wing | 2016-04-24 05:17:19 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-04-24 05:17:19 -0400 |
| commit | 2d1f7031f03194fbceffc15b1d6376abea243e22 (patch) | |
| tree | 29481b3837f6363f6405ac15e82b07b4cb89f773 /src | |
| parent | c67fdff0c3cda883b5bf577988e1c555931969cf (diff) | |
| download | mutt-alias-auto-add-2d1f7031f03194fbceffc15b1d6376abea243e22.tar.bz2 | |
Move `write_alias` function to `Alias#write_to_file`
Expand the responsibility of `Alias#write_to_file` so that it performs
what it used to do plus all of what `write_alias` did.
This allows us to consolidate the functionality into a single method,
and move it into the `Alias` implementation.
The change required some modification to our `write_to_file` test:
* Create our test alias as mutable
* Write a new alias to the test file based on our test alias but with a
different email. This allows the `write_to_file` method to work
without erroring with an `AliasSearchError::NotFound`.
* Needed to `derive(Clone)` on `Alias` in order to be able to easily
clone it into the new near-duplicate alias.
* Change our `unwrap()` calls to `expect()` to make it easier to see
where exactly we panicked. Otherwise I didn't really have any way of
knowing.
* Add some comments for clarity
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 18 | ||||
| -rw-r--r-- | src/tests.rs | 28 |
2 files changed, 28 insertions, 18 deletions
diff --git a/src/main.rs b/src/main.rs index 0004cc4..91545bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::path::Path; #[cfg(test)] mod tests; +#[derive(Clone)] struct Alias { alias: String, name: String, @@ -77,9 +78,13 @@ impl Alias { } } - fn write_to_file<P: AsRef<Path>>(&self, file: P) -> Result<(), io::Error> { + fn write_to_file<P: AsRef<Path>>(&mut self, file: P) -> Result<(), AliasSearchError> { + let similar_aliases = try!(self.find_in_file(&file)); + self.update_alias_id(similar_aliases); + let mut f = try!(OpenOptions::new().append(true).open(file)); try!(f.write_all(format!("{}\n", self.to_string()).as_bytes())); + Ok(()) } @@ -90,14 +95,6 @@ impl Alias { } } -fn write_alias<P: AsRef<Path>>(from: String, file: P) -> Result<(), AliasSearchError> { - let mut alias = Alias::new(&from); - let similar_aliases = try!(alias.find_in_file(&file)); - alias.update_alias_id(similar_aliases); - try!(alias.write_to_file(&file)); - Ok(()) -} - #[derive(Debug)] enum AliasSearchError { NotFound, @@ -182,7 +179,8 @@ fn main() { println!("{}", line); if line.starts_with("From: ") { - match write_alias(line, &file) { + let mut alias = Alias::new(&line); + match alias.write_to_file(&file) { Ok(_) => continue, Err(e @ AliasSearchError::NotFound) | Err(e @ AliasSearchError::EmailExists) => io::stderr().write(e.to_string().as_bytes()).ok(), diff --git a/src/tests.rs b/src/tests.rs index 88f2a3b..21cf147 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,5 +1,5 @@ -use std::fs::{self, File}; -use std::io::Read; +use std::fs::{self, File, OpenOptions}; +use std::io::{Read, Write}; use super::{Alias, AliasSearchError}; @@ -124,17 +124,29 @@ fn update_alias_id_increments_alias() { #[test] fn alias_write_to_file_must_write_given_alias_to_file() { - let alias = update_alias_id_sample_alias(); + let mut alias = update_alias_id_sample_alias(); + // Create a new test file let test_file = "./testdata/write_to_file"; - fs::copy("./testdata/aliases", test_file).unwrap(); - alias.write_to_file(test_file).unwrap(); + fs::copy("./testdata/aliases", test_file).expect("Alias file copy failed"); + + // Write a duplicate alias so that `write_to_file` is able to append a + // new one + let mut f = OpenOptions::new().append(true).open(test_file) + .expect("Failed to open test file for appending"); + writeln!(f, "{}", Alias { email: "derpy@home.pv".to_owned(), .. alias.clone() } + .to_string()) + .expect("Failed to append matching alias"); + + // Write our new alias to the file + alias.write_to_file(test_file).expect("`write_to_file` failed"); - let mut f = File::open(test_file).unwrap(); + // Get the file's contents for testing + let mut f = File::open(test_file).expect("Failed to open test file"); let mut file_contents = String::new(); - f.read_to_string(&mut file_contents).unwrap(); + f.read_to_string(&mut file_contents).expect("Failed to read test file contents"); let file_contents: Vec<&str> = file_contents.split('\n').collect(); - fs::remove_file(test_file).unwrap(); + fs::remove_file(test_file).expect("Failed to delete test file"); assert_eq!(alias.to_string(), file_contents[file_contents.len() - 2]); } |
