Rust
Provide data from Rust while composing the UI in Contract.
Start with an Exact Rust host
Install @rightui/ui and its font using Installation. Your app also needs an Exact host configured for a Rust DataSource. Use the Exact Rust data source documentation and its Sparkline example for the host and Cargo configuration. The example below shows the data source and UI; it is not a standalone application.
Declare the Contract
Use this as app.contract. The greeting resource will be answered by Rust rather than app.ts. The component import is the same as in a TypeScript app.
contract
use defaultFontWeights from "@rightui/ui"
font "Geist"
100 900 = "assets/fonts/Geist.ttf"
100 900 italic = "assets/fonts/Geist-Italic.ttf"
font "Newsreader"
200 800 = "assets/fonts/Newsreader.ttf"
200 800 italic = "assets/fonts/Newsreader-Italic.ttf"
use uiFontFamily, Button from "@rightui/ui"
shape Greeting
label: string
component App
provide
uiFont = "theme"
uiWeights = defaultFontWeights()
resource greeting = greeting() as shape Greeting
state count = 0
action increment
count = count + 1
view
column padding=24 gap=12 color="ui.foreground" background-color="ui.background"
text greeting.label font-family=uiFontFamily("theme") data-rightui-font="body"
text count.toString() testId="counter" font-family=uiFontFamily("theme") data-rightui-font="body"
Button(id="increment", label="Increment", variant="primary", disabled=false, press=increment)
Implement the data source
Add this implementation to your data crate. Set app_id() to your application ID. Value record fields must follow the order of shape Greeting in Contract.
rust
use exact_plan::Value;
use exact_runner::{DataError, DataSource};
/// Field order matches `shape Greeting` in app.contract.
#[derive(Debug, PartialEq)]
pub struct Greeting {
pub label: String,
}
impl Greeting {
pub fn into_value(self) -> Value {
Value::record(vec![Value::str(&self.label)])
}
}
#[derive(Default)]
pub struct Greetings;
impl DataSource for Greetings {
fn app_id(&self) -> &str { "dev.rightui.example" }
fn query(&mut self, source: &str, _: &[Value]) -> Result<Value, DataError> {
match source {
"greeting" => Ok(Greeting { label: "Hello from Rust".into() }.into_value()),
_ => Err(DataError::UnknownSource(source.into())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn queries_match_the_contract_record_and_reject_unknown_sources() {
let mut data = Greetings;
assert_eq!(data.query("greeting", &[]).unwrap(),
Value::record(vec![Value::str("Hello from Rust")]));
assert!(matches!(data.query("missing", &[]),
Err(DataError::UnknownSource(name)) if name == "missing"));
assert_eq!(data.app_id(), "dev.rightui.example");
}
}
Connect the data source
Register Greetings with your Exact host for both initial rendering and runtime queries, following the host configuration above. Keep exact-plan and exact-runner on the same SDK revision as your app. Rightui supplies Contract components; rendering and Rust host integration are provided by Exact.