[][src]Function nom_supreme::tag::complete::tag

pub fn tag<T, I, E>(tag: T) -> impl Clone + Fn(I) -> IResult<I, I, E> where
    T: InputLength + Clone,
    I: InputTake + Compare<T>,
    E: TagError<I, T>, 

Parser recognizing a fixed pattern, called a tag. If the front of the input data matches the tag, that part of the input will be returned. Records the tag in the error in the event of a parse failure via TagError.

Example

use cool_asserts::assert_matches;
use nom_supreme::tag::complete::tag;
use nom_supreme::error::{ErrorTree, BaseErrorKind, Expectation};

fn parse_hello(s: &str) -> IResult<&str, &str, ErrorTree<&str>> {
    tag("hello")(s)
}

assert_matches!(
    parse_hello("hello, world!"),
    Ok((", world!", "hello")),
);

assert_matches!(
    parse_hello("something"),
    Err(Err::Error(ErrorTree::Base {
        location: "something",
        kind: BaseErrorKind::Expected(Expectation::Tag("hello")),
    }))
);

assert_matches!(
    parse_hello("hel"),
    Err(Err::Error(ErrorTree::Base {
        location: "hel",
        kind: BaseErrorKind::Expected(Expectation::Tag("hello")),
    }))
);