diff options
| author | Teddy Wing | 2016-04-06 12:39:16 -0400 | 
|---|---|---|
| committer | Teddy Wing | 2016-04-06 12:39:16 -0400 | 
| commit | cad8246ab54f7c81b70d1445ccac356bc4b06d02 (patch) | |
| tree | abf70f99de2910f719486a582df34004181fb9a9 /src/main.rs | |
| parent | 02688030cb7b1de5c63732b8b820017391bfd2a0 (diff) | |
| download | mutt-alias-auto-add-cad8246ab54f7c81b70d1445ccac356bc4b06d02.tar.bz2 | |
Implement `build_alias`
Copy of the functionality in W. Caleb McDaniel's Bash script implemented
in Rust:
    echo "${MESSAGE}" | grep ^"From: " | sed s/[\,\"\']//g | awk '{$1=""; if (NF == 3) {print "alias" $0;} else if (NF == 2) {print "alias" $0 $0;} else if (NF > 3) {print "alias", tolower($(NF-1))"-"tolower($2) $0;}}'
Doesn't currently include the [,"'] filtering.
Added a couple of new test cases to cover other address formats.
Not sure if I should use `if..else if` here or a `match`. I feel like I
should return an empty string at the end if we can't build an alias
line.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 22 | 
1 files changed, 21 insertions, 1 deletions
| diff --git a/src/main.rs b/src/main.rs index 92ea8e1..2a0a471 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,27 @@ fn handle_alias(s: &str) {  }  fn build_alias(s: &str) -> String { -    String::from("String") +    // Replace , ' " with nothing + +    let mut split: Vec<&str> = s.split_whitespace().collect(); + +    // Remove "From: " +    split.remove(0); + +    let mut alias_line = String::from("alias "); + +    if split.len() == 1 { +        alias_line.push_str(&format!("{} ", split[0].to_lowercase())); +        alias_line.push_str(&split.join(" ")) +    } else if split.len() == 2 { +        alias_line.push_str(&format!("{} ", split[0].to_lowercase())); +        alias_line.push_str(&split.join(" ")); +    } else if split.len() > 2 { +        alias_line.push_str(&format!("{}-{} ", split[split.len() - 2], split[0]).to_lowercase()); +        alias_line.push_str(&split.join(" ")); +    } + +    alias_line  }  fn main() { | 
