Crates.io | elm-rust-binding |
lib.rs | elm-rust-binding |
version | 0.2.0 |
source | src |
created_at | 2024-11-18 20:28:23.691153 |
updated_at | 2024-11-19 09:34:22.626792 |
description | Call Elm functions from Rust in an ergonomic way |
homepage | https://github.com/anmolitor/elm-rust-binding |
repository | https://github.com/anmolitor/elm-rust-binding |
max_upload_size | |
id | 1452629 |
size | 25,489 |
This crate offers a way to conveniently call an Elm function from Rust. The main motivation here is for testing purposes:
Then you can call your Elm code in a Rust test with this crate to verify the two implementations are in sync. The performance this crate offers is NOT optimal - do not use this for Interop in production code.
use elm_rust_binding::{ElmRoot, Result};
#[test]
fn call_elm() -> Result<()> {
let mut elm_root = ElmRoot::new("../frontend/src")?;
let mut square_fn = elm_root.prepare("Math.square")?;
let squared = square_fn.call(4)?;
assert_eq!(squared, 16);
Ok(())
}
-- In /frontend/src/Math.elm
module Math exposing (square)
square : Int -> Int
square n = n * n
Note that you can call the function multiple times, which will improve performance over creating a new ElmRoot
and ElmFunctionHandle
.
This is especially useful for fuzz/property-based testing
How does this work under the hood? It essentially boils down to 6 steps:
Platform.worker
main function. It will accept the input type over its flags and return the output type via a port. Internally it will call the specified function by importing it..call
is invoked by passing the input argument as flags and getting the output via .subscribe
.The generated files are removed automatically and are postfixed with a UUID to prevent different invocations messing with each other.
If you call .debug()
on the ElmRoot
, the files are not removed to help debugging issues.