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
// started      24/03/05
// last updated 24/03/06



#![feature(let_chains)]



pub mod input_options;
pub mod ranges;



use std::{error::Error, io::Write};

pub type BoxResult<T> = Result<T, Box<dyn Error>>;



/// reads a line of text, a number, etc
/// 
/// General syntax:
/// read!([default_value] ...) // works with prompt, like prompt!("Give input: "; [3])
/// 
/// 
/// Existing functionalities:
/// 
/// 
/// Input Options
/// These allow you to specify which inputs are allowed. Example: read!(&["a", "b", "c"])
/// Special syntax: read!(= 1, 2, 3)
/// 
/// Implemented types:
/// impl<T: ToString + Clone> ReadLine for ReadData<T, &[T]>
/// impl<T: ToString + Clone> ReadLine for ReadData<T, &[T; LEN]>
/// impl<T: ToString + Clone> ReadLine for ReadData<T, Vec<T>>
/// impl<T: ToString + Clone> ReadLine for ReadData<T, VecDeque<T>>
/// impl<T: ToString + Clone> ReadLine for ReadData<T, LinkedList<T>>
/// 
/// 
/// Ranges
/// These allow you to take a number within a specified range. Example: read!(1. .. 100.), or read!(10..), etc
/// 
/// Implemented types:
/// impl<T: Display + FromStr + PartialOrd<T>> ReadLine for Range<T>
/// impl<T: Display + FromStr + PartialOrd<T>> ReadLine for RangeInclusive<T>
/// impl<T: Display + FromStr + PartialOrd<T>> ReadLine for RangeTo<T>
/// impl<T: Display + FromStr + PartialOrd<T>> ReadLine for RangeFrom<T>
/// impl<T: Display + FromStr + PartialOrd<T>> ReadLine for RangeToInclusive<T>
/// 
/// 
/// If you have ideas for more functionality, feel free to open an issue
#[macro_export]
macro_rules! read {
	($($args:tt)*) => {
		smart_read::try_read!($($args)*).unwrap()
	}
}

/// same as read!(), but returns a result
#[macro_export]
macro_rules! try_read {
	() => {
		smart_read::read_string()
	};
	([$default:expr]) => {{
		print!("(default: {}) ", $default);
		smart_read::read_string_or_default($default.to_string())
	}};
	($custom_input:expr) => {{
		use smart_read::ReadLine;
		$custom_input.try_read_line(None, None)
	}};
	([$default:expr] $custom_input:expr) => {{
		use smart_read::ReadLine;
		$custom_input.try_read_line(None, Some($default))
	}};
	(= $($choice:expr),*) => {{
		use smart_read::ReadLine;
		let choices = &[$($choice,)*];
		choices.try_read_line(None, None)
	}};
	([$default:expr] = $($choice:expr),*) => {{
		use smart_read::ReadLine;
		let choices = &[$($choice,)*];
		choices.try_read_line(None, Some($default))
	}};
}



/// prompts a line of text, a number, etc
#[macro_export]
macro_rules! prompt {
	($($args:tt)*) => {
		smart_read::try_prompt!($($args)*).unwrap()
	}
}

/// same as prompt!(), but returns a result
#[macro_export]
macro_rules! try_prompt {
	($prompt:expr) => {{
		print!("{}", $prompt);
		smart_read::read_string()
	}};
	($prompt:expr; [$default:expr]) => {
		print!("{}(default: {}) ", $prompt, $default);
		smart_read::read_string_or_default($default.to_string())
	};
	($prompt:expr; $custom_input:expr) => {{
		use smart_read::ReadLine;
		$custom_input.try_read_line(Some($prompt.to_string()), None)
	}};
	($prompt:expr; [$default:expr] $custom_input:expr) => {{
		use smart_read::ReadLine;
		$custom_input.try_read_line(Some($prompt.to_string()), Some($default))
	}};
	($prompt:expr; = $($choice:expr),*) => {{
		use smart_read::ReadLine;
		let choices = &[$($choice,)*];
		choices.try_read_line(Some($prompt.to_string()), None)
	}};
	($prompt:expr; [$default:expr] = $($choice:expr),*) => {{
		use smart_read::ReadLine;
		let choices = &[$($choice,)*];
		choices.try_read_line(Some($prompt.to_string()), Some($default))
	}};
}



/// This is what powers the whole crate. Any struct that implements this can be passed to read!(), try_read!(), prompt!(), and try_prompt!()
pub trait ReadLine: Sized {
	type Output;
	fn try_read_line(&self, prompt: Option<String>, default: Option<Self::Output>) -> BoxResult<Self::Output>;
	fn read_line(&self, prompt: Option<String>, default: Option<Self::Output>) -> Self::Output {
		self.try_read_line(prompt, default).unwrap()
	}
}





/// small utility function, mostly for internal use
pub fn read_string() -> BoxResult<String> {
	
	let mut output = String::new();
	std::io::stdout().flush()?;
	std::io::stdin().read_line(&mut output)?;
	
	if output.as_bytes().last() == Some(&10) {output.pop();} // pop \n
	if output.as_bytes().last() == Some(&13) {output.pop();} // pop \r
	
	Ok(output)
}

pub fn read_string_or_default(default: String) -> BoxResult<String> {
	
	let mut output = String::new();
	std::io::stdout().flush()?;
	std::io::stdin().read_line(&mut output)?;
	
	if output.as_bytes().last() == Some(&10) {output.pop();} // pop \n
	if output.as_bytes().last() == Some(&13) {output.pop();} // pop \r
	
	Ok(if output.is_empty() {
		default
	} else {
		output
	})
}