Skip to content

Instantly share code, notes, and snippets.

@pczarn
Created January 28, 2022 08:13
Show Gist options
  • Save pczarn/607b991ecc835c323b0bd33b0ddd2f9b to your computer and use it in GitHub Desktop.
Save pczarn/607b991ecc835c323b0bd33b0ddd2f9b to your computer and use it in GitHub Desktop.
Nom error
use crate::Response;
use nom::IResult;
use nom::character::complete::{char, anychar};
use nom::bytes::complete::take;
use nom::bytes::complete::take_until;
use nom::bytes::complete::take_till;
use nom::multi::many_till;
use nom::combinator::eof;
use nom::sequence::pair;
use nom::branch::alt;
use nom::Err;
fn parse_message(content: &[u8]) -> Response {
// type annotations needed
// cannot infer type for type parameter `E` declared on the function `pair`
// note: cannot satisfy `_: nom::error::ParseError<&[u8]>`
// help: consider specifying the type arguments in the function call: `::<I, O1, O2, E, F, G>`
let memory = pair(char('m'), many_till(anychar, eof));
// ^^^^ error
let last_memory = pair(char('l'), many_till(anychar, eof));
let message = alt((
memory,
last_memory,
));
// (
// &[u8],
// (
// char,
// (std::vec::Vec<char>, &[u8])
// )
// )
let x = message(content).unwrap();
let response = match x.1.0 {
'm' => {
Response::ReadMemory(x.1.1.0.into_iter().map(|c| c as u8).collect())
}
'l' => {
Response::LastReadMemory(x.1.1.0.into_iter().map(|c| c as u8).collect())
}
};
// let y: () =
response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment