Function literal

Source
pub fn literal<Literal, Input, Error>(
    literal: Literal,
) -> impl Parser<Input, <Input as Stream>::Slice, Error>
where Input: StreamIsPartial + Stream + Compare<Literal>, Literal: Clone + Debug, Error: ParserError<Input>,
Expand description

Recognizes a literal

The input data will be compared to the literal combinator’s argument and will return the part of the input that matches the argument

It will return Err(ErrMode::Backtrack(_)) if the input doesn’t match the literal

Note: Parser is implemented for strings and byte strings as a convenience (complete only)

§Effective Signature

Assuming you are parsing a &str Stream:

pub fn literal(literal: &str) -> impl Parser<&str, &str, ContextError>

§Example

fn parser<'i>(s: &mut &'i str) -> ModalResult<&'i str> {
  "Hello".parse_next(s)
}

assert_eq!(parser.parse_peek("Hello, World!"), Ok((", World!", "Hello")));
assert!(parser.parse_peek("Something").is_err());
assert!(parser.parse_peek("").is_err());

fn parser<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> {
  "Hello".parse_next(s)
}

assert_eq!(parser.parse_peek(Partial::new("Hello, World!")), Ok((Partial::new(", World!"), "Hello")));
assert!(parser.parse_peek(Partial::new("Something")).is_err());
assert!(parser.parse_peek(Partial::new("S")).is_err());
assert_eq!(parser.parse_peek(Partial::new("H")), Err(ErrMode::Incomplete(Needed::Unknown)));
use winnow::token::literal;
use winnow::ascii::Caseless;

fn parser<'i>(s: &mut &'i str) -> ModalResult<&'i str> {
  literal(Caseless("hello")).parse_next(s)
}

assert_eq!(parser.parse_peek("Hello, World!"), Ok((", World!", "Hello")));
assert_eq!(parser.parse_peek("hello, World!"), Ok((", World!", "hello")));
assert_eq!(parser.parse_peek("HeLlO, World!"), Ok((", World!", "HeLlO")));
assert!(parser.parse_peek("Something").is_err());
assert!(parser.parse_peek("").is_err());