use std::{io::BufRead, str::Utf8Error};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum InputError {
#[error("Invalid string: {0}")]
InvalidString(#[from] Utf8Error),
#[error("Unexpected EOF")]
UnexpectedEof,
#[error("Unsatisfied expectation: expected '{expected}', found '{found}'")]
UnsatisfiedExpectation { expected: String, found: String },
#[error("I/O error: {0}")]
IO(#[from] std::io::Error),
}
impl PartialEq for InputError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(InputError::InvalidString(a), InputError::InvalidString(b)) => a == b,
(InputError::UnexpectedEof, InputError::UnexpectedEof) => true,
(InputError::IO(a), InputError::IO(b)) => a.kind() == b.kind(),
(
InputError::UnsatisfiedExpectation {
expected: a1,
found: a2,
},
InputError::UnsatisfiedExpectation {
expected: b1,
found: b2,
},
) => a1 == b1 && a2 == b2,
_ => false,
}
}
}
fn try_from_utf8(bytes: &[u8]) -> Result<(&str, usize), Utf8Error> {
match std::str::from_utf8(bytes) {
Ok(s) => Ok((s, bytes.len())),
Err(e) => {
if e.error_len().is_some() {
Err(e)
} else {
let valid_up_to = e.valid_up_to();
let valid_bytes = &bytes[..valid_up_to];
let partial = unsafe { std::str::from_utf8_unchecked(valid_bytes) };
Ok((partial, valid_up_to))
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Coordinates {
line: usize,
column: usize,
}
impl Default for Coordinates {
fn default() -> Self {
Self { line: 1, column: 1 }
}
}
impl Coordinates {
pub fn new(line: usize, column: usize) -> Self {
Self { line, column }
}
pub fn line(&self) -> usize {
self.line
}
pub fn column(&self) -> usize {
self.column
}
fn evaluate(&mut self, ch: char) {
if ch == '\n' {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
}
}
impl std::fmt::Display for Coordinates {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
pub struct Input<R> {
rdr: R,
peeked: String,
pending: Vec<u8>,
position: usize,
coordinates: Coordinates,
}
impl<R: BufRead> Input<R> {
pub fn new(rdr: R) -> Self {
Self {
rdr,
peeked: String::new(),
pending: vec![],
position: 0,
coordinates: Coordinates::default(),
}
}
pub fn peek(&mut self) -> Result<Option<char>, InputError> {
let s = self.peek_n(1)?;
Ok(s.and_then(|s| s.chars().next()))
}
pub fn peek_n(&mut self, required: usize) -> Result<Option<&str>, InputError> {
let missing_chars = required.saturating_sub(self.peeked.chars().count());
if missing_chars > 0 {
let mut decoded = 0;
let mut delta = String::with_capacity(missing_chars);
loop {
let mut curr = [0u8; 256];
let found = self.rdr.read(&mut curr)?;
if found == 0 {
break;
}
self.pending.extend(&curr[..found]);
let (partial, used) = try_from_utf8(&self.pending)?;
delta.push_str(partial);
decoded += partial.chars().count();
self.pending.drain(..used);
if decoded >= missing_chars {
break;
}
}
if !self.pending.is_empty() {
return Err(InputError::UnexpectedEof);
}
self.peeked.push_str(&delta);
}
let end = self
.peeked
.char_indices()
.map(|(i, _)| i)
.chain(std::iter::once(self.peeked.len()))
.nth(required);
Ok(end.map(|l| &self.peeked[..l]))
}
pub fn expect(&mut self, expected: &str) -> Result<(), InputError> {
let found = self.peek_n(expected.chars().count())?;
let found = found.ok_or_else(|| InputError::UnsatisfiedExpectation {
expected: expected.to_string(),
found: String::new(),
})?;
if found != expected {
Err(InputError::UnsatisfiedExpectation {
expected: expected.to_string(),
found: found.to_string(),
})
} else {
for ch in expected.chars() {
self.coordinates.evaluate(ch);
}
self.peeked.drain(..expected.len());
self.position += expected.len();
Ok(())
}
}
pub fn consume_while<P>(&mut self, predicate: P) -> Result<String, InputError>
where
P: Fn(char) -> bool,
{
let mut found = String::new();
loop {
let next = self.peek()?;
if let Some(ch) = next {
if !predicate(ch) {
break;
}
found.push(ch);
self.peeked.drain(..ch.len_utf8());
} else {
break;
}
}
self.position += found.len();
for ch in found.chars() {
self.coordinates.evaluate(ch);
}
Ok(found)
}
pub fn consume_until<P>(&mut self, predicate: P) -> Result<String, InputError>
where
P: Fn(char) -> bool,
{
let mut found = String::new();
loop {
let next = self.peek()?;
if let Some(ch) = next {
if predicate(ch) {
break;
}
found.push(ch);
self.peeked.drain(..ch.len_utf8());
} else {
break;
}
}
for ch in found.chars() {
self.coordinates.evaluate(ch);
}
self.position += found.len();
Ok(found)
}
pub fn consume_char(&mut self) -> Result<Option<char>, InputError> {
let found = self.peek()?;
if let Some(ch) = found {
self.peeked.drain(..ch.len_utf8());
}
if let Some(ch) = found {
self.coordinates.evaluate(ch);
self.position += ch.len_utf8();
}
Ok(found)
}
pub fn expect_char(&mut self, expected: char) -> Result<(), InputError> {
let found = self.peek()?;
if let Some(ch) = found {
if expected != ch {
Err(InputError::UnsatisfiedExpectation {
expected: expected.to_string(),
found: ch.to_string(),
})
} else {
self.peeked.drain(..ch.len_utf8());
self.coordinates.evaluate(ch);
self.position += ch.len_utf8();
Ok(())
}
} else {
Err(InputError::UnsatisfiedExpectation {
expected: expected.to_string(),
found: String::new(),
})
}
}
pub fn is_eof(&mut self) -> Result<bool, InputError> {
Ok(self.peek()?.is_none())
}
pub fn skip_whitespace(&mut self) -> Result<(), InputError> {
self.consume_while(char::is_whitespace)?;
Ok(())
}
pub fn position(&self) -> usize {
self.position
}
pub fn coordinates(&self) -> Coordinates {
self.coordinates
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn peek_on_empty_input_returns_none() {
let cursor = Cursor::new(Vec::<u8>::new());
let mut input = Input::new(cursor);
assert_eq!(input.peek().unwrap(), None);
}
#[test]
fn peek_returns_first_char() {
let cursor = Cursor::new(b"hello");
let mut input = Input::new(cursor);
assert_eq!(input.peek().unwrap(), Some('h'));
}
#[test]
fn peek_is_idempotent() {
let cursor = Cursor::new(b"hello");
let mut input = Input::new(cursor);
assert_eq!(input.peek().unwrap(), Some('h'));
assert_eq!(input.peek().unwrap(), Some('h'));
assert_eq!(input.peek().unwrap(), Some('h'));
}
#[test]
fn peek_n_returns_prefix() {
let cursor = Cursor::new(b"hello");
let mut input = Input::new(cursor);
assert_eq!(input.peek_n(3).unwrap(), Some("hel"));
}
#[test]
fn peek_n_returns_full_string_when_exact_length() {
let cursor = Cursor::new(b"hello");
let mut input = Input::new(cursor);
assert_eq!(input.peek_n(5).unwrap(), Some("hello"));
}
#[test]
fn peek_n_beyond_eof_returns_none() {
let cursor = Cursor::new(b"hi");
let mut input = Input::new(cursor);
assert_eq!(input.peek_n(3).unwrap(), None);
}
#[test]
fn peek_n_handles_utf8_characters() {
let cursor = Cursor::new("èabc".as_bytes());
let mut input = Input::new(cursor);
assert_eq!(input.peek_n(1).unwrap(), Some("è"));
assert_eq!(input.peek_n(2).unwrap(), Some("èa"));
assert_eq!(input.peek_n(4).unwrap(), Some("èabc"));
}
#[test]
fn peek_handles_utf8_character() {
let cursor = Cursor::new("èabc".as_bytes());
let mut input = Input::new(cursor);
let result = input.peek();
assert!(result.is_ok());
}
#[test]
fn peek_n_invalid_utf8_returns_error() {
let cursor = Cursor::new(vec![0xff, 0xfe]);
let mut input = Input::new(cursor);
let err = input.peek_n(2).unwrap_err();
matches!(err, InputError::InvalidString(_));
}
#[test]
fn expect_success() {
let cursor = Cursor::new(b"hello");
let mut input = Input::new(cursor);
assert!(input.expect("hell").is_ok());
assert!(input.expect("o").is_ok());
assert_eq!(input.peek().unwrap(), None);
}
#[test]
fn expect_failure() {
let cursor = Cursor::new(b"hello");
let mut input = Input::new(cursor);
let got = input.expect("ciao");
assert_eq!(
got,
Err(InputError::UnsatisfiedExpectation {
expected: "ciao".to_string(),
found: "hell".to_string(),
})
);
}
#[test]
fn expect_utf8() {
let cursor = Cursor::new("è".as_bytes());
let mut input = Input::new(cursor);
assert!(input.expect("è").is_ok());
}
#[test]
fn expect_eof() {
let cursor = Cursor::new(b"he");
let mut input = Input::new(cursor);
assert_eq!(
input.expect("hello"),
Err(InputError::UnsatisfiedExpectation {
expected: "hello".to_string(),
found: String::new(),
})
);
}
#[test]
fn expect_failure_does_not_consume() {
let cursor = Cursor::new(b"hello");
let mut input = Input::new(cursor);
assert!(input.expect("ciao").is_err());
assert_eq!(input.peek_n(5).unwrap(), Some("hello"));
}
#[test]
fn expect_consumes_only_matched_text() {
let cursor = Cursor::new(b"hello world");
let mut input = Input::new(cursor);
input.expect("hello").unwrap();
assert_eq!(input.peek_n(6).unwrap(), Some(" world"));
}
#[test]
fn consume_while_whitespace() {
let cursor = Cursor::new(b"\t \nhello world");
let mut input = Input::new(cursor);
let x = input.consume_while(char::is_whitespace).unwrap();
assert_eq!(x, "\t \n".to_string());
input.expect("hello").unwrap();
}
#[test]
fn consume_while_stops_before_first_non_matching() {
let mut input = Input::new(Cursor::new("123abc"));
let consumed = input.consume_while(|c| c.is_ascii_digit()).unwrap();
assert_eq!(consumed, "123");
assert_eq!(input.peek_n(3).unwrap(), Some("abc"));
}
#[test]
fn consume_while_utf8_until_eof() {
let mut input = Input::new(Cursor::new("èéà"));
let consumed = input.consume_while(|_| true).unwrap();
assert_eq!(consumed, "èéà");
assert_eq!(input.peek().unwrap(), None);
}
#[test]
fn consume_while_utf8() {
let mut input = Input::new(Cursor::new("èéabc"));
let consumed = input.consume_while(|c| !c.is_ascii()).unwrap();
assert_eq!(consumed, "èé");
assert_eq!(input.peek_n(3).unwrap(), Some("abc"));
}
#[test]
fn consume_while_until_eof() {
let mut input = Input::new(Cursor::new("123"));
let consumed = input.consume_while(|c| c.is_ascii_digit()).unwrap();
assert_eq!(consumed, "123");
assert_eq!(input.peek().unwrap(), None);
}
#[test]
fn consume_while_matches_nothing() {
let mut input = Input::new(Cursor::new("hello"));
let consumed = input.consume_while(char::is_whitespace).unwrap();
assert_eq!(consumed, "");
assert_eq!(input.peek_n(5).unwrap(), Some("hello"));
}
#[test]
fn consume_until_utf8() {
let mut input = Input::new(Cursor::new("èéà!abc"));
let consumed = input.consume_until(|c| c == '!').unwrap();
assert_eq!(consumed, "èéà");
assert_eq!(input.peek().unwrap(), Some('!'));
}
#[test]
fn consume_until_eof() {
let mut input = Input::new(Cursor::new("hello"));
let consumed = input.consume_until(|c| c == ',').unwrap();
assert_eq!(consumed, "hello");
assert_eq!(input.peek().unwrap(), None);
}
#[test]
fn consume_until_immediate_match() {
let mut input = Input::new(Cursor::new(",world"));
let consumed = input.consume_until(|c| c == ',').unwrap();
assert_eq!(consumed, "");
assert_eq!(input.peek().unwrap(), Some(','));
}
#[test]
fn consume_until_stops_before_match() {
let mut input = Input::new(Cursor::new("hello,world"));
let consumed = input.consume_until(|c| c == ',').unwrap();
assert_eq!(consumed, "hello");
assert_eq!(input.peek().unwrap(), Some(','));
}
#[test]
fn consume_char_handles_utf8() {
let mut input = Input::new(Cursor::new("èa"));
assert_eq!(input.consume_char().unwrap(), Some('è'));
assert_eq!(input.consume_char().unwrap(), Some('a'));
assert_eq!(input.consume_char().unwrap(), None);
}
#[test]
fn consume_char_returns_none_at_eof() {
let mut input = Input::new(Cursor::new(""));
assert_eq!(input.consume_char().unwrap(), None);
}
#[test]
fn consume_char_returns_next_character() {
let mut input = Input::new(Cursor::new("abc"));
assert_eq!(input.consume_char().unwrap(), Some('a'));
assert_eq!(input.consume_char().unwrap(), Some('b'));
assert_eq!(input.consume_char().unwrap(), Some('c'));
}
#[test]
fn expect_char_utf8() {
let mut input = Input::new(Cursor::new("èabc"));
assert!(input.expect_char('è').is_ok());
assert_eq!(input.peek().unwrap(), Some('a'));
}
#[test]
fn expect_char_failure() {
let mut input = Input::new(Cursor::new("abc"));
assert!(input.expect_char('z').is_err());
assert_eq!(input.peek().unwrap(), Some('a'));
}
#[test]
fn expect_char_success() {
let mut input = Input::new(Cursor::new("abc"));
assert!(input.expect_char('a').is_ok());
assert_eq!(input.peek().unwrap(), Some('b'));
}
#[test]
fn is_eof_after_consuming_utf8_input() {
let mut input = Input::new(Cursor::new("è"));
input.consume_char().unwrap();
assert!(input.is_eof().unwrap());
}
#[test]
fn is_eof_after_consuming_all_input() {
let mut input = Input::new(Cursor::new("abc"));
input.consume_char().unwrap();
input.consume_char().unwrap();
input.consume_char().unwrap();
assert!(input.is_eof().unwrap());
}
#[test]
fn is_eof_on_non_empty_input() {
let mut input = Input::new(Cursor::new("abc"));
assert!(!input.is_eof().unwrap());
}
#[test]
fn is_eof_on_empty_input() {
let mut input = Input::new(Cursor::new(""));
assert!(input.is_eof().unwrap());
}
#[test]
fn position_starts_at_zero() {
let input = Input::new(Cursor::new("abc"));
assert_eq!(input.position(), 0);
}
#[test]
fn position_advances_by_bytes_ascii() {
let mut input = Input::new(Cursor::new("abc"));
input.consume_char().unwrap();
assert_eq!(input.position(), 1);
input.consume_char().unwrap();
assert_eq!(input.position(), 2);
}
#[test]
fn position_advances_by_bytes_utf8() {
let mut input = Input::new(Cursor::new("èa"));
input.consume_char().unwrap(); assert_eq!(input.position(), 2);
input.consume_char().unwrap(); assert_eq!(input.position(), 3);
}
#[test]
fn position_unchanged_at_eof() {
let mut input = Input::new(Cursor::new("a"));
input.consume_char().unwrap();
input.consume_char().unwrap(); assert_eq!(input.position(), 1);
}
#[test]
fn coordinates_starts_at_line_1_col_1() {
let input = Input::new(Cursor::new("abc"));
assert_eq!(input.coordinates(), Coordinates::new(1, 1));
}
#[test]
fn coordinates_advances_column() {
let mut input = Input::new(Cursor::new("abc"));
input.consume_char().unwrap();
assert_eq!(input.coordinates(), Coordinates::new(1, 2));
}
#[test]
fn coordinates_advances_line_on_newline() {
let mut input = Input::new(Cursor::new("a\nb"));
input.consume_char().unwrap(); input.consume_char().unwrap(); assert_eq!(input.coordinates(), Coordinates::new(2, 1));
input.consume_char().unwrap(); assert_eq!(input.coordinates(), Coordinates::new(2, 2));
}
}