Add Engine::gen_fn_metadata_with_ast_to_json.

This commit is contained in:
Stephen Chung 2020-12-22 11:13:13 +08:00
parent ff67efc6d5
commit 8c41e549f7
2 changed files with 19 additions and 7 deletions

View File

@ -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 are comments prefixed by either `///` or `/**`). Doc-comments allow third-party tools to
automatically generate documentation for functions defined in a Rhai script. 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, A new API, `Engine::gen_fn_metadata_to_json` and `Engine::gen_fn_metadata_with_ast_to_json`,
exports the full list of functions metadata (including those in an `AST`) as a JSON document. paired with the new `metadata` feature, exports the full list of functions metadata
(including those in an `AST`) as a JSON document.
Bug fixes 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 `/**`. * `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. * _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`. * `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. * `NativeCallContext` and `EvalContext` both expose `source()` which returns the current source, if any.

View File

@ -213,17 +213,17 @@ impl From<&crate::Module> for ModuleMetadata {
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl Engine { 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. /// in JSON format. Available only under the `metadata` feature.
/// ///
/// Functions from the following sources are included: /// 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 /// 2) Functions registered into the global namespace
/// 3) Functions in registered sub-modules /// 3) Functions in registered sub-modules
/// 4) Functions in packages (optional) /// 4) Functions in packages (optional)
pub fn gen_fn_metadata_to_json( pub fn gen_fn_metadata_with_ast_to_json(
&self, &self,
ast: Option<&AST>, ast: &AST,
include_packages: bool, include_packages: bool,
) -> serde_json::Result<String> { ) -> serde_json::Result<String> {
let mut global: ModuleMetadata = Default::default(); let mut global: ModuleMetadata = Default::default();
@ -254,4 +254,15 @@ impl Engine {
serde_json::to_string_pretty(&global) 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<String> {
self.gen_fn_metadata_with_ast_to_json(&Default::default(), include_packages)
}
} }