sort consistently

This commit is contained in:
Gonne 2024-11-03 09:28:48 +01:00
parent 14112acea3
commit 4050aeb2dd

View file

@ -1,18 +1,19 @@
use std::io::{self};
use std::collections::HashMap;
use std::collections::BTreeMap;
fn main() {
let redirects = parse_alias_to_hashmap();
let redirects = parse_alias_to_BTreeMap();
let sieve_script = generate_sieve_script(redirects);
println!("{}", sieve_script);
}
fn generate_sieve_script(redirects: HashMap<String, Vec<String>>) -> String {
fn generate_sieve_script(redirects: BTreeMap<String, Vec<String>>) -> String {
let mut script : String = "require [\"envelope\", \"copy\"];\n".to_string();
for (redirect, destinations) in redirects {
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();
}
@ -24,9 +25,9 @@ fn generate_sieve_script(redirects: HashMap<String, Vec<String>>) -> String {
return script;
}
fn parse_alias_to_hashmap() -> HashMap<String, Vec<String>> {
fn parse_alias_to_BTreeMap() -> BTreeMap<String, Vec<String>> {
// File must exist in the current path
let mut redirect_map : HashMap<String, Vec<String>> = HashMap::new();
let mut redirect_map : BTreeMap<String, Vec<String>> = BTreeMap::new();
let mut destinations : Vec<String> = Vec::new();
for line in io::stdin().lines() {
let line = line.unwrap();
@ -46,7 +47,7 @@ fn parse_alias_to_hashmap() -> HashMap<String, Vec<String>> {
let mut changed = true;
while changed {
changed = false;
let mut all_new_redirects : HashMap<String, Vec<String>> = HashMap::new();
let mut all_new_redirects : BTreeMap<String, Vec<String>> = BTreeMap::new();
for destination in destinations.iter() {
for forward_to in redirect_map.get(destination).unwrap().iter() {
if let Some(new_redirects) = redirect_map.get(forward_to) {