unexist.dev

Changeset 232:cdfe2417dd14

Slightly refactored
author unexist
date Mon, 30 Nov 2020 13:34:30 +0100
parents bf5214c3c77b
children 5b05066b8c21
files rubtle/src/main.rs
diffstat 1 files changed, 12 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/rubtle/src/main.rs	Mon Nov 30 07:30:04 2020 +0100
+++ b/rubtle/src/main.rs	Mon Nov 30 13:34:30 2020 +0100
@@ -39,7 +39,12 @@
     Ok(Value::from(true))
 }
 
-fn initialize(rubtle: &Rubtle) {
+fn init_global(rubtle: &Rubtle) {
+    rubtle.set_global_function("print", js_printer);
+    rubtle.set_global_function("assert", js_assert_eq);
+}
+
+fn init_rubtle(rubtle: &Rubtle) {
     #[derive(Default)]
     struct UserData {
         value: i32,
@@ -77,9 +82,6 @@
         .build();
 
     rubtle.set_global_object("Rubtle", &mut object);
-
-    rubtle.set_global_function("print", js_printer);
-    rubtle.set_global_function("assert", js_assert_eq);
 }
 
 fn main() {
@@ -87,14 +89,15 @@
 
     if 1 < args.len() {
         let contents = fs::read_to_string(&args[1]);
-
-        println!("{:?}", contents);
-
         let rubtle = Rubtle::new();
 
-        initialize(&rubtle);
+        init_global(&rubtle);
+        init_rubtle(&rubtle);
 
-        rubtle.eval(&contents.unwrap());
+        match contents {
+            Ok(val) => rubtle.eval(&val),
+            Err(_) => eprintln!("File read failed"),
+        }
     } else {
         println!("Usage: {}: <JSFile>", args[0]);
     }