aboutsummaryrefslogtreecommitdiff
path: root/bundle/src-tauri/src/main.rs
blob: 07f25b3831ea9de569600323af12536bda88690d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod compiler;
mod runner;
use compiler::Compiler;
use runner::{OutputEvent, Runner};
use tauri::{ipc::Channel, Manager, State};

#[tauri::command]
fn read_file(path: String) -> Result<String, String> {
    std::fs::read_to_string(path).map_err(|error| error.to_string())
}

#[tauri::command]
fn write_file(path: String, text: String) -> Result<(), String> {
    std::fs::write(path, text).map_err(|error| error.to_string())
}

#[tauri::command]
async fn run_file(
    path: String,
    input: String,
    output: Channel<OutputEvent>,
    runner: State<'_, Runner>,
    compiler: State<'_, Compiler>,
) -> Result<(), String> {
    let executable = compiler.inner().0.clone()?;
    let result = runner
        .run(executable, path.into(), input, |stream, bytes| {
            let _ = output.send(OutputEvent::Data {
                stream: stream.into(),
                bytes,
            });
        })
        .await?;
    output
        .send(OutputEvent::Finished {
            code: result.code,
            stopped: result.stopped,
        })
        .map_err(|error| error.to_string())
}

#[tauri::command]
async fn stop_run(runner: State<'_, Runner>) -> Result<(), String> {
    runner.stop().await;
    Ok(())
}

fn main() {
    let compiler = Compiler::load();
    tauri::Builder::default()
        .plugin(tauri_plugin_dialog::init())
        .manage(Runner::default())
        .manage(compiler)
        .invoke_handler(tauri::generate_handler![
            read_file, write_file, run_file, stop_run
        ])
        .build(tauri::generate_context!())
        .expect("Could not start CPSC Bundle")
        .run(|app, event| {
            if matches!(event, tauri::RunEvent::ExitRequested { .. }) {
                tauri::async_runtime::block_on(app.state::<Runner>().stop());
            }
        });
}