WASM Module Guide
Menu & Ordering Module
This guide demonstrates how to process incoming JSON payloads (orders) and append them to a Kitchen Display System (KDS) queue.
#Architecture Flow
Instead of a single counter, a KDS requires a list of pending orders. Since state::set overwrites keys entirely, the module must append the new order to an existing list, or use a unique ID for the key (e.g., order:1234).
src/lib.rs
#![no_std]
use qos_sdk::{state, input, output};
#[no_mangle]
pub extern "C" fn run() -> i32 {
let payload = input::read().unwrap_or_default();
// Fetch current order index
let mut index: u32 = 0;
if let Some(val) = state::get("menu:order_index") {
if val.len() == 4 {
let mut buf = [0u8; 4];
buf.copy_from_slice(&val);
index = u32::from_le_bytes(buf) + 1;
}
}
// Update index
state::set("menu:order_index", &index.to_le_bytes()).unwrap();
// Save order payload to a unique key
// We format the string manually since no_std prevents String::format
// For simplicity in this example, we assume order ID matches the index.
// In reality, use a tiny string formatting crate.
let key = "menu:order_pending";
state::set(key, &payload).unwrap();
output::write(b"{\"status\":\"sent_to_kitchen\"}");
0
}💡 TIP
When an order is saved, the
STATE_MUTATION event is sent to the Admin HUD immediately, allowing the kitchen screen to populate in real-time without polling.