1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::{read_string, ReadData, ReadLine};



/// Allows you to take a bool input
pub struct BoolInput;

impl ReadLine for BoolInput {
	type Output = bool;
	fn try_read_line(&self, mut read_data: ReadData<Self::Output>) -> crate::BoxResult<Self::Output> {
		let mut prompt = read_data.prompt.unwrap_or(String::from("Enter a bool: "));
		if let Some(default) = read_data.default.as_ref() {
			prompt += &format!("(default: {default}) ");
		}
		loop {
			
			print!("{prompt}");
			let input = read_string(&mut read_data.input)?.to_lowercase();
			match (&*input, read_data.default) {
				("", Some(default)) => return Ok(default),
				("true", _) | ("t", _) => return Ok(true),
				("false", _) | ("f", _) => return Ok(false),
				(_, _) => println!("Invalid input."),
			}
			
		}
	}
}



/// Allows you to take a bool input
pub struct YesNoInput;

impl ReadLine for YesNoInput {
	type Output = bool;
	fn try_read_line(&self, mut read_data: ReadData<Self::Output>) -> crate::BoxResult<Self::Output> {
		let mut prompt = read_data.prompt.unwrap_or(String::from("Enter 'Yes' or 'No': "));
		if let Some(default) = read_data.default.as_ref() {
			prompt += &format!("(default: {}) ", if *default {"Yes"} else {"No"});
		}
		loop {
			
			print!("{prompt}");
			let input = read_string(&mut read_data.input)?.to_lowercase();
			match (&*input, read_data.default) {
				("", Some(default)) => return Ok(default),
				("yes", _) | ("y", _) => return Ok(true),
				("no", _) | ("n", _) => return Ok(false),
				(_, _) => println!("Invalid input."),
			}
			
		}
	}
}



macro_rules! implement_number_input {
	($type_name:tt, $type_base:ty, $default_prompt:expr) => {
		impl ReadLine for $type_name {
			type Output = $type_base;
			fn try_read_line(&self, mut read_data: ReadData<Self::Output>) -> crate::BoxResult<Self::Output> {
				let mut prompt = read_data.prompt.unwrap_or(String::from($default_prompt));
				if let Some(default) = read_data.default.as_ref() {
					prompt += &format!("(default: {default}) ");
				}
				loop {
					
					print!("{prompt}");
					let input_string = read_string(&mut read_data.input)?.to_lowercase();
					if input_string.is_empty() && let Some(default) = read_data.default {
						return Ok(default);
					}
					
					let Ok(input) = input_string.parse::<$type_base>() else {
						println!("Could not parse input.");
						continue;
					};
					return Ok(input);
					
				}
			}
		}
	};
}

/// Allows you take take a char input
pub struct CharInput;
implement_number_input!(CharInput, char, "Enter a character: ");

/// Allows you take take a usize input
pub struct UsizeInput;
implement_number_input!(UsizeInput, usize, "Enter an unsigned int: ");

/// Allows you take take an isize input
pub struct IsizeInput;
implement_number_input!(IsizeInput, isize, "Enter an int: ");

/// Allows you take take a u8 input
pub struct U8Input;
implement_number_input!(U8Input, u8, "Enter an int (u8): ");

/// Allows you take take an i8 input
pub struct I8Input;
implement_number_input!(I8Input, i8, "Enter an int (i8): ");

/// Allows you take take a u16 input
pub struct U16Input;
implement_number_input!(U16Input, u16, "Enter an int (u16): ");

/// Allows you take take an i16 input
pub struct I16Input;
implement_number_input!(I16Input, i16, "Enter an int (i16): ");

/// Allows you take take a u32 input
pub struct U32Input;
implement_number_input!(U32Input, u32, "Enter an int (u32): ");

/// Allows you take take an i32 input
pub struct I32Input;
implement_number_input!(I32Input, i32, "Enter an int (i32): ");

/// Allows you take take a u64 input
pub struct U64Input;
implement_number_input!(U64Input, u64, "Enter an int (u64): ");

/// Allows you take take an i64 input
pub struct I64Input;
implement_number_input!(I64Input, i64, "Enter an int (i64): ");

/// Allows you take take a u128 input
pub struct U128Input;
implement_number_input!(U128Input, u128, "Enter an int (u128): ");

/// Allows you take take an i128 input
pub struct I128Input;
implement_number_input!(I128Input, i128, "Enter an int (i128): ");

/// Allows you take take an f32 input
pub struct F32Input;
implement_number_input!(F32Input, f32, "Enter a number: ");

/// Allows you take take an f64 input
pub struct F64Input;
implement_number_input!(F64Input, f64, "Enter a number: ");