Created
June 19, 2018 23:12
-
-
Save Yatekii/2541664fa43c552bbb5829a6ec1aa73b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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