clippy suggestions
This commit is contained in:
parent
4050aeb2dd
commit
203f4e12d5
1 changed files with 13 additions and 14 deletions
27
src/main.rs
27
src/main.rs
|
@ -2,30 +2,29 @@ use std::io::{self};
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
fn main() {
|
||||
let redirects = parse_alias_to_BTreeMap();
|
||||
let redirects = parse_alias_to_map();
|
||||
let sieve_script = generate_sieve_script(redirects);
|
||||
println!("{}", sieve_script);
|
||||
}
|
||||
|
||||
fn generate_sieve_script(redirects: BTreeMap<String, Vec<String>>) -> String {
|
||||
let mut script : String = "require [\"envelope\", \"copy\"];\n".to_string();
|
||||
let mut script : String = "require [\"envelope\", \"copy\"];\n\n".to_string();
|
||||
for (redirect, mut destinations) in redirects {
|
||||
script += format!("if envelope :is \"to\" \"{}\" {{\n{}}}\n", redirect,
|
||||
(|| {
|
||||
{
|
||||
let mut subscript : String = "".to_string();
|
||||
destinations.sort();
|
||||
for destination in destinations.iter().rev().skip(1).rev() {
|
||||
subscript += format!(" redirect :copy \"{}\";\n", destination).as_str();
|
||||
}
|
||||
subscript += format!(" redirect \"{}\";\n", destinations.iter().rev().next().unwrap()).as_str();
|
||||
return subscript;
|
||||
})()
|
||||
subscript + format!(" redirect \"{}\";\n", destinations.iter().next_back().unwrap()).as_str()
|
||||
}
|
||||
).as_str();
|
||||
}
|
||||
return script;
|
||||
script
|
||||
}
|
||||
|
||||
fn parse_alias_to_BTreeMap() -> BTreeMap<String, Vec<String>> {
|
||||
fn parse_alias_to_map() -> BTreeMap<String, Vec<String>> {
|
||||
// File must exist in the current path
|
||||
let mut redirect_map : BTreeMap<String, Vec<String>> = BTreeMap::new();
|
||||
let mut destinations : Vec<String> = Vec::new();
|
||||
|
@ -33,12 +32,12 @@ fn parse_alias_to_BTreeMap() -> BTreeMap<String, Vec<String>> {
|
|||
let line = line.unwrap();
|
||||
let line = String::from(line.split_at(line.find("#").unwrap_or(line.len())).0);
|
||||
let destination = line.split_at(line.find(char::is_whitespace).unwrap_or(0)).0;
|
||||
if destination == "" {
|
||||
if destination.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let redirects: Vec<String> = line.split_at(line.find(char::is_whitespace).unwrap_or(0)).1.split(" ")
|
||||
.filter(|address| address.trim().to_string().replace(",","") != "").map(|address| to_mailaddress(address)).collect();
|
||||
if redirects.len() == 0 {
|
||||
.filter(|address| address.trim().to_string().replace(",","") != "").map(to_mailaddress).collect();
|
||||
if redirects.is_empty() {
|
||||
continue;
|
||||
}
|
||||
destinations.push(to_mailaddress(destination));
|
||||
|
@ -54,7 +53,7 @@ fn parse_alias_to_BTreeMap() -> BTreeMap<String, Vec<String>> {
|
|||
changed = true;
|
||||
all_new_redirects.entry(destination.clone()).or_insert(redirect_map.get(destination).unwrap().clone())
|
||||
.retain(|dest| *dest != *forward_to);
|
||||
all_new_redirects.entry(destination.clone()).and_modify(|d| d.extend(new_redirects.iter().map(|x| x.clone())));
|
||||
all_new_redirects.entry(destination.clone()).and_modify(|d| d.extend(new_redirects.iter().cloned()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +61,7 @@ fn parse_alias_to_BTreeMap() -> BTreeMap<String, Vec<String>> {
|
|||
*redirect_map.get_mut(&destination).unwrap() = new_redirect;
|
||||
}
|
||||
}
|
||||
return redirect_map;
|
||||
redirect_map
|
||||
}
|
||||
|
||||
fn to_mailaddress(local_part: &str) -> String {
|
||||
|
@ -71,5 +70,5 @@ fn to_mailaddress(local_part: &str) -> String {
|
|||
if addr.contains("@") {
|
||||
return addr;
|
||||
}
|
||||
return addr + "@mathebau.de";
|
||||
addr + "@mathebau.de"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue