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
#![deny(warnings, intra_doc_link_resolution_failure)]
#![deny(clippy::all, clippy::pedantic)]

use mruby::convert::FromMrb;
use mruby::eval::{EvalContext, MrbEval};
use mruby::sys;
use mruby::value::Value;
use std::env;
use std::fs::File;
use std::io::{self, Read};
use std::process;

fn main() {
    let interp = match mruby::interpreter() {
        Ok(interp) => interp,
        Err(err) => {
            eprintln!("{}", err);
            process::exit(1);
        }
    };
    let mrb = interp.borrow().mrb;

    // program is either supplied as a file via command line argument or piped
    // in via stdin.
    let mut program = vec![];
    if let Some(file) = env::args().nth(1) {
        match File::open(file) {
            Ok(mut file) => {
                if let Err(err) = file.read_to_end(&mut program) {
                    eprintln!("Unable to read program: {}", err);
                    process::exit(1);
                }
            }
            Err(err) => {
                eprintln!("{}", err);
                process::exit(1);
            }
        }
    } else if let Err(err) = io::stdin().read_to_end(&mut program) {
        eprintln!("Unable to read program: {}", err);
        process::exit(1);
    }

    let data = Value::from_mrb(
        &interp,
        include_bytes!("../../ruby/fixtures/learnxinyminutes.txt").as_ref(),
    );
    data.protect();
    unsafe { sys::mrb_gv_set(mrb, interp.borrow_mut().sym_intern("$data"), data.inner()) }
    let ctx = EvalContext::new("(main)");
    if let Err(err) = interp.eval_with_context(program, ctx) {
        eprintln!("{}", err);
        process::exit(1);
    }
}