3 unstable releases
new 0.96.2 | Feb 11, 2025 |
---|---|
0.96.0 | Jan 10, 2025 |
0.95.0 | Jan 8, 2025 |
#905 in Encoding
103 downloads per month
Used in rusp
510KB
11K
SLoC
rhai-rusp
rhai-rusp
is a new approach to address your USP protocol needs by
providing Rhai bindings for the rusp-lib crate, thus providing scripting capabilities
to USP in Rhai for either standalone runners or Rust applications which can
embed the Rhai interpreter together with the rhai-rusp
bindings.
How to embed rhai-rusp
?
rhai-rusp
can be used as a library in your own Rust applications to embed a
Rhai interpreter. To use rhai-rusp
as a library, you simply need to add the
rhai-rusp
crate to your Cargo.toml
as dependency:
[dependencies]
rhai-rusp = "0.96"
The usual steps to embed a Rhai interpreter with rusp support are:
- Initialize a Rhai engine via
rhai::Engine::new()
- Add the
rusp
bindings into the namespace perengine.register_static_module("rusp", RuspPackage::new().as_shared_module())
- Compile the
Rhai
code into an AST viaengine.compile(contents)
- Execute the
Rhai
AST viaengine.run_ast(ast)
- Handle errors, err... profit!
There's also the rusp-run
binary as part of the rusp crate which you can install via:
# cargo install rusp
Example Rhai scripts
The documentation provides plenty of examples inside the various modules of how to use them. But here are some practical examples anyway to give you a taste:
let msg = rusp::load_msg("msg.pb");
print(msg);
loads and prints a Protobuf encoded USP Message as a readable JSON structure.
let record = rusp::load_record("record.pb");
print(record.to_c_array());
loads and prints a Protobuf encoded USP Record as a C array which can e.g. be embedded into unit tests.
let body = rusp::get_builder()
.with_params(["Device."])
.with_max_depth(2)
.build();
let msg = rusp::msg_builder()
.with_msg_id("Foo")
.with_body(body)
.build();
let record = rusp::record_builder()
.with_version("1.3")
.with_to_id("proto::to")
.with_from_id("proto::from")
.with_no_session_context_payload(msg)
.build();
builds a body with a Get request, wraps it in a USP Msg and encapsulates that in a USP Record.
What else?
You may use this crate however you like under the BSD 3-Clause Licence.
Feel free to spread the word or drop us a note if you like it. Collaboration on this crate is highly welcome as are pull requests in our GitHub repo.
Contact us
If you are in need of software for USP management software (agent, controller or testing) or expertise please get in touch with us. We're also happy to solve all other device management and monitoring needs!
Licence
Dependencies
~6.5MB
~121K SLoC