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]:
# use winnow::prelude::*;;
# use winnow::error::ContextError;
pub fn literal(literal: &str) -> impl Parser<&str, &str, ContextError>
# {
# winnow::token::literal(literal)
# }
Example
# use winnow::prelude::*;
# use winnow::{error::ErrMode, error::ContextError, error::Needed};
#
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());
# use winnow::prelude::*;
# use winnow::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::Partial;
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::{error::ErrMode, error::ContextError, error::Needed};
# use winnow::prelude::*;
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());