From 02ef119603785a7280bb0e6dbd13bccce09d50cb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 23 Nov 2022 17:23:54 +0800 Subject: [PATCH] Fix builds. --- src/api/compile.rs | 3 +- src/api/eval.rs | 5 ++-- src/api/json.rs | 3 +- src/api/register.rs | 5 ++-- src/api/run.rs | 3 +- src/module/mod.rs | 2 +- src/serde/deserialize.rs | 44 +++++++++++++++++++++++----- src/serde/ser.rs | 63 ++++++++++++++++++++++------------------ tests/custom_syntax.rs | 20 +++++++------ 9 files changed, 92 insertions(+), 56 deletions(-) diff --git a/src/api/compile.rs b/src/api/compile.rs index 0f14e199..e031820b 100644 --- a/src/api/compile.rs +++ b/src/api/compile.rs @@ -1,7 +1,8 @@ //! Module that defines the public compilation API of [`Engine`]. use crate::parser::{ParseResult, ParseState}; -use crate::{Engine, OptimizationLevel, Scope, StringsInterner, AST}; +use crate::types::StringsInterner; +use crate::{Engine, OptimizationLevel, Scope, AST}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/api/eval.rs b/src/api/eval.rs index 5c2078d2..fd248f58 100644 --- a/src/api/eval.rs +++ b/src/api/eval.rs @@ -2,10 +2,9 @@ use crate::eval::{Caches, GlobalRuntimeState}; use crate::parser::ParseState; -use crate::types::dynamic::Variant; +use crate::types::{dynamic::Variant, StringsInterner}; use crate::{ - reify, Dynamic, Engine, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, - StringsInterner, AST, ERR, + reify, Dynamic, Engine, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/api/json.rs b/src/api/json.rs index 65d37719..036db0b3 100644 --- a/src/api/json.rs +++ b/src/api/json.rs @@ -3,7 +3,8 @@ use crate::parser::{ParseSettingFlags, ParseState}; use crate::tokenizer::Token; -use crate::{Engine, LexError, Map, OptimizationLevel, RhaiResultOf, Scope, StringsInterner}; +use crate::types::StringsInterner; +use crate::{Engine, LexError, Map, OptimizationLevel, RhaiResultOf, Scope}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/api/register.rs b/src/api/register.rs index 0ff7dedf..7f590266 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -1,7 +1,6 @@ //! Module that defines the public function/module registration API of [`Engine`]. use crate::func::{FnCallArgs, RegisterNativeFunction, SendSync}; -use crate::module::ModuleFlags; use crate::types::dynamic::Variant; use crate::{ Engine, FnAccess, FnNamespace, Identifier, Module, NativeCallContext, RhaiResultOf, Shared, @@ -743,9 +742,9 @@ impl Engine { } let exclude_flags = if include_packages { - ModuleFlags::INTERNAL + crate::module::ModuleFlags::INTERNAL } else { - ModuleFlags::INTERNAL | ModuleFlags::STANDARD_LIB + crate::module::ModuleFlags::INTERNAL | crate::module::ModuleFlags::STANDARD_LIB }; signatures.extend( diff --git a/src/api/run.rs b/src/api/run.rs index f5fa71e8..0bc418df 100644 --- a/src/api/run.rs +++ b/src/api/run.rs @@ -2,7 +2,8 @@ use crate::eval::{Caches, GlobalRuntimeState}; use crate::parser::ParseState; -use crate::{Engine, RhaiResultOf, Scope, StringsInterner, AST}; +use crate::types::StringsInterner; +use crate::{Engine, RhaiResultOf, Scope, AST}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/module/mod.rs b/src/module/mod.rs index e02a77db..68fe9f2e 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -311,7 +311,7 @@ impl Module { dynamic_functions_filter: BloomFilterU64::new(), type_iterators: None, all_type_iterators: None, - flags: ModuleFlags::empty(), + flags: ModuleFlags::INDEXED, } } diff --git a/src/serde/deserialize.rs b/src/serde/deserialize.rs index 680632c6..d8593c5c 100644 --- a/src/serde/deserialize.rs +++ b/src/serde/deserialize.rs @@ -1,7 +1,6 @@ //! Implementations of [`serde::Deserialize`]. use crate::{Dynamic, Identifier, ImmutableString, Scope, INT}; -use num_traits::FromPrimitive; use serde::{ de::{Error, SeqAccess, Visitor}, Deserialize, Deserializer, @@ -10,6 +9,9 @@ use std::fmt; #[cfg(feature = "no_std")] use std::prelude::v1::*; +#[cfg(feature = "decimal")] +use num_traits::FromPrimitive; + struct DynamicVisitor; impl<'de> Visitor<'de> for DynamicVisitor { @@ -56,7 +58,7 @@ impl<'de> Visitor<'de> for DynamicVisitor { #[cfg(not(feature = "no_float"))] return Ok(Dynamic::from_float(v as crate::FLOAT)); - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } } #[inline] @@ -75,7 +77,7 @@ impl<'de> Visitor<'de> for DynamicVisitor { #[cfg(not(feature = "no_float"))] return Ok(Dynamic::from_float(v as crate::FLOAT)); - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } } #[inline(always)] @@ -106,7 +108,7 @@ impl<'de> Visitor<'de> for DynamicVisitor { #[cfg(not(feature = "no_float"))] return Ok(Dynamic::from_float(v as crate::FLOAT)); - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } } #[inline] @@ -124,7 +126,7 @@ impl<'de> Visitor<'de> for DynamicVisitor { return Ok(Dynamic::from_float(v as crate::FLOAT)); #[allow(unreachable_code)] - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } #[inline] fn visit_u128(self, v: u128) -> Result { @@ -141,18 +143,44 @@ impl<'de> Visitor<'de> for DynamicVisitor { return Ok(Dynamic::from_float(v as crate::FLOAT)); #[allow(unreachable_code)] - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } #[cfg(not(feature = "no_float"))] #[inline(always)] fn visit_f32(self, v: f32) -> Result { - Ok(crate::FLOAT::from(v).into()) + #[cfg(not(feature = "no_float"))] + return Ok((v as crate::FLOAT).into()); + + #[allow(unreachable_code)] + { + #[cfg(feature = "decimal")] + if let Some(n) = rust_decimal::Decimal::from_f32(v) { + return Ok(Dynamic::from_decimal(n)); + } + + Err(Error::custom(format!( + "floating-point number is not supported: {v}" + ))) + } } #[cfg(not(feature = "no_float"))] #[inline(always)] fn visit_f64(self, v: f64) -> Result { - Ok(crate::FLOAT::from(v).into()) + #[cfg(not(feature = "no_float"))] + return Ok((v as crate::FLOAT).into()); + + #[allow(unreachable_code)] + { + #[cfg(feature = "decimal")] + if let Some(n) = rust_decimal::Decimal::from_f64(v) { + return Ok(Dynamic::from_decimal(n)); + } + + Err(Error::custom(format!( + "floating-point number is not supported: {v}" + ))) + } } #[cfg(feature = "no_float")] diff --git a/src/serde/ser.rs b/src/serde/ser.rs index a6e7da68..8c85f4d9 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -1,7 +1,6 @@ //! Implement serialization support of [`Dynamic`][crate::Dynamic] for [`serde`]. use crate::{Dynamic, Identifier, Position, RhaiError, RhaiResult, RhaiResultOf, ERR, INT}; -use num_traits::FromPrimitive; use serde::ser::{ Error, SerializeMap, SerializeSeq, SerializeStruct, SerializeTuple, SerializeTupleStruct, }; @@ -10,6 +9,9 @@ use std::fmt; #[cfg(feature = "no_std")] use std::prelude::v1::*; +#[cfg(feature = "decimal")] +use num_traits::FromPrimitive; + /// Serializer for [`Dynamic`][crate::Dynamic]. pub struct DynamicSerializer { /// Buffer to hold a temporary key. @@ -146,7 +148,7 @@ impl Serializer for &mut DynamicSerializer { #[cfg(not(feature = "no_float"))] return Ok(Dynamic::from_float(v as crate::FLOAT)); - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } } @@ -166,7 +168,7 @@ impl Serializer for &mut DynamicSerializer { #[cfg(not(feature = "no_float"))] return Ok(Dynamic::from_float(v as crate::FLOAT)); - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } } @@ -200,7 +202,7 @@ impl Serializer for &mut DynamicSerializer { #[cfg(not(feature = "no_float"))] return Ok(Dynamic::from_float(v as crate::FLOAT)); - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } } @@ -219,7 +221,7 @@ impl Serializer for &mut DynamicSerializer { return Ok(Dynamic::from_float(v as crate::FLOAT)); #[allow(unreachable_code)] - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } #[inline] @@ -237,40 +239,43 @@ impl Serializer for &mut DynamicSerializer { return Ok(Dynamic::from_float(v as crate::FLOAT)); #[allow(unreachable_code)] - Err(Error::custom(format!("integer number too large: {}", v))) + Err(Error::custom(format!("integer number too large: {v}"))) } - #[cfg(not(feature = "no_float"))] #[inline(always)] fn serialize_f32(self, v: f32) -> RhaiResultOf { - Ok(crate::FLOAT::from(v).into()) + #[cfg(not(feature = "no_float"))] + return Ok((v as crate::FLOAT).into()); + + #[allow(unreachable_code)] + { + #[cfg(feature = "decimal")] + if let Some(n) = rust_decimal::Decimal::from_f32(v) { + return Ok(Dynamic::from_decimal(n)); + } + + Err(Error::custom(format!( + "floating-point number is not supported: {v}" + ))) + } } - #[cfg(not(feature = "no_float"))] #[inline(always)] fn serialize_f64(self, v: f64) -> RhaiResultOf { - Ok(crate::FLOAT::from(v).into()) - } + #[cfg(not(feature = "no_float"))] + return Ok((v as crate::FLOAT).into()); - #[cfg(feature = "no_float")] - #[cfg(feature = "decimal")] - #[inline] - fn serialize_f32(self, v: f32) -> RhaiResultOf { - use std::convert::TryFrom; + #[allow(unreachable_code)] + { + #[cfg(feature = "decimal")] + if let Some(n) = rust_decimal::Decimal::from_f64(v) { + return Ok(Dynamic::from_decimal(n)); + } - rust_decimal::Decimal::try_from(v) - .map(|v| v.into()) - .map_err(Error::custom) - } - #[cfg(feature = "no_float")] - #[cfg(feature = "decimal")] - #[inline] - fn serialize_f64(self, v: f64) -> RhaiResultOf { - use std::convert::TryFrom; - - rust_decimal::Decimal::try_from(v) - .map(|v| v.into()) - .map_err(Error::custom) + Err(Error::custom(format!( + "floating-point number is not supported: {v}" + ))) + } } #[inline(always)] diff --git a/tests/custom_syntax.rs b/tests/custom_syntax.rs index c3a5d0fc..899d83a9 100644 --- a/tests/custom_syntax.rs +++ b/tests/custom_syntax.rs @@ -266,16 +266,18 @@ fn test_custom_syntax_raw() -> Result<(), Box> { *state = Dynamic::FALSE; Ok(Some("$ident$".into())) } - 2 => match stream[1].as_str() { - "world" if state.as_bool().unwrap_or(false) => Ok(Some("$$world".into())), - "world" => Ok(Some("$$hello".into())), - "kitty" => { - *state = (42 as INT).into(); - Ok(None) + 2 => { + match stream[1].as_str() { + "world" if state.as_bool().unwrap_or(false) => Ok(Some("$$world".into())), + "world" => Ok(Some("$$hello".into())), + "kitty" => { + *state = (42 as INT).into(); + Ok(None) + } + s => Err(LexError::ImproperSymbol(s.to_string(), String::new()) + .into_err(Position::NONE)), } - s => Err(LexError::ImproperSymbol(s.to_string(), String::new()) - .into_err(Position::NONE)), - }, + } _ => unreachable!(), }, true,