Handle errors for unparseable input (#326)
This commit is contained in:
parent
078a8cbc26
commit
9493bc97a3
|
@ -1,4 +1,4 @@
|
|||
use std::io;
|
||||
use std::io::BufRead;
|
||||
|
||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||
pub struct Config {
|
||||
|
@ -22,22 +22,33 @@ pub fn validate_inputs(config: &Config) -> Result<(), frost_ed25519::Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn request_inputs() -> Config {
|
||||
pub fn request_inputs(input: &mut impl BufRead) -> Result<Config, frost_ed25519::Error> {
|
||||
println!("The minimum number of signers: (2 or more)");
|
||||
|
||||
let mut min = String::new();
|
||||
io::stdin().read_line(&mut min).expect("Invalid input");
|
||||
input.read_line(&mut min).unwrap();
|
||||
|
||||
let min_signers = min.trim().parse::<u16>().expect("Invalid input");
|
||||
let min_signers = min.trim().parse::<u16>();
|
||||
match min_signers {
|
||||
Ok(_) => (),
|
||||
Err(_) => {
|
||||
return Err(frost_ed25519::Error::InvalidMinSigners);
|
||||
}
|
||||
}
|
||||
|
||||
println!("The maximum number of signers: ");
|
||||
|
||||
let mut max = String::new();
|
||||
io::stdin().read_line(&mut max).expect("Invalid input");
|
||||
let max_signers = max.trim().parse::<u16>().expect("Invalid input");
|
||||
|
||||
Config {
|
||||
min_signers,
|
||||
max_signers,
|
||||
input.read_line(&mut max).unwrap();
|
||||
let max_signers = max.trim().parse::<u16>();
|
||||
match max_signers {
|
||||
Ok(_) => (),
|
||||
Err(_) => {
|
||||
return Err(frost_ed25519::Error::InvalidMaxSigners);
|
||||
}
|
||||
}
|
||||
Ok(Config {
|
||||
min_signers: min_signers.unwrap(),
|
||||
max_signers: max_signers.unwrap(),
|
||||
})
|
||||
}
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -13,7 +13,18 @@ use crate::inputs::{request_inputs, validate_inputs};
|
|||
use crate::trusted_dealer_keygen::trusted_dealer_keygen;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let config = request_inputs();
|
||||
let mut reader = Box::new(io::stdin().lock());
|
||||
let config = request_inputs(&mut reader);
|
||||
match config {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(exitcode::DATAERR)
|
||||
}
|
||||
}
|
||||
|
||||
let config = config.unwrap();
|
||||
|
||||
let mut rng = thread_rng();
|
||||
|
||||
let valid = validate_inputs(&config);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::inputs::{validate_inputs, Config};
|
||||
use crate::inputs::{request_inputs, validate_inputs, Config};
|
||||
|
||||
#[test]
|
||||
fn check_valid_input_for_signers() {
|
||||
|
@ -47,3 +47,31 @@ fn return_error_if_max_participant_is_less_than_2() {
|
|||
|
||||
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_config_if_valid_input() {
|
||||
let mut valid_input = "3\n6\n".as_bytes();
|
||||
let config = request_inputs(&mut valid_input).unwrap();
|
||||
let expected = Config {
|
||||
min_signers: 3,
|
||||
max_signers: 6,
|
||||
};
|
||||
|
||||
assert_eq!(expected, config)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_error_if_invalid_min_signers_input() {
|
||||
let mut invalid_input = "hello\n6\n".as_bytes();
|
||||
let expected = request_inputs(&mut invalid_input);
|
||||
|
||||
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMinSigners))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_error_if_invalid_max_signers_input() {
|
||||
let mut invalid_input = "4\nworld\n".as_bytes();
|
||||
let expected = request_inputs(&mut invalid_input);
|
||||
|
||||
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue