rivescript_core/macros.rs
1//! Foreign Programming Language Object Macros
2//!
3//! This module provides the LanguageLoader trait which enables you to define
4//! a custom programming-language handler for RiveScript Object Macros written
5//! in languages other than Rust.
6//!
7//! For example, a RiveScript document might define an object macro written
8//! in JavaScript like so:
9//!
10//! ```
11//! > object reverse javascript
12//! let str = args.join(" ");
13//! return str.split('').reverse().join('');
14//! < object
15//!
16//! + reverse *
17//! - "<star>" spelled backwards is "<call>reverse <star></call>."
18//! ```
19
20/// The trait for a custom programming language loader for RiveScript object macros.
21///
22/// The load() function will receive the name of the object macro along with its
23/// source code (as lines of text). Your LanguageLoader might then parse and
24/// evaluate the code using your backing runtime or VM.
25///
26/// The call() function is invoked when a named object macro has been called via
27/// the `<call>` tag in a RiveScript reply. The `name` is the name of the object
28/// macro and the `args` are the parameters (using shell-style quoting rules, so
29/// a "quoted string" would come as one item of the `Vec<String>`).
30pub trait LanguageLoader: Send + Sync {
31 fn load(&mut self, name: &str, code: Vec<String>) -> Result<bool, String>;
32 fn call(&self, name: &str, args: Vec<String>) -> Result<String, String>;
33}