From b00bf8535d58cc8bad4729f82785024a921e6d33 Mon Sep 17 00:00:00 2001 From: tamasfe Date: Tue, 26 Jul 2022 13:55:10 +0200 Subject: [PATCH] fix(defs): conditional compilation and refactors --- .../definitions/builtin-operators.d.rhai | 0 src/{ => api}/definitions/builtin.d.rhai | 0 src/{ => api}/definitions/mod.rs | 31 ++++++++++++------- src/api/mod.rs | 3 ++ src/lib.rs | 9 +++--- 5 files changed, 27 insertions(+), 16 deletions(-) rename src/{ => api}/definitions/builtin-operators.d.rhai (100%) rename src/{ => api}/definitions/builtin.d.rhai (100%) rename src/{ => api}/definitions/mod.rs (91%) diff --git a/src/definitions/builtin-operators.d.rhai b/src/api/definitions/builtin-operators.d.rhai similarity index 100% rename from src/definitions/builtin-operators.d.rhai rename to src/api/definitions/builtin-operators.d.rhai diff --git a/src/definitions/builtin.d.rhai b/src/api/definitions/builtin.d.rhai similarity index 100% rename from src/definitions/builtin.d.rhai rename to src/api/definitions/builtin.d.rhai diff --git a/src/definitions/mod.rs b/src/api/definitions/mod.rs similarity index 91% rename from src/definitions/mod.rs rename to src/api/definitions/mod.rs index 1b1842ce..ee8a5cc2 100644 --- a/src/definitions/mod.rs +++ b/src/api/definitions/mod.rs @@ -4,10 +4,10 @@ use crate::{ use core::fmt; #[cfg(feature = "no_std")] -use alloc::borrow::Cow; +use std::prelude::v1::*; #[cfg(feature = "no_std")] -use alloc::string::String; +use alloc::borrow::Cow; #[cfg(not(feature = "no_std"))] use std::borrow::Cow; @@ -86,8 +86,14 @@ impl<'e> Definitions<'e> { fs::create_dir_all(path)?; - fs::write(path.join("__builtin__.d.rhai"), include_bytes!("builtin.d.rhai"))?; - fs::write(path.join("__builtin-operators__.d.rhai"), include_bytes!("builtin-operators.d.rhai"))?; + fs::write( + path.join("__builtin__.d.rhai"), + include_bytes!("builtin.d.rhai"), + )?; + fs::write( + path.join("__builtin-operators__.d.rhai"), + include_bytes!("builtin-operators.d.rhai"), + )?; fs::write(path.join("__static__.d.rhai"), self.static_module())?; @@ -95,6 +101,7 @@ impl<'e> Definitions<'e> { fs::write(path.join("__scope__.d.rhai"), self.scope())?; } + #[cfg(not(feature = "no_module"))] for (name, decl) in self.modules() { fs::write(path.join(format!("{name}.d.rhai")), decl)?; } @@ -196,13 +203,15 @@ impl Module { continue; } - f.write_definition( - writer, - def, - def.engine.custom_keywords.contains_key(&f.metadata.name) - || (!f.metadata.name.contains('$') - && !is_valid_function_name(&f.metadata.name)), - )?; + #[cfg(not(feature = "no_custom_syntax"))] + let operator = def.engine.custom_keywords.contains_key(&f.metadata.name) + || (!f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name)); + + #[cfg(feature = "no_custom_syntax")] + let operator = + !f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name); + + f.write_definition(writer, def, operator)?; } Ok(()) diff --git a/src/api/mod.rs b/src/api/mod.rs index 4f5e96e1..f24777c6 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -28,6 +28,9 @@ pub mod custom_syntax; pub mod deprecated; +#[cfg(feature = "metadata")] +pub mod definitions; + use crate::{Dynamic, Engine, Identifier}; #[cfg(not(feature = "no_custom_syntax"))] diff --git a/src/lib.rs b/src/lib.rs index cc3b6d7d..dcf1aa88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,8 +82,6 @@ mod reify; mod tests; mod tokenizer; mod types; -#[cfg(feature = "metadata")] -mod definitions; /// Error encountered when parsing a script. type PERR = ParseErrorType; @@ -183,6 +181,10 @@ pub use types::{ #[cfg(not(feature = "no_custom_syntax"))] pub use api::custom_syntax::Expression; + +#[cfg(feature = "metadata")] +pub use api::definitions::Definitions; + /// _(debugging)_ Module containing types for debugging. /// Exported under the `debugging` feature only. #[cfg(feature = "debugging")] @@ -260,9 +262,6 @@ pub mod serde; #[cfg(not(feature = "no_optimize"))] pub use optimizer::OptimizationLevel; -#[cfg(feature = "metadata")] -pub use definitions::Definitions; - /// Placeholder for the optimization level. #[cfg(feature = "no_optimize")] pub type OptimizationLevel = ();