0.2.0 improved error reporting
This commit is contained in:
parent
3646593258
commit
8188c31dbb
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -288,7 +288,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "vote-counter"
|
name = "vote-counter"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"colored",
|
"colored",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "vote-counter"
|
name = "vote-counter"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "An opinionated single transferrable vote counter for the command line."
|
description = "An opinionated single transferrable vote counter for the command line."
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
25
src/main.rs
25
src/main.rs
@ -11,14 +11,28 @@ use std::process;
|
|||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
/// Adjusts threshold to be within permitted range, warning the user.
|
||||||
|
fn adjust_threshold(threshold : f64) -> f64 {
|
||||||
|
reporting::threshold_squash(threshold);
|
||||||
|
if threshold < 0.0 {
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
else if threshold > 1.0 {
|
||||||
|
1.0
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
threshold
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[clap(author, version)]
|
#[clap(author, about, version)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Path to the CSV containing the ballots.
|
/// Path to the CSV containing the ballots.
|
||||||
#[clap()]
|
#[clap()]
|
||||||
path : path::PathBuf,
|
path : path::PathBuf,
|
||||||
|
|
||||||
/// Threshold to win.
|
/// Threshold to win (from 0.0 to 1.0).
|
||||||
#[clap(long, short, default_value = "0.5")]
|
#[clap(long, short, default_value = "0.5")]
|
||||||
threshold : f64,
|
threshold : f64,
|
||||||
|
|
||||||
@ -28,7 +42,9 @@ struct Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Primary entry point to vote counting algorithms.
|
/// Primary entry point to vote counting algorithms.
|
||||||
fn count(args : Args) -> Result<(), csv::Error> {
|
fn count(mut args : Args) -> Result<(), csv::Error> {
|
||||||
|
|
||||||
|
args.threshold = adjust_threshold(args.threshold);
|
||||||
|
|
||||||
let mut ballot_box = BallotBox::from_file(&args.path, args.report)?;
|
let mut ballot_box = BallotBox::from_file(&args.path, args.report)?;
|
||||||
|
|
||||||
@ -48,12 +64,13 @@ fn count(args : Args) -> Result<(), csv::Error> {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
match count(args) {
|
match count(args) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
process::exit(exitcode::OK);
|
process::exit(exitcode::OK);
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
println!("An error occured reading the CSV data: {}", error);
|
reporting::csv_error(error);
|
||||||
process::exit(exitcode::DATAERR);
|
process::exit(exitcode::DATAERR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,3 +60,18 @@ pub fn winner(winner : Option<usize>, candidates : &Candidates) {
|
|||||||
None => println!("{}", "The election was a tie".bright_blue()),
|
None => println!("{}", "The election was a tie".bright_blue()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Notifies the user if the threshold was adjusted.
|
||||||
|
pub fn threshold_squash(prev_threshold : f64) {
|
||||||
|
if prev_threshold < 0.0 {
|
||||||
|
println!("{} Threshold was below the allowed range, and set to 0", "Warning:".yellow().bold())
|
||||||
|
}
|
||||||
|
else if prev_threshold > 1.0 {
|
||||||
|
println!("{} Threshold was above the allowed range, and set to 1", "Warning:".yellow().bold())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Displays a CSV error.
|
||||||
|
pub fn csv_error(error : csv::Error) {
|
||||||
|
println!("{} {}", "CSV Error:".red().bold(), error);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user