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 trim_start(self) -> Self;
fn is_empty(self) -> bool;
fn display(self) -> Cow<'a, str>;
fn strip_prefix(self, prefix: &str) -> ParseResult<Option<Self>>;
fn find(self, needle: &str) -> ParseResult<Option<Self>>;
fn slice_to(self, end: Self) -> ParseResult<Self>;
fn take_char(self) -> ParseResult<Option<(char, Self)>>;
fn take_while<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
where
P: for<'b> Pattern<'b> + Copy;
fn take_till<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
where
P: for<'b> Pattern<'b> + Copy;
}
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;