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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::*;



impl TryRead for () {
	type Output = String;
	fn try_read_line(&self, mut read_args: TryReadArgs<Self::Output>) -> BoxResult<Self::Output> {
		match (read_args.prompt, &read_args.default) {
			(Some(prompt), Some(default)) => print!("{prompt}(default: {default}) "),
			(None, Some(default)) => print!("(default: {default}) "),
			(Some(prompt), None) => print!("{prompt}"),
			(None, None) => {},
		}
		let output = read_string(&mut read_args.input)?;
		Ok(if output.is_empty() && let Some(default) = read_args.default {
			default
		} else {
			output
		})
	}
}



pub struct NonEmptyInput;

impl TryRead for NonEmptyInput {
	type Output = String;
	fn try_read_line(&self, mut read_args: TryReadArgs<String>) -> BoxResult<Self::Output> {
		let mut prompt = read_args.prompt.unwrap_or(String::from("Enter a bool: "));
		if let Some(default) = read_args.default.as_ref() {
			prompt += &format!("(default: {default}) ");
		}
		loop {
			
			print!("{prompt}");
			let input = read_string(&mut read_args.input)?.to_lowercase();
			if input.is_empty() {println!("Invalid input."); continue;}
			return BoxResult::Ok(input);
			
		}
	}
}



pub struct NonWhitespaceInput;

impl TryRead for NonWhitespaceInput {
	type Output = String;
	fn try_read_line(&self, mut read_args: TryReadArgs<String>) -> BoxResult<Self::Output> {
		let mut prompt = read_args.prompt.unwrap_or(String::from("Enter a bool: "));
		if let Some(default) = read_args.default.as_ref() {
			prompt += &format!("(default: {default}) ");
		}
		loop {
			
			print!("{prompt}");
			let input = read_string(&mut read_args.input)?.to_lowercase();
			if input.trim().is_empty() {println!("Invalid input."); continue;}
			return BoxResult::Ok(input);
			
		}
	}
}



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

impl TryRead for BoolInput {
	type Output = bool;
	fn try_read_line(&self, mut read_args: TryReadArgs<Self::Output>) -> crate::BoxResult<Self::Output> {
		let mut prompt = read_args.prompt.unwrap_or(String::from("Enter a bool: "));
		if let Some(default) = read_args.default.as_ref() {
			prompt += &format!("(default: {default}) ");
		}
		loop {
			
			print!("{prompt}");
			let input = read_string(&mut read_args.input)?.to_lowercase();
			match (&*input, read_args.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 TryRead for YesNoInput {
	type Output = bool;
	fn try_read_line(&self, mut read_args: TryReadArgs<Self::Output>) -> crate::BoxResult<Self::Output> {
		let mut prompt = read_args.prompt.unwrap_or(String::from("Enter 'Yes' or 'No': "));
		if let Some(default) = read_args.default.as_ref() {
			prompt += &format!("(default: {}) ", if *default {"Yes"} else {"No"});
		}
		loop {
			
			print!("{prompt}");
			let input = read_string(&mut read_args.input)?.to_lowercase();
			match (&*input, read_args.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 TryRead for $type_name {
			type Output = $type_base;
			fn try_read_line(&self, mut read_args: TryReadArgs<Self::Output>) -> crate::BoxResult<Self::Output> {
				let mut prompt = read_args.prompt.unwrap_or(String::from($default_prompt));
				if let Some(default) = read_args.default.as_ref() {
					prompt += &format!("(default: {default}) ");
				}
				loop {
					
					print!("{prompt}");
					let input_string = read_string(&mut read_args.input)?.to_lowercase();
					if input_string.is_empty() && let Some(default) = read_args.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: ");