Clippy suggestions

This commit is contained in:
Gonne 2024-11-25 20:31:28 +01:00
parent bbd936cf88
commit defa39de28

View file

@ -4,7 +4,7 @@ use std::fs::File;
use std::io::{self, BufRead}; use std::io::{self, BufRead};
use std::path::Path; use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
use fqdn::*; use fqdn::FQDN;
use email_address_parser::EmailAddress; use email_address_parser::EmailAddress;
#[derive(Debug)] #[derive(Debug)]
@ -24,13 +24,10 @@ fn main() {
let mut alias_files : Vec<AliasFile> = Vec::new(); let mut alias_files : Vec<AliasFile> = Vec::new();
for i in (1..args.len()).step_by(2) { for i in (1..args.len()).step_by(2) {
if let Ok(lines) = read_lines(&args[i]) { if let Ok(lines) = read_lines(&args[i]) {
alias_files.push(AliasFile{content: lines, default_domain: FQDN::from_str(&args[i+1]).unwrap()}) alias_files.push(AliasFile{content: lines, default_domain: FQDN::from_str(&args[i+1]).unwrap()});
} }
} }
println!("{}", generate_sieve_script(parse_alias_to_map(alias_files)));
let redirects = parse_alias_to_map(alias_files);
let sieve_script = generate_sieve_script(redirects);
println!("{}", sieve_script);
} }
fn generate_sieve_script(redirects: AliasMap) -> String { fn generate_sieve_script(redirects: AliasMap) -> String {
@ -38,9 +35,9 @@ fn generate_sieve_script(redirects: AliasMap) -> String {
for (redirect, destinations) in redirects { for (redirect, destinations) in redirects {
script += format!("if envelope :is \"to\" \"{}\" {{\n{}}}\n", redirect, script += format!("if envelope :is \"to\" \"{}\" {{\n{}}}\n", redirect,
{ {
let mut subscript : String = "".to_string(); let mut subscript : String = String::new();
for destination in destinations.iter().rev().skip(1).rev() { for destination in destinations.iter().rev().skip(1).rev() {
subscript += format!(" redirect :copy \"{}\";\n", destination).as_str(); subscript += format!(" redirect :copy \"{destination}\";\n").as_str();
} }
subscript + format!(" redirect \"{}\";\n", destinations.iter().next_back().unwrap()).as_str() subscript + format!(" redirect \"{}\";\n", destinations.iter().next_back().unwrap()).as_str()
} }
@ -53,16 +50,16 @@ fn parse_alias_to_map(alias_files: Vec<AliasFile>) -> AliasMap {
// File must exist in the current path // File must exist in the current path
let mut redirect_map : AliasMap = AliasMap::new(); let mut redirect_map : AliasMap = AliasMap::new();
let mut destinations : Vec<EmailAddress> = Vec::new(); let mut destinations : Vec<EmailAddress> = Vec::new();
for alias_file in alias_files.into_iter() { for alias_file in alias_files {
for line in alias_file.content { for line in alias_file.content {
let line = line.unwrap(); let line = line.unwrap();
let line = String::from(line.split_at(line.find("#").unwrap_or(line.len())).0); 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; let destination = line.split_at(line.find(char::is_whitespace).unwrap_or(0)).0;
if destination.is_empty() { if destination.is_empty() {
continue; continue;
} }
let redirects: Vec<EmailAddress> = line.split_at(line.find(char::is_whitespace).unwrap_or(0)).1.split(" ") let redirects: Vec<EmailAddress> = line.split_at(line.find(char::is_whitespace).unwrap_or(0)).1.split(' ')
.filter(|address| address.trim().to_string().replace(",","") != "").map(|addr| to_mailaddress(addr, &alias_file.default_domain)).collect(); .filter(|address| address.trim().to_string().replace(',',"") != "").map(|addr| to_mailaddress(addr, &alias_file.default_domain)).collect();
if redirects.is_empty() { if redirects.is_empty() {
continue; continue;
} }
@ -74,8 +71,8 @@ fn parse_alias_to_map(alias_files: Vec<AliasFile>) -> AliasMap {
while changed { while changed {
changed = false; changed = false;
let mut all_new_redirects : AliasMap = AliasMap::new(); let mut all_new_redirects : AliasMap = AliasMap::new();
for destination in destinations.iter() { for destination in &destinations {
for forward_to in redirect_map.get(destination).unwrap().iter() { for forward_to in redirect_map.get(destination).unwrap() {
if let Some(new_redirects) = redirect_map.get(forward_to) { if let Some(new_redirects) = redirect_map.get(forward_to) {
changed = true; changed = true;
all_new_redirects.entry(destination.clone()).or_insert(redirect_map.get(destination).unwrap().clone()) all_new_redirects.entry(destination.clone()).or_insert(redirect_map.get(destination).unwrap().clone())
@ -93,8 +90,8 @@ fn parse_alias_to_map(alias_files: Vec<AliasFile>) -> AliasMap {
fn to_mailaddress(local_part: &str, default_domain : &FQDN) -> EmailAddress { fn to_mailaddress(local_part: &str, default_domain : &FQDN) -> EmailAddress {
let mut addr = local_part.trim().to_string(); let mut addr = local_part.trim().to_string();
addr = addr.replace(",", ""); addr = addr.replace(',', "");
if addr.contains("@") { if addr.contains('@') {
return EmailAddress::parse(&addr, None).unwrap(); return EmailAddress::parse(&addr, None).unwrap();
} }
EmailAddress::new(&addr, &default_domain.to_string(), None).unwrap() EmailAddress::new(&addr, &default_domain.to_string(), None).unwrap()