use std::time::{Duration, Instant};
use rhue::Engine;
async fn weather(city: String) -> Result<String, String> {
tokio::time::sleep(Duration::from_millis(300)).await;
match city.as_str() {
"Atlantis" => Err(format!("no station in {city}")),
_ => Ok(format!("{city}: {}°", city.len() * 3)),
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), rhue::Error> {
let mut engine = Engine::new();
engine.open_tasks();
engine.set_sleep(tokio::time::sleep);
engine.register_async("weather", weather);
engine.register("log", |s: String| println!(" {s}"));
let started = Instant::now();
engine
.eval_async(
r#"
local done = 0
for _, city in { "Adams", "Watertown", "Atlantis" } do
task.spawn(function()
local ok, report = pcall(weather, city)
log(if ok then report else "failed: " .. report)
done += 1
end)
end
while done < 3 do task.wait(0.05) end
"#,
)
.await?;
println!("three lookups in {:?}", started.elapsed());
let handle = engine.interrupt_handle();
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(200));
handle.interrupt();
});
let err = engine.eval_async("while true do end").await.unwrap_err();
println!(
"runaway script: {err} (interrupted: {})",
err.is_interrupted()
);
Ok(())
}