diff options
| author | Teddy Wing | 2016-04-24 04:43:00 -0400 | 
|---|---|---|
| committer | Teddy Wing | 2016-04-24 04:43:00 -0400 | 
| commit | c67fdff0c3cda883b5bf577988e1c555931969cf (patch) | |
| tree | ac421a2ffecf10b6561378234b7b36d745e6e62c /src/main.rs | |
| parent | 5059d4028035bc54849bf99dc3546363f5f28476 (diff) | |
| download | mutt-alias-auto-add-c67fdff0c3cda883b5bf577988e1c555931969cf.tar.bz2 | |
Move `find_alias_in_file` function to `Alias#find_in_file`
Makes more sense for this function to live in a method on `Alias`
because it operates directly on an alias.
Refactor our tests and code to support this new organisation.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 50 | 
1 files changed, 25 insertions, 25 deletions
| diff --git a/src/main.rs b/src/main.rs index 75e74ac..0004cc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,6 +53,30 @@ impl Alias {          }      } +    fn find_in_file<P: AsRef<Path>>(&self, file: P) -> Result<Vec<String>, AliasSearchError> { +        let mut matches = Vec::new(); +        let f = try!(File::open(file)); +        let file = BufReader::new(&f); +        for line in file.lines() { +            let line = try!(line); +            let split: Vec<&str> = line.split_whitespace().collect(); + +            if line.contains(&self.email) { +                return Err(AliasSearchError::EmailExists) +            } + +            if split[1].starts_with(&self.alias) { +                matches.push(split[1].to_owned()); +            } +        } + +        if matches.is_empty() { +            Err(AliasSearchError::NotFound) +        } else { +            Ok(matches) +        } +    } +      fn write_to_file<P: AsRef<Path>>(&self, file: P) -> Result<(), io::Error> {          let mut f = try!(OpenOptions::new().append(true).open(file));          try!(f.write_all(format!("{}\n", self.to_string()).as_bytes())); @@ -68,7 +92,7 @@ impl Alias {  fn write_alias<P: AsRef<Path>>(from: String, file: P) -> Result<(), AliasSearchError> {      let mut alias = Alias::new(&from); -    let similar_aliases = try!(find_alias_in_file(&alias, &file)); +    let similar_aliases = try!(alias.find_in_file(&file));      alias.update_alias_id(similar_aliases);      try!(alias.write_to_file(&file));      Ok(()) @@ -134,30 +158,6 @@ impl PartialEq<AliasSearchError> for AliasSearchError {      }  } -fn find_alias_in_file<P: AsRef<Path>>(alias: &Alias, file: P) -> Result<Vec<String>, AliasSearchError> { -    let mut matches = Vec::new(); -    let f = try!(File::open(file)); -    let file = BufReader::new(&f); -    for line in file.lines() { -        let line = try!(line); -        let split: Vec<&str> = line.split_whitespace().collect(); - -        if line.contains(&alias.email) { -            return Err(AliasSearchError::EmailExists) -        } - -        if split[1].starts_with(&alias.alias) { -            matches.push(split[1].to_owned()); -        } -    } - -    if matches.is_empty() { -        Err(AliasSearchError::NotFound) -    } else { -        Ok(matches) -    } -} -  fn print_usage(program: &str) {      println!("Usage: {} FILE", program);  } | 
