use crate::{test_generators::InsideFunc, InputBuild, InputBuilder};
use std::{error::Error, fmt::Display, str::FromStr};
pub fn valid_input<T, F>(test: F) -> T
where
T: FromStr,
F: Fn(&T) -> bool + 'static,
{
input().add_test(test).get()
}
pub fn input_inside<T, U>(constraint: U) -> T
where
T: FromStr,
U: InsideFunc<T>,
{
input().inside(constraint).get()
}
pub fn simple_input<T: FromStr>() -> T {
input().get()
}
pub fn input<T: FromStr>() -> InputBuilder<T> {
InputBuilder::new()
}
pub fn input_d<T: DefaultBuilderSettings>() -> InputBuilder<T> {
T::settings()
}
pub trait DefaultBuilderSettings: FromStr {
fn settings() -> InputBuilder<Self>;
}
impl DefaultBuilderSettings for bool {
fn settings() -> InputBuilder<Self> {
input()
.repeat_msg("Please input true or false: ")
.err("Only type true or false.")
}
}
impl DefaultBuilderSettings for char {
fn settings() -> InputBuilder<Self> {
input()
.repeat_msg("Please input a character: ")
.err("Only type a single character.")
}
}
macro_rules! impl_default_builder_for_int {
($($t:ty),*) => {$(
impl DefaultBuilderSettings for $t {
fn settings() -> InputBuilder<Self> {
input()
.repeat_msg("Please input an integer: ")
.err("Only type integers.")
}
}
)*}
}
impl_default_builder_for_int! { i8, i16, i32, i64, i128, isize }
macro_rules! impl_default_builder_for_whole {
($($t:ty),*) => {$(
impl DefaultBuilderSettings for $t {
fn settings() -> InputBuilder<Self> {
input()
.repeat_msg("Please input a positive integer: ")
.err("Only type positive integers.")
}
}
)*}
}
impl_default_builder_for_whole! { u8, u16, u32, u64, u128, usize }
macro_rules! impl_default_builder_for_float {
($($t:ty),*) => {$(
impl DefaultBuilderSettings for $t {
fn settings() -> InputBuilder<Self> {
input()
.repeat_msg("Please input a number: ")
.err("Only type numbers or decimal point.")
}
}
)*}
}
impl_default_builder_for_float! { f32, f64 }
pub fn with_display<T: Display>(x: &T) -> Option<String> {
Some(format!("Error: \"{}\"", x))
}
#[deprecated(
since = "0.8.4",
note = "Deprecated due to the depreciation of `std::error::Error::description`. Please use the `with_display` function instead."
)]
#[allow(deprecated)]
pub fn with_description<T: Error>(x: &T) -> Option<String> {
Some(format!("Error: \"{}\"", (*x).description()))
}