Rust Function Node

The Rust Function Node allows for execution of user-defined Rust code.

Rust Function Node

Configuration

The Rust Function Node takes only one piece of configuration: the Rust code itself.

Code

The Rust code you provide to the Rust Function Node will be run within the scope of a closure in the Embedded Workflow function. In other words, you should think of the code you are writing as if it were the body of a function. As such, syntax such as let declarations can be used and global (static and const) declarations should be avoided.

The Rust Function Node provides a payload variable to your Rust code, which contains the current payload. This variable is a mutable Serde JSON value and you may modify it. You may optionally return a status or error code of data type u8. Any value other than 0 will be sent to the imported trace log function, eea_trace.

The following example maps a list of analog input values at payload.data.analogValues to their voltage value, and then adds the mapped values to the payload path at payload.working.voltageValues:

let analogValues = payload["data"]["analogValues"].as_array().unwrap();
let voltageValues = analogValues.iter().map(|analogValue| {
    analogValue.as_f64().unwrap() * (5.0 / 1024.0)
}).collect();

payload["working"]["voltageValues"] = voltageValues;

Node Errors

The code input in the Rust Function Node editor provides Rust syntax highlighting for ease of use. Note that the editor does not have access to a compiler and thus will not check the validity of your Rust code. However, the workflow will check the validity of your Rust code on save. This is the most common error in the Rust Function Node, and details of the compiler error will show up in the error notification that pops up on the bottom of your screen.

Was this page helpful?


Still looking for help? You can also search the WEGnology Forums or submit your question there.