From 61e4eefca37ccead5679012350e65e36199f5939 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 21 Aug 2021 01:19:54 +1000 Subject: [PATCH] convert: Allow --seats to override input file --- src/cli/convert.rs | 26 +++++++++++++++----------- src/parser/csp.rs | 4 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/cli/convert.rs b/src/cli/convert.rs index 2f71ff6..f9d7a18 100644 --- a/src/cli/convert.rs +++ b/src/cli/convert.rs @@ -74,7 +74,7 @@ pub fn main(mut cmd_opts: SubcmdOptions) -> Result<(), i32> { } // Read input file - let election: Election; + let mut election: Election; match cmd_opts.r#in.as_deref().unwrap() { "blt" => { @@ -89,20 +89,24 @@ pub fn main(mut cmd_opts: SubcmdOptions) -> Result<(), i32> { } } "csp" => { - match cmd_opts.seats { - Some(seats) => { - let file = File::open(cmd_opts.infile).expect("IO Error"); - election = parser::csp::parse_reader(file, seats); - } - None => { - println!("Error: --seats must be specified with CSP input"); - return Err(1); - } - } + let file = File::open(cmd_opts.infile).expect("IO Error"); + election = parser::csp::parse_reader(file); } _ => unreachable!() }; + match cmd_opts.seats { + Some(seats) => { + election.seats = seats; + } + None => { + if election.seats == 0 { + println!("Error: --seats must be specified with CSP input"); + return Err(1); + } + } + } + // Write output file let output = File::create(cmd_opts.outfile).expect("IO Error"); diff --git a/src/parser/csp.rs b/src/parser/csp.rs index 00e9de8..f72e997 100644 --- a/src/parser/csp.rs +++ b/src/parser/csp.rs @@ -24,7 +24,7 @@ use std::collections::HashMap; use std::io::Read; /// Parse the given CSP file -pub fn parse_reader(reader: R, seats: usize) -> Election { +pub fn parse_reader(reader: R) -> Election { // Read CSV file let mut reader = ReaderBuilder::new() .has_headers(true) @@ -93,7 +93,7 @@ pub fn parse_reader(reader: R, seats: usize) -> Election return Election { name: String::new(), - seats: seats, + seats: 0, candidates: candidates, withdrawn_candidates: Vec::new(), ballots: ballots,