typeward 0.6.0

A type-driven parser combinator library for Rust.
Documentation
use std::borrow::Cow;

use super::{BorrowInput, Input, shared};
use crate::error::{ParseError, ParseResult};
use stable_pattern::Pattern;

impl<'a> Input<'a> for &'a [u8] {
    fn input_len(self) -> usize {
        self.len()
    }

    fn position(self) -> Option<usize> {
        Some(self.as_ptr() as usize)
    }

    fn trim_start(self) -> Self {
        let start = shared::trim_start_utf8_len(self);
        &self[start..]
    }

    fn is_empty(self) -> bool {
        self.is_empty()
    }

    fn display(self) -> Cow<'a, str> {
        String::from_utf8_lossy(self)
    }

    fn strip_prefix(self, prefix: &str) -> ParseResult<Option<Self>> {
        Ok(self.strip_prefix(prefix.as_bytes()))
    }

    fn advance(self, bytes: usize) -> ParseResult<Self> {
        if bytes > self.len() {
            return Err(ParseError::fatal(
                "invalid input bounds while advancing byte input",
            ));
        }

        Ok(&self[bytes..])
    }

    fn find(self, needle: &str) -> ParseResult<Option<Self>> {
        let s = shared::valid_utf8_prefix(self);
        Ok(s.find(needle).map(|idx| &self[idx..]))
    }

    fn slice_to(self, end: Self) -> ParseResult<Self> {
        let consumed = shared::checked_consumed_len(self.len(), end.len(), "byte")?;
        let suffix = &self[consumed..];
        if suffix.as_ptr() != end.as_ptr() || suffix.len() != end.len() {
            return Err(ParseError::fatal(
                "invalid input bounds while slicing byte input",
            ));
        }
        Ok(&self[..consumed])
    }

    fn take_char(self) -> ParseResult<Option<(char, Self)>> {
        Ok(shared::take_char_prefix(self)?.map(|(ch, width)| (ch, &self[width..])))
    }

    fn take_while<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
    where
        P: for<'b> Pattern<'b> + Copy,
    {
        let (matched, rest) = self.take_while_borrowed(predicate)?;
        Ok((Cow::Borrowed(matched), rest))
    }

    fn take_till<P>(self, predicate: P) -> ParseResult<(Cow<'a, str>, Self)>
    where
        P: for<'b> Pattern<'b> + Copy,
    {
        let (matched, rest) = self.take_till_borrowed(predicate)?;
        Ok((Cow::Borrowed(matched), rest))
    }

    fn take_till_str(self, needle: &str) -> ParseResult<(Cow<'a, str>, Self)> {
        self.take_till(needle)
    }
}

impl<'a> BorrowInput<'a> for &'a [u8] {
    fn take_while_borrowed<P>(self, predicate: P) -> ParseResult<(&'a str, Self)>
    where
        P: for<'b> Pattern<'b> + Copy,
    {
        let s = shared::valid_utf8_prefix(self);
        let idx = shared::take_while_prefix_len(s, predicate);
        Ok((&s[..idx], &self[idx..]))
    }

    fn take_till_borrowed<P>(self, predicate: P) -> ParseResult<(&'a str, Self)>
    where
        P: for<'b> Pattern<'b> + Copy,
    {
        let s = shared::valid_utf8_prefix(self);
        let (matched, rest) = shared::split_take_till(s, predicate);
        Ok((matched, rest.as_bytes()))
    }
}