OXYGENHUB
# 74bbbc259910 oxygenhub-android / rust-core / src / lib.rs
2.8 KiB · Rust · last changed The phone runner clones: its oxy home is in app storage, and a change to OxygenFlow rebuilds rust-core (refs #8) 10 days ago source blame history raw
57c08243 Andy McCall ↶ 1
uniffi::setup_scaffolding!();
2
4aee5293 Andy McCall ↶ 3
use std::path::Path;
4
5
// A simple test function to ensure the Kotlin <-> Rust bridge is working.
57c08243 Andy McCall ↶ 6
#[uniffi::export]
7
pub fn oxygen_greeting(name: String) -> String {
8
    format!("Hello, {}! Oxygen Native Core is active.", name)
4aee5293 Andy McCall ↶ 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)
57c08243 Andy McCall ↶ 40
}
41
4aee5293 Andy McCall ↶ 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)?;
74bbbc25 Andy McCall ↶ 54
    // An app has no HOME and runs in /, where oxy-core's clone would make
55
    // its own directory: keep it with the runner's.
56
    if std::env::var_os("OXY_HOME").is_none() {
57
        std::env::set_var("OXY_HOME", Path::new(&home).join("oxy"));
58
    }
4aee5293 Andy McCall ↶ 59
    flow::runner::serve_embedded(&h).map_err(failed)
60
}
61
62
/// Drain the runner: claim nothing more, finish and report what it
63
/// holds, then [`runner_serve`] returns. For leaving the charger or the
64
/// allowed network, or the owner stopping it.
65
#[uniffi::export]
66
pub fn runner_stop() {
67
    flow::runner::stop();
68
}
69
70
/// The tiers this phone offers (the WASM tier), and why not the others.
71
#[uniffi::export]
72
pub fn runner_tiers(home: String) -> Vec<String> {
73
    let Ok(h) = flow::home::Home::locate(Some(Path::new(&home))) else { return vec![] };
74
    match flow::runner::tiers(&h) {
75
        Ok((have, _)) => have,
76
        Err(_) => vec![],
77
    }
78
}