From 8c41e549f7953d17a237f9a39486bab99c1bfb96 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 22 Dec 2020 11:13:13 +0800 Subject: [PATCH] Add Engine::gen_fn_metadata_with_ast_to_json. --- RELEASES.md | 7 ++++--- src/serde_impl/metadata.rs | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 3de6e89a..bbdbc1ba 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -10,8 +10,9 @@ Each function defined in an `AST` can optionally attach _doc-comments_ (which, a are comments prefixed by either `///` or `/**`). Doc-comments allow third-party tools to automatically generate documentation for functions defined in a Rhai script. -A new API, `Engine::gen_fn_metadata_to_json`, paired with the new `metadata` feature, -exports the full list of functions metadata (including those in an `AST`) as a JSON document. +A new API, `Engine::gen_fn_metadata_to_json` and `Engine::gen_fn_metadata_with_ast_to_json`, +paired with the new `metadata` feature, exports the full list of functions metadata +(including those in an `AST`) as a JSON document. Bug fixes --------- @@ -36,7 +37,7 @@ New features * `AST::iter_functions` now returns `ScriptFnMetadata` which includes, among others, _doc-comments_ for functions prefixed by `///` or `/**`. * _Doc-comments_ can be enabled/disabled with the new `Engine::set_doc_comments` method. -* A new feature `metadata` is added that pulls in `serde_json` and enables `Engine::gen_fn_metadata_to_json` which exports the full list of functions metadata (including those inside an `AST`) in JSON format. +* A new feature `metadata` is added that pulls in `serde_json` and enables `Engine::gen_fn_metadata_to_json` and ``Engine::gen_fn_metadata_with_ast_to_json` which exports the full list of functions metadata (including those inside an `AST`) in JSON format. * `Engine::on_debug` provides two additional parameters: `source: Option<&str>` and `pos: Position`. * `NativeCallContext` and `EvalContext` both expose `source()` which returns the current source, if any. diff --git a/src/serde_impl/metadata.rs b/src/serde_impl/metadata.rs index ef291705..da0e6ab1 100644 --- a/src/serde_impl/metadata.rs +++ b/src/serde_impl/metadata.rs @@ -213,17 +213,17 @@ impl From<&crate::Module> for ModuleMetadata { #[cfg(feature = "serde")] impl Engine { - /// Generate a list of all functions (including those defined in an [`AST`][crate::AST], if provided) + /// Generate a list of all functions (including those defined in an [`AST`][crate::AST]) /// in JSON format. Available only under the `metadata` feature. /// /// Functions from the following sources are included: - /// 1) Functions defined in an [`AST`][crate::AST] (if provided) + /// 1) Functions defined in an [`AST`][crate::AST] /// 2) Functions registered into the global namespace /// 3) Functions in registered sub-modules /// 4) Functions in packages (optional) - pub fn gen_fn_metadata_to_json( + pub fn gen_fn_metadata_with_ast_to_json( &self, - ast: Option<&AST>, + ast: &AST, include_packages: bool, ) -> serde_json::Result { let mut global: ModuleMetadata = Default::default(); @@ -254,4 +254,15 @@ impl Engine { serde_json::to_string_pretty(&global) } + + /// Generate a list of all functions in JSON format. + /// Available only under the `metadata` feature. + /// + /// Functions from the following sources are included: + /// 1) Functions registered into the global namespace + /// 2) Functions in registered sub-modules + /// 3) Functions in packages (optional) + pub fn gen_fn_metadata_to_json(&self, include_packages: bool) -> serde_json::Result { + self.gen_fn_metadata_with_ast_to_json(&Default::default(), include_packages) + } }