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
let election: Election<Rational>;
let mut election: Election<Rational>;
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");

View File

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