convert: Allow --seats to override input file

This commit is contained in:
RunasSudo 2021-08-21 01:19:54 +10:00
parent 88ab06d633
commit 61e4eefca3
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 17 additions and 13 deletions

View File

@ -74,7 +74,7 @@ pub fn main(mut cmd_opts: SubcmdOptions) -> Result<(), i32> {
} }
// Read input file // Read input file
let election: Election<Rational>; let mut election: Election<Rational>;
match cmd_opts.r#in.as_deref().unwrap() { match cmd_opts.r#in.as_deref().unwrap() {
"blt" => { "blt" => {
@ -89,19 +89,23 @@ pub fn main(mut cmd_opts: SubcmdOptions) -> Result<(), i32> {
} }
} }
"csp" => { "csp" => {
let file = File::open(cmd_opts.infile).expect("IO Error");
election = parser::csp::parse_reader(file);
}
_ => unreachable!()
};
match cmd_opts.seats { match cmd_opts.seats {
Some(seats) => { Some(seats) => {
let file = File::open(cmd_opts.infile).expect("IO Error"); election.seats = seats;
election = parser::csp::parse_reader(file, seats);
} }
None => { None => {
if election.seats == 0 {
println!("Error: --seats must be specified with CSP input"); println!("Error: --seats must be specified with CSP input");
return Err(1); return Err(1);
} }
} }
} }
_ => unreachable!()
};
// Write output file // Write output file
let output = File::create(cmd_opts.outfile).expect("IO Error"); let output = File::create(cmd_opts.outfile).expect("IO Error");

View File

@ -24,7 +24,7 @@ use std::collections::HashMap;
use std::io::Read; use std::io::Read;
/// Parse the given CSP file /// Parse the given CSP file
pub fn parse_reader<R: Read, N: Number>(reader: R, seats: usize) -> Election<N> { pub fn parse_reader<R: Read, N: Number>(reader: R) -> Election<N> {
// Read CSV file // Read CSV file
let mut reader = ReaderBuilder::new() let mut reader = ReaderBuilder::new()
.has_headers(true) .has_headers(true)
@ -93,7 +93,7 @@ pub fn parse_reader<R: Read, N: Number>(reader: R, seats: usize) -> Election<N>
return Election { return Election {
name: String::new(), name: String::new(),
seats: seats, seats: 0,
candidates: candidates, candidates: candidates,
withdrawn_candidates: Vec::new(), withdrawn_candidates: Vec::new(),
ballots: ballots, ballots: ballots,