From e5b4789304f33aac6ae707702fe0c1a159a5e649 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 19 Mar 2020 19:51:59 +0100 Subject: [PATCH] replace dependency lexical-core with num-traits --- Cargo.lock | 43 +++++++++++++++++++++++---------- Cargo.toml | 2 +- src/ad7172/mod.rs | 2 +- src/command_parser.rs | 56 +++++++++++++++++++++++++++++++++---------- src/steinhart_hart.rs | 2 +- 5 files changed, 78 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d4776a..681acb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,12 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + [[package]] name = "bare-metal" version = "0.2.5" @@ -149,14 +155,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "857afb5ee9e767c3a73b2ad7212b6deea0c3761a27db1e20ea0ed57ee352cfef" [[package]] -name = "lexical-core" -version = "0.7.4" +name = "libm" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616" -dependencies = [ - "bitflags", - "cfg-if", -] +checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" [[package]] name = "log" @@ -195,6 +197,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "num-traits" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" +dependencies = [ + "autocfg", + "libm", +] + [[package]] name = "panic-abort" version = "0.3.2" @@ -235,6 +247,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f" +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" + [[package]] name = "rustc_version" version = "0.2.3" @@ -290,9 +308,9 @@ dependencies = [ [[package]] name = "stm32f4" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88640ad08c62e0651a1320187f38c3655d025ed580a10f0e4d85a2cc4829069f" +checksum = "44a3d6c58b14e63926273694e7dd644894513c5e35ce6928c4657ddb62cae976" dependencies = [ "bare-metal", "cortex-m", @@ -302,8 +320,8 @@ dependencies = [ [[package]] name = "stm32f4xx-hal" -version = "0.6.0" -source = "git+https://github.com/thalesfragoso/stm32f4xx-hal?branch=pwm-impl#ef939935b90581553dc03f9146d05510b3ceba58" +version = "0.7.0" +source = "git+https://github.com/thalesfragoso/stm32f4xx-hal?branch=pwm-impl#cfd073e094daa9be9dd2b0a1f859a4e1c6be2b77" dependencies = [ "bare-metal", "cast", @@ -311,6 +329,7 @@ dependencies = [ "cortex-m-rt", "embedded-hal", "nb", + "rand_core", "stm32f4", "void", ] @@ -338,9 +357,9 @@ dependencies = [ "cortex-m-rt", "embedded-hal", "hash2hwaddr", - "lexical-core", "log", "nom", + "num-traits", "panic-abort", "panic-semihosting", "smoltcp", diff --git a/Cargo.toml b/Cargo.toml index f1555b2..3211541 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ hash2hwaddr = { version = "0.0", optional = true } bit_field = "0.10" byteorder = { version = "1", default-features = false } nom = { version = "5", default-features = false } -lexical-core = { version = "0.7", default-features = false } +num-traits = { version = "0.2", default-features = false, features = ["libm"] } [features] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] diff --git a/src/ad7172/mod.rs b/src/ad7172/mod.rs index 43d3e2f..9068cda 100644 --- a/src/ad7172/mod.rs +++ b/src/ad7172/mod.rs @@ -1,5 +1,5 @@ use core::fmt; -use lexical_core::Float; +use num_traits::float::Float; use stm32f4xx_hal::{ time::{MegaHertz, U32Ext}, spi, diff --git a/src/command_parser.rs b/src/command_parser.rs index d075faa..f803d80 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -1,4 +1,6 @@ use core::fmt; +use core::num::ParseIntError; +use core::str::{from_utf8, Utf8Error}; use nom::{ IResult, branch::alt, @@ -9,7 +11,7 @@ use nom::{ multi::{fold_many0, fold_many1}, error::ErrorKind, }; -use lexical_core as lexical; +use num_traits::{Num, ParseFloatError}; #[derive(Clone, Debug, PartialEq)] @@ -17,7 +19,10 @@ pub enum Error { Parser(ErrorKind), Incomplete, UnexpectedInput(u8), - ParseNumber(lexical::Error) + Utf8(Utf8Error), + ParseInt(ParseIntError), + // `num_traits::ParseFloatError` does not impl Clone + ParseFloat, } impl<'t> From> for Error { @@ -33,9 +38,21 @@ impl<'t> From> for Error { } } -impl From for Error { - fn from(e: lexical::Error) -> Self { - Error::ParseNumber(e) +impl From for Error { + fn from(e: Utf8Error) -> Self { + Error::Utf8(e) + } +} + +impl From for Error { + fn from(e: ParseIntError) -> Self { + Error::ParseInt(e) + } +} + +impl From for Error { + fn from(_: ParseFloatError) -> Self { + Error::ParseFloat } } @@ -52,10 +69,17 @@ impl fmt::Display for Error { "parser: ".fmt(fmt)?; (e as &dyn core::fmt::Debug).fmt(fmt) } - Error::ParseNumber(e) => { - "parsing number: ".fmt(fmt)?; + Error::Utf8(e) => { + "utf8: ".fmt(fmt)?; (e as &dyn core::fmt::Debug).fmt(fmt) } + Error::ParseInt(e) => { + "parsing int: ".fmt(fmt)?; + (e as &dyn core::fmt::Debug).fmt(fmt) + } + Error::ParseFloat => { + "parsing float".fmt(fmt) + } } } } @@ -157,8 +181,12 @@ fn whitespace(input: &[u8]) -> IResult<&[u8], ()> { fn unsigned(input: &[u8]) -> IResult<&[u8], Result> { take_while1(is_digit)(input) .map(|(input, digits)| { - let result = lexical::parse(digits) - .map_err(|e| e.into()); + let result = + from_utf8(digits) + .map_err(|e| e.into()) + .and_then(|digits| u32::from_str_radix(digits, 10) + .map_err(|e| e.into()) + ); (input, result) }) } @@ -167,9 +195,13 @@ fn float(input: &[u8]) -> IResult<&[u8], Result> { let (input, sign) = opt(is_a("-"))(input)?; let negative = sign.is_some(); let (input, digits) = take_while1(|c| is_digit(c) || c == '.' as u8)(input)?; - let result = lexical::parse(digits) - .map(|result: f64| if negative { -result } else { result }) - .map_err(|e| e.into()); + let result = + from_utf8(digits) + .map_err(|e| e.into()) + .and_then(|digits| f64::from_str_radix(digits, 10) + .map_err(|e| e.into()) + ) + .map(|result: f64| if negative { -result } else { result }); Ok((input, result)) } diff --git a/src/steinhart_hart.rs b/src/steinhart_hart.rs index 792e857..fd3b099 100644 --- a/src/steinhart_hart.rs +++ b/src/steinhart_hart.rs @@ -1,4 +1,4 @@ -use lexical_core::Float; +use num_traits::float::Float; /// Steinhart-Hart equation parameters #[derive(Clone, Debug)]