From 4050aeb2dd4d926983b856828873b8c88d18c22f Mon Sep 17 00:00:00 2001 From: Gonne Date: Sun, 3 Nov 2024 09:28:48 +0100 Subject: [PATCH] sort consistently --- src/main.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index cd31b7a..b2242bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { +fn generate_sieve_script(redirects: BTreeMap>) -> 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 { return script; } -fn parse_alias_to_hashmap() -> HashMap> { +fn parse_alias_to_BTreeMap() -> BTreeMap> { // File must exist in the current path - let mut redirect_map : HashMap> = HashMap::new(); + let mut redirect_map : BTreeMap> = BTreeMap::new(); let mut destinations : Vec = Vec::new(); for line in io::stdin().lines() { let line = line.unwrap(); @@ -46,7 +47,7 @@ fn parse_alias_to_hashmap() -> HashMap> { let mut changed = true; while changed { changed = false; - let mut all_new_redirects : HashMap> = HashMap::new(); + let mut all_new_redirects : BTreeMap> = 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) {