OXYGENHUB
# dfbc3e972872 oxygenhub-android / rust-core / src / lib.rs
2.5 KiB · Rust · last changed The phone as an OxygenFlow runner, first part (refs #8) 10 days ago source blame history raw
1
uniffi::setup_scaffolding!();
2
3
use std::path::Path;
4
5
// A simple test function to ensure the Kotlin <-> Rust bridge is working.
6
#[uniffi::export]
7
pub fn oxygen_greeting(name: String) -> String {
8
    format!("Hello, {}! Oxygen Native Core is active.", name)
9
}
10
11
/// Why a runner call failed, in OxygenFlow's words.
12
#[derive(Debug, uniffi::Error)]
13
pub enum RunnerError {
14
    Failed { reason: String },
15
}
16
17
impl std::fmt::Display for RunnerError {
18
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19
        match self {
20
            RunnerError::Failed { reason } => f.write_str(reason),
21
        }
22
    }
23
}
24
25
impl std::error::Error for RunnerError {}
26
27
fn failed(e: anyhow::Error) -> RunnerError {
28
    RunnerError::Failed { reason: format!("{e:#}") }
29
}
30
31
/// The phone as an OxygenFlow runner (oxygenhub-android#8): OxygenFlow's
32
/// own runner, built into the app, offering only the WASM tier. `home` is
33
/// the app's directory for it; `register` keeps the runner's credential
34
/// there (runner.toml), as `flow runner register` does on a host.
35
#[uniffi::export]
36
pub fn runner_register(home: String, hub: String, token: String, name: String) -> Result<String, RunnerError> {
37
    let h = flow::home::Home::locate(Some(Path::new(&home))).map_err(failed)?;
38
    let r = flow::runner::register(&h, &hub, &token, &name, None, vec!["android".into()]).map_err(failed)?;
39
    Ok(r.name)
40
}
41
42
/// Whether `home` holds a registration, and so can serve.
43
#[uniffi::export]
44
pub fn runner_is_registered(home: String) -> bool {
45
    Path::new(&home).join(flow::home::RUNNER_TOML).is_file()
46
}
47
48
/// Claim and run jobs until [`runner_stop`] drains the runner: returns
49
/// once the jobs it holds are done and reported. Blocks, so call it from
50
/// a thread of its own (the app's runner service).
51
#[uniffi::export]
52
pub fn runner_serve(home: String) -> Result<(), RunnerError> {
53
    let h = flow::home::Home::locate(Some(Path::new(&home))).map_err(failed)?;
54
    flow::runner::serve_embedded(&h).map_err(failed)
55
}
56
57
/// Drain the runner: claim nothing more, finish and report what it
58
/// holds, then [`runner_serve`] returns. For leaving the charger or the
59
/// allowed network, or the owner stopping it.
60
#[uniffi::export]
61
pub fn runner_stop() {
62
    flow::runner::stop();
63
}
64
65
/// The tiers this phone offers (the WASM tier), and why not the others.
66
#[uniffi::export]
67
pub fn runner_tiers(home: String) -> Vec<String> {
68
    let Ok(h) = flow::home::Home::locate(Some(Path::new(&home))) else { return vec![] };
69
    match flow::runner::tiers(&h) {
70
        Ok((have, _)) => have,
71
        Err(_) => vec![],
72
    }
73
}