Skip to main content

nifty_cli/
run.rs

1//! `nf run`: check the app, then run it with native windows.
2
3use std::path::Path;
4use std::process::{self, ExitCode};
5use std::rc::Rc;
6
7use nifty_interp::RuntimeError;
8
9use crate::frontend::Sources;
10use crate::{CommandResult, EXIT_HAS_ERRORS, report};
11
12pub(crate) fn run(target: &Path) -> CommandResult {
13    let sources = Rc::new(Sources::read(target)?);
14    let program = match sources.compile() {
15        Ok(program) => program,
16        Err(diagnostics) => {
17            report::print(&sources, &diagnostics)?;
18            return Ok(ExitCode::from(EXIT_HAS_ERRORS));
19        }
20    };
21
22    // An error inside an event leaves the app half-way through a change, so it stops there.
23    let on_error = Rc::new({
24        let sources = Rc::clone(&sources);
25        move |error: RuntimeError| {
26            report::runtime_error(&sources, &error);
27            process::exit(i32::from(EXIT_HAS_ERRORS));
28        }
29    });
30    match nifty_interp::run(program, nifty_platform::platform_backend(), on_error) {
31        Ok(()) => Ok(ExitCode::SUCCESS),
32        Err(error) => {
33            report::runtime_error(&sources, &error);
34            Ok(ExitCode::from(EXIT_HAS_ERRORS))
35        }
36    }
37}