mod tests;
use std::any::TypeId;
use std::fmt::Display;
use std::io::stdin;
use std::io::stdout;
use std::io::BufRead;
use std::io::Write;
use std::str::FromStr;
pub fn once<T>() -> Result<T, <T as FromStr>::Err>
where
T: FromStr + 'static,
{
let mut stdin = stdin().lock();
let mut buf: String = String::new();
while stdin.read_line(&mut buf).is_err() {
println!("Failed to read line. Please try again.");
}
if TypeId::of::<T>() != TypeId::of::<String>() {
buf = buf.trim().to_string();
}
buf.parse::<T>()
}
pub fn until_ok<T>() -> T
where
T: FromStr + 'static,
{
loop {
match once::<T>() {
Ok(value) => return value,
Err(_) => {
println!("Failed to parse. Please input a correct data type.");
continue;
}
}
}
}
pub fn prompt<T>(prompt: &(impl ToString + Display + ?Sized)) -> Result<T, <T as FromStr>::Err>
where
T: FromStr + 'static,
{
let mut stdout = stdout().lock();
print!("{}", prompt);
stdout.flush().expect("Failed to flush stdout");
once()
}
pub fn prompt_until_ok<T>(prompt: &(impl ToString + Display + ?Sized)) -> T
where
T: FromStr + 'static,
{
let mut stdout = stdout().lock();
loop {
print!("{}", prompt);
stdout.flush().expect("Failed to flush stdout");
match once::<T>() {
Ok(value) => return value,
Err(_) => {
println!("Failed to parse. Please input a correct data type.");
continue;
}
}
};
}
#[deprecated(since = "1.1.0", note = "Use `until_ok` instead.")]
pub fn read_stdin_until_ok<T>() -> T
where
T: FromStr + 'static,
{
until_ok()
}
#[deprecated(since = "1.1.0", note = "Use `once` instead.")]
pub fn read_stdin<T>() -> Result<T, <T as FromStr>::Err>
where
T: FromStr + 'static,
{
once()
}