From b182ea18dd664bc36e56601635ceb5ffdd67dc69 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 17 Apr 2016 21:35:49 -0400 Subject: Add a new `Alias` struct and implementation This allows us to represent an alias not just as a string, but as an entity where we can ask for and isolate different parts of the alias. This makes it more convenient to get just the "alias" part, or just the "email" part for example. Doing so is necessary for making accurate comparisons/searches to find out whether this email or alias already exists in our alias file. Two functions are implemented on this type. One, `to_string`, creates a Mutt alias line as a string. The other builds a new `Alias` object from a "From: " string, taking nearly the same steps as `build_alias` does. We'll want to transition `build_alias` to this `Alias::new` function, using the new one in the future. --- src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 5e02b38..27b3e48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,48 @@ use std::io::{self, BufRead}; #[cfg(test)] mod tests; +struct Alias { + alias: String, + name: String, + email: String, +} + +impl Alias { + fn new(email: &str) -> Alias { + let mut split: Vec<&str> = email.split_whitespace().collect(); + + // Remove "From: " + split.remove(0); + + let mut alias = String::new(); + let mut name = String::new(); + let mut email = String::new(); + + if split.len() == 1 { + alias = split[0].to_lowercase().to_string(); + email = split[0].to_string(); + } else if split.len() == 2 { + alias = split[0].to_lowercase().to_string(); + name = split[0].to_string(); + email = split[1].to_string(); + } else if split.len() > 2 { + alias = format!("{}-{}", split[split.len() - 2], split[0]).to_lowercase().to_string(); + name = split[0..(split.len() - 1)].join(" "); + email = split[split.len() - 1].to_string(); + } + + alias = alias.replace(',', ""); + alias = alias.replace('\'', ""); + alias = alias.replace('"', ""); + + Alias { alias: alias, name: name, email: email } + } + + fn to_string(&self) -> String { + format!("alias {} {} {}", self.alias, self.name, self.email) + } +} + fn handle_alias(s: &str) { let alias = build_alias(s); } -- cgit v1.2.3 From e6b919bd86f4f3a07aae8625b2d4c53d34cfaae3 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 18 Apr 2016 03:31:50 -0400 Subject: Alias#to_string: Don't format `name` if it's empty When we get a "From: " address that doesn't contain a name, the resulting `Alias` object created by `Alias::new` will have an empty name. When `to_string` is called on these kinds of objects, an extra space appears between the alias and the email address because of how the format string is constructed. This caused a test failure for the `new_alias_with_only_email` test method in fc05eb9e2f111642dbec093218b7184f25740b90. Our change here fixes and passes this test. --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 27b3e48..49c30d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,11 @@ impl Alias { } fn to_string(&self) -> String { - format!("alias {} {} {}", self.alias, self.name, self.email) + if self.name.is_empty() { + format!("alias {} {}", self.alias, self.email) + } else { + format!("alias {} {} {}", self.alias, self.name, self.email) + } } } -- cgit v1.2.3