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
/*
* Copyright 2022 - Oliver Lenehan (sunsetkookaburra)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
//! # input-macro - No-nonsense input!(...) macro for Rust.
//!
//! # Example
//!
//! ```no_run
//! use input_macro::input;
//!
//! fn main() {
//! let name = input!("What's your name? ");
//! println!("Hello, {name}!");
//!
//! let age: i64 = input!("How old are you today, {name}? ").parse().unwrap();
//!
//! match age {
//! i if i < 0 => {
//! println!("Whoah, negative age! Impressive! 🌌");
//! },
//! _ => {
//! println!("Happy Birthday! Congratulations! 🥳");
//! },
//! }
//!
//! match input!("Do you like chocolate 🍫 (y/N)? ").as_str() {
//! "y" | "Y" => {
//! println!("Yay! I like chocolate too 🙂.");
//! },
//! _ => {
//! println!("Oh well, all the more for me 😋.");
//! },
//! }
//! }
//! ```
use Arguments;
use ;
/// Displays formatted prompt text to the standard output and
/// then reads the next line from the standard input,
/// returning it as a [`String`].
///
/// # Panics
///
/// Panics if writing to `std::io::stdout()` fails,
/// or reading from `std::io::stdin()` fails.
///
/// # Example
/// ```no_run
/// use input_macro::input;
///
/// let name: String = input!("What's your name? ");
/// let age: i64 = input!("How old are you today {name}? ").parse().unwrap();
/// println!(
/// "In hexadecimal, thats {}{:x}!",
/// if age < 0 { "-" } else { "" }, age.abs(),
/// );
/// ```
/// Writes and flushes a formatted string as prompt text to the `dst` ([`Write`])
/// then reads the next line from the `src` ([`io::BufRead`]),
/// returning it as a [`io::Result<String>`].
///
/// # Errors
///
/// This function will return any I/O error reported while formatting, flushing or reading.
/// Also returns an [`io::ErrorKind::UnexpectedEof`] error if the stream reaches EOF.
///
/// # Example
/// ```
/// use std::io::Cursor;
/// use input_macro::input_fmt;
///
/// let mut source = Cursor::new("Joe Bloggs\n");
/// let mut output = Vec::new();
/// let name = input_fmt(&mut source, &mut output, format_args!("What's your {}? ", "name"));
/// assert_eq!(String::from_utf8(output).unwrap(), "What's your name? ");
/// assert_eq!(name.unwrap(), "Joe Bloggs");
/// ```
/// Reads the next line from `src` ([`io::BufRead`]), mapping
/// EOF to [`io::ErrorKind::UnexpectedEof`] and returning a [`io::Result<String>`].
///
/// # Errors
///
/// This function will return any I/O error reported while reading.
/// Also returns an [`io::ErrorKind::UnexpectedEof`] error if `src` reaches EOF (returns `Ok(0)`).
///
/// # Example
/// ```
/// use std::io::Cursor;
/// use input_macro::read_line_expected;
///
/// let mut source = Cursor::new("Insert Text Here\n");
/// let text = read_line_expected(&mut source);
/// assert_eq!(text.unwrap(), "Insert Text Here");
/// ```