/* OpenTally: Open-source election vote counting
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use opentally::stv;
use opentally::election::{Candidate, CandidateState, CountCard, CountState, CountStateOrRef, Election, StageResult};
use opentally::numbers::{NativeFloat64, Number, Rational};
use clap::{AppSettings, Clap};
use std::fs::File;
use std::io::{self, BufRead};
use std::ops;
/// Open-source election vote counting
#[derive(Clap)]
#[clap(name="OpenTally", version=opentally::VERSION)]
struct Opts {
#[clap(subcommand)]
command: Command,
}
#[derive(Clap)]
enum Command {
STV(STV),
}
/// Count a single transferable vote (STV) election
#[derive(Clap)]
#[clap(setting=AppSettings::DeriveDisplayOrder)]
struct STV {
// ----------------
// -- File input --
/// Path to the BLT file to be counted
filename: String,
// ----------------------
// -- Numbers settings --
/// Numbers mode
#[clap(help_heading=Some("NUMBERS"), short, long, possible_values=&["rational", "float64"], default_value="rational", value_name="mode")]
numbers: String,
// -----------------------
// -- Rounding settings --
/// Round transfer values to specified decimal places
#[clap(help_heading=Some("ROUNDING"), long, value_name="dps")]
round_tvs: Option,
/// Round ballot weights to specified decimal places
#[clap(help_heading=Some("ROUNDING"), long, value_name="dps")]
round_weights: Option,
/// Round votes to specified decimal places
#[clap(help_heading=Some("ROUNDING"), long, value_name="dps")]
round_votes: Option,
/// Round quota to specified decimal places
#[clap(help_heading=Some("ROUNDING"), long, value_name="dps")]
round_quota: Option,
// -----------
// -- Quota --
/// Quota type
#[clap(help_heading=Some("QUOTA"), short, long, possible_values=&["droop", "hare", "droop_exact", "hare_exact"], default_value="droop_exact")]
quota: String,
/// Whether to elect candidates on meeting (geq) or strictly exceeding (gt) the quota
#[clap(help_heading=Some("QUOTA"), short='c', long, possible_values=&["geq", "gt"], default_value="gt", value_name="criterion")]
quota_criterion: String,
// ------------------
// -- STV variants --
/// Method of surplus transfers
#[clap(help_heading=Some("STV VARIANTS"), short='s', long, possible_values=&["wig", "uig", "eg", "meek"], default_value="wig", value_name="method")]
surplus: String,
#[clap(help_heading=Some("STV VARIANTS"), long, possible_values=&["by_size", "by_order"], default_value="by_size", value_name="order")]
surplus_order: String,
/// Examine only transferable papers during surplus distributions
#[clap(help_heading=Some("STV VARIANTS"), long)]
transferable_only: bool,
/// Method of exclusions
#[clap(help_heading=Some("STV VARIANTS"), long, possible_values=&["single_stage", "by_value", "parcels_by_order"], default_value="single_stage", value_name="method")]
exclusion: String,
// ----------------------
// -- Display settings --
/// Hide excluded candidates from results report
#[clap(help_heading=Some("DISPLAY"), long)]
hide_excluded: bool,
/// Sort candidates by votes in results report
#[clap(help_heading=Some("DISPLAY"), long)]
sort_votes: bool,
/// Print votes to specified decimal places in results report
#[clap(help_heading=Some("DISPLAY"), long, default_value="2", value_name="dps")]
pp_decimals: usize,
}
fn main() {
// Read arguments
let opts: Opts = Opts::parse();
let Command::STV(cmd_opts) = opts.command;
// Read BLT file
let file = File::open(&cmd_opts.filename).expect("IO Error");
let lines = io::BufReader::new(file).lines();
// Create and count election according to --numbers
if cmd_opts.numbers == "rational" {
let election: Election = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter());
count_election(election, cmd_opts);
} else if cmd_opts.numbers == "float64" {
let election: Election = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter());
count_election(election, cmd_opts);
}
}
fn count_election(election: Election, cmd_opts: STV)
where
for<'r> &'r N: ops::Sub<&'r N, Output=N>,
for<'r> &'r N: ops::Div<&'r N, Output=N>,
for<'r> &'r N: ops::Neg