From e825007ebb01e2c1d8a52cee90b8c9b265f5e0e3 Mon Sep 17 00:00:00 2001 From: arkhon7 Date: Sat, 4 Mar 2023 17:16:57 +0800 Subject: [PATCH] more fixes --- src/parser.rs | 12 ++++++++---- src/utils.rs | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 559f129..e2de6cc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,7 +2,7 @@ use nom::{ branch::alt, bytes::streaming::tag, character::complete::char, - combinator::{all_consuming, complete}, + combinator::{all_consuming, complete, eof}, multi::many0, number::complete::float, sequence::{delimited, preceded, separated_pair, terminated, tuple}, @@ -33,7 +33,9 @@ pub fn value(input: &str) -> IResult<&str, f32, nom::error::Error<&str>> { parse(input) } -pub fn keyword<'a>(name: &'a str) -> impl FnMut(&'a str) -> IResult<&str, &str, nom::error::Error<&str>> { +pub fn keyword<'a>( + name: &'a str, +) -> impl FnMut(&'a str) -> IResult<&str, &str, nom::error::Error<&str>> { alt(( terminated(tag(name), whitespaces), delimited(whitespaces, tag(name), whitespaces), @@ -42,7 +44,9 @@ pub fn keyword<'a>(name: &'a str) -> impl FnMut(&'a str) -> IResult<&str, &str, )) } -pub fn reading<'a>(name: &'a str) -> impl FnMut(&'a str) -> IResult<&str, (&str, f32), nom::error::Error<&str>> { +pub fn reading<'a>( + name: &'a str, +) -> impl FnMut(&'a str) -> IResult<&str, (&str, f32), nom::error::Error<&str>> { return separated_pair(keyword(name), char(':'), value); } @@ -67,7 +71,7 @@ pub fn readings(input: &str) -> IResult<&str, Readings, nom::error::Error<&str>> } pub fn consume(input: &str) -> IResult<&str, Readings, nom::error::Error<&str>> { - let mut parse = all_consuming(readings); + let mut parse = all_consuming(terminated(readings, eof)); parse(input) } diff --git a/src/utils.rs b/src/utils.rs index 808132a..b30652e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -148,7 +148,7 @@ pub fn start(baud_rate: u32, timeout: u64) { let mut to_resolve: Vec = Vec::new(); let mut buf = [0; 32]; - + loop { match open_port(OS, baud_rate, timeout) { Ok(mut port) => { @@ -159,7 +159,7 @@ pub fn start(baud_rate: u32, timeout: u64) { let marker = b'$'; // splitting symbol // filter null bytes (unix) - let buffer = &buf[0..num] + let buffer = &buf .iter() .filter_map(|&b| if b != 0 { Some(b) } else { None }) .collect::>();