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 212 213 214 215 216 217 218 219 220 221
#![feature(read_buf)]
use std::{
io,
io::{BorrowedBuf, Read, Write},
mem,
mem::MaybeUninit,
process::{ExitCode, Termination},
};
/// The answer to a question posed in [ask].
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Answer {
Yes,
No,
Unknown,
}
impl Termination for Answer {
fn report(self) -> ExitCode {
match self {
Self::Yes => ExitCode::SUCCESS,
Self::No => ExitCode::FAILURE,
Self::Unknown => ExitCode::from(2),
}
}
}
enum State {
Start,
Ask {
/// Passthrough for [State::Read].
pending_crlf: bool,
},
/// Continuously reads from stdin until encountering a newline,
/// returning the index of its first byte.
///
/// This state deals with a number of edge cases:
/// - If stdin reaches the EOF, exit the process *after* processing all
/// remaining input.
/// - If a \r byte is seen on the border of the buffer, fail the input
/// (because it will be at a higher index than the max length of all
/// possible valid replies and therefore cannot be a valid reply) and
/// consume a \n if it is the next byte. Note that this consumption cannot
/// happen before printing the question again or we might get blocked on
/// stdin if there are no more bytes available.
/// - If no newline was found within the buffer, fail the reply since it
/// cannot possibly be valid.
Read {
/// The reply is known to be invalid, but we have not yet seen a
/// newline.
failed: bool,
/// A CRLF might be striding the buffer.
pending_crlf: bool,
},
HandleReply {
newline_index: usize,
},
}
/// Ask the user a yes or no question on stdout, reading the reply from stdin.
///
/// Replies are delimited by newlines of any kind and must be one of '' (maps to
/// yes), 'y', 'yes', 'n', 'no', case insensitive. If the reply fails to parse,
/// the question will be asked again ad infinitum.
///
/// # Examples
///
/// ```
/// # use std::{io, io::Read, str::from_utf8};
/// use ask_cli::{ask, Answer};
///
/// assert!(matches!(
/// ask("Continue? [Y/n] ", &mut "y\n".as_bytes(), &mut io::sink()),
/// Ok(Answer::Yes)
/// ));
/// assert!(matches!(
/// ask("Continue? [Y/n] ", &mut "n\n".as_bytes(), &mut io::sink()),
/// Ok(Answer::No)
/// ));
/// assert!(matches!(
/// ask("Continue? [Y/n] ", &mut "".as_bytes(), &mut io::sink()),
/// Ok(Answer::Unknown)
/// ));
///
/// // Here we use 3 different kinds of line endings
/// let mut stdout = Vec::new();
/// ask(
/// "Continue? [Y/n] ",
/// &mut "a\nb\rc\r\nyes\n".as_bytes(),
/// &mut stdout,
/// )
/// .unwrap();
/// assert_eq!(
/// "Continue? [Y/n] Continue? [Y/n] Continue? [Y/n] Continue? [Y/n] ",
/// from_utf8(&stdout).unwrap()
/// );
/// ```
///
/// # Errors
///
/// Underlying I/O errors are bubbled up.
pub fn ask<Q: AsRef<[u8]>, In: Read, Out: Write>(
question: Q,
stdin: &mut In,
stdout: &mut Out,
) -> Result<Answer, io::Error> {
// max_len(yes, no, y, n) = 3 -> 3 + 2 bytes for new lines
const BUF_LEN: usize = 5;
let (mut buf, mut buf2) = (
[MaybeUninit::uninit(); BUF_LEN],
[MaybeUninit::uninit(); BUF_LEN],
);
let (mut buf, mut buf2) = (
BorrowedBuf::from(buf.as_mut()),
BorrowedBuf::from(buf2.as_mut()),
);
macro_rules! consume_bytes {
($count:expr) => {
buf2.clear();
buf2.unfilled().append(&buf.filled()[$count..]);
mem::swap(&mut buf, &mut buf2);
};
}
macro_rules! consume_newline {
($newline_index:expr) => {
let newline_index = $newline_index;
let is_crlf = buf.filled()[newline_index] == b'\r'
&& matches!(buf.filled().get(newline_index + 1), Some(b'\n'));
let skip = if is_crlf { 2 } else { 1 };
consume_bytes!(newline_index + skip);
};
}
let mut state = State::Start;
loop {
state = match state {
State::Start => State::Ask {
pending_crlf: false,
},
State::Ask { pending_crlf } => {
stdout.write_all(question.as_ref())?;
stdout.flush()?;
State::Read {
failed: false,
pending_crlf,
}
}
State::Read {
failed,
pending_crlf,
} => {
debug_assert!(buf.len() < buf.capacity());
let prev_count = buf.len();
stdin.read_buf(buf.unfilled())?;
if pending_crlf && matches!(buf.filled().first(), Some(b'\n')) {
consume_bytes!(1);
}
match buf.filled().iter().position(|&b| b == b'\n' || b == b'\r') {
Some(newline_index) if newline_index == BUF_LEN - 1 => {
let pending_crlf = buf.filled()[newline_index] == b'\r';
buf.clear();
State::Ask { pending_crlf }
}
Some(newline_index) if failed => {
consume_newline!(newline_index);
State::Ask {
pending_crlf: false,
}
}
Some(newline_index) => State::HandleReply { newline_index },
None if buf.len() == buf.capacity() => {
buf.clear();
State::Read {
failed: true,
pending_crlf: false,
}
}
None if !pending_crlf && buf.len() == prev_count => {
// Reached EOF
return Ok(Answer::Unknown);
}
None => State::Read {
failed,
pending_crlf: false,
},
}
}
State::HandleReply { newline_index } => {
let reply = &mut buf.filled_mut()[..newline_index];
reply.make_ascii_lowercase();
match &*reply {
b"" | b"y" | b"yes" => return Ok(Answer::Yes),
b"n" | b"no" => return Ok(Answer::No),
_ => {
consume_newline!(newline_index);
State::Ask {
pending_crlf: false,
}
}
}
}
}
}
}
#[cfg(kani)]
#[kani::proof]
#[kani::unwind(9)]
fn ask_proof() {
let input: [u8; 4] = kani::any();
let output = ask("?", &mut input.as_slice(), &mut io::sink());
output.unwrap();
}