Skip to content

Instantly share code, notes, and snippets.

@kali
Created July 3, 2019 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kali/dad3c1b4b6fa69782f3edab9d0a22b28 to your computer and use it in GitHub Desktop.
Save kali/dad3c1b4b6fa69782f3edab9d0a22b28 to your computer and use it in GitHub Desktop.
// should recognize:
// input-node name=foo dim=12
// i want to assume both are here, but not necessarily in this order
// i have similar case with more numrous foo=bar cases
fn parse_input_node_line(i: &str) -> IResult<&str, (String, usize)> {
use crate::parser::spaced;
use std::cell::Cell;
let name: Cell<Option<String>> = Cell::new(None);
let dim: Cell<Option<usize>> = Cell::new(None);
let (i, _) = tag("input-node")(i)?;
let (i, _) = many1(spaced(alt((
map(preceded(tag("name="), identifier), |n| {
name.set(Some(n.to_string()));
}),
map(preceded(tag("dim="), digit1), |n: &str| {
dim.set(Some(n.parse().unwrap()));
}),
))))(i)?;
match (name.into_inner(), dim.into_inner()) {
(Some(name), Some(dim)) => Ok((i, (name, dim))),
(None, _) => panic!("expect name"),
(_, None) => panic!("expect dim"),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment