Skip to content

Instantly share code, notes, and snippets.

@Yatekii
Created June 19, 2018 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Yatekii/2541664fa43c552bbb5829a6ec1aa73b to your computer and use it in GitHub Desktop.
Save Yatekii/2541664fa43c552bbb5829a6ec1aa73b to your computer and use it in GitHub Desktop.
use nom::types::CompleteStr;
use std::str;
use nom::space;
use nom::types::CompleteByteSlice;
use geometry::SchemaPoint2D;
fn bytes_to_utf8(c: CompleteByteSlice) -> Result<CompleteStr, str::Utf8Error> {
str::from_utf8(c.0).map(|i| { CompleteStr(i) })
}
/// Parses a general utf8 string
named!(pub utf8_str(CompleteByteSlice) -> CompleteStr,
map_res!(
take_until_either!(" \r\n"),
bytes_to_utf8
)
);
/// Parses a utf8 numberstring value to float
named!(pub coordinate(CompleteByteSlice) -> f32,
map_res!(number_str, { |i: CompleteStr| i.parse() })
);
named!(number_str<CompleteStr>,
map_res!(do_parse!(b: take_while!(is_number_char) >> (str::from_utf8(b))), |r| r)
);
fn is_number_char(c: u8) -> bool {
((c >= '0' as u8) && (c <= '9' as u8)) || c == '-' as u8 || c == '.' as u8
}
named!(pub point<SchemaPoint2D>,
do_parse!(
x: coordinate >>
space >>
y: coordinate >>
(SchemaPoint2D::new(x,y))
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment