mod bytes_input;
mod read_input_buf;
mod read_input_stream;
mod shared;
mod str_input;
use std::borrow::Cow;
pub use read_input_buf::{ReadInput, ReadInputBuf};
pub use read_input_stream::{ReadInputStream, ReadInputStreamInput};
use crate::error::{ParseError, ParseResult};
use stable_pattern::Pattern;
pub trait Input<'a>: Copy + Sized {
fn input_len(self) -> usize;
fn position(self) -> Option<usize> {
None
}
fn remaining_len(self) -> Option<usize> {
Some(self.input_len())
}
fn display(self) -> Cow<'a, str>;
fn take_char(self) -> ParseResult<Option<(char, Self)>>;
#[must_use]
fn trim_start(self) -> Self {
self.try_trim_start().unwrap_or(self)
}
fn try_trim_start(self) -> ParseResult<Self> {
let (_, rest) = self.take_while(|c: char| c.is_whitespace())?;
Ok(rest)
}
fn is_empty(self) -> bool {
self.try_is_empty().unwrap_or(false)
}
fn try_is_empty(self) -> ParseResult<bool> {
Ok(self.take_char()?.is_none())
}
fn strip_prefix(self, prefix: &str) -> ParseResult<Option<Self>> {
let mut rest = self;
for expected in prefix.chars() {
match rest.take_char()? {
Some((found, next)) if found == expected => rest = next,
Some(_) | None => return Ok(None),
}
}
Ok(Some(rest))
}
fn advance(self, bytes: usize) -> ParseResult<Self> {
let mut rest = self;
let mut remaining = bytes;
while remaining > 0 {
let Some((ch, next)) = rest.take_char()? else {
return Err(ParseError::fatal(
"invalid input bounds while advancing input",
));
};
let width = ch.len_utf8();
if width > remaining {
return Err(ParseError::fatal(
"invalid UTF-8 boundary while advancing input",
));
}
remaining -= width;
rest = next;
}
Ok(rest)
}
fn find(self, needle: &str) -> ParseResult<Option<Self>> {
let mut rest = self;
loop {
if rest.strip_prefix(needle)?.is_some() {
return Ok(Some(rest));
}
match rest.take_char()? {
Some((_, next)) => rest = next,
None => return Ok(None),
}
}
}
fn slice_to(self, end: Self) -> ParseResult<Self>;
fn take_char1(self) -> ParseResult<(char, Self)> {
match self.take_char()? {
Some(value) => Ok(value),
None => Err(ParseError::UnexpectedEOF),
}
}
fn take_while<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
where
P: for<'b> Pattern<'b> + Copy,
{
if predicate.strip_prefix_of("").is_some() {
return Ok((Cow::Owned(String::new()), self));
}
let mut matched = String::new();
let mut rest = self;
while let Some((ch, next)) = rest.take_char()? {
let mut encoded = [0_u8; 4];
let ch_str = ch.encode_utf8(&mut encoded);
if predicate.strip_prefix_of(ch_str).is_some() {
matched.push(ch);
rest = next;
} else {
break;
}
}
Ok((Cow::Owned(matched), rest))
}
fn take_while1<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
where
P: for<'b> Pattern<'b> + Copy,
{
let (value, rest) = self.take_while(predicate)?;
if value.is_empty() {
return Err(ParseError::custom(
"expected at least one matching character",
));
}
Ok((value, rest))
}
fn take_till<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
where
P: for<'b> Pattern<'b> + Copy,
{
if predicate.strip_prefix_of("").is_some() {
return Ok((Cow::Owned(String::new()), self));
}
let mut matched = String::new();
let mut rest = self;
while let Some((ch, next)) = rest.take_char()? {
let mut encoded = [0_u8; 4];
let ch_str = ch.encode_utf8(&mut encoded);
if predicate.strip_prefix_of(ch_str).is_some() {
break;
}
matched.push(ch);
rest = next;
}
Ok((Cow::Owned(matched), rest))
}
fn take_till_str(self, needle: &str) -> ParseResult<(Cow<'a, str>, Self)> {
if needle.is_empty() {
return Ok((Cow::Owned(String::new()), self));
}
match self.find(needle)? {
Some(end) => {
let (matched, _) = self.slice_to(end)?.take_while(|_: char| true)?;
Ok((matched, end))
}
None => self.take_while(|_: char| true),
}
}
fn take_till1<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
where
P: for<'b> Pattern<'b> + Copy,
{
let (value, rest) = self.take_till(predicate)?;
if value.is_empty() {
return Err(ParseError::custom(
"expected at least one character before the delimiter",
));
}
Ok((value, rest))
}
}
pub trait BorrowInput<'a>: Input<'a> {
fn take_while_borrowed<P>(self, predicate: P) -> ParseResult<(&'a str, Self)>
where
P: for<'b> Pattern<'b> + Copy;
fn take_till_borrowed<P>(self, predicate: P) -> ParseResult<(&'a str, Self)>
where
P: for<'b> Pattern<'b> + Copy;
}
pub trait FromInputStr<'a, I>: AsRef<str> + Sized
where
I: Input<'a>,
{
fn from_input_str(value: Cow<'a, str>) -> ParseResult<Self>;
}
impl<'a, I> FromInputStr<'a, I> for String
where
I: Input<'a>,
{
fn from_input_str(value: Cow<'a, str>) -> ParseResult<Self> {
Ok(value.into_owned())
}
}
impl<'a, I> FromInputStr<'a, I> for Cow<'a, str>
where
I: Input<'a>,
{
fn from_input_str(value: Cow<'a, str>) -> ParseResult<Self> {
Ok(value)
}
}
impl<'a, I> FromInputStr<'a, I> for &'a str
where
I: BorrowInput<'a>,
{
fn from_input_str(value: Cow<'a, str>) -> ParseResult<Self> {
match value {
Cow::Borrowed(value) => Ok(value),
Cow::Owned(_) => Err(ParseError::custom(
"borrowed output requires borrow-capable input",
)),
}
}
}
#[cfg(test)]
mod tests;