From 630e0e837e5df840d652bf7d64d5d930ba8fe547 Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Mon, 16 Apr 2018 12:18:55 +0100
Subject: [PATCH] Tmp route for script testing

---
 src/lib.rs                  |  3 ++-
 src/main.rs                 |  6 ++++-
 src/routing/mod.rs          |  3 ++-
 src/routing/scripting.rs    |  8 +++++++
 src/scripting/mod.rs        |  3 +++
 src/scripting/run_script.rs | 46 +++++++++++++++++++++++++++++++++++++
 6 files changed, 66 insertions(+), 3 deletions(-)
 create mode 100644 src/routing/scripting.rs
 create mode 100644 src/scripting/mod.rs
 create mode 100644 src/scripting/run_script.rs

diff --git a/src/lib.rs b/src/lib.rs
index 06de602..26cb650 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,4 +12,5 @@ extern crate hyper;
 extern crate rand;
 
 pub mod cli;
-pub mod routing;
\ No newline at end of file
+pub mod routing;
+pub mod scripting;
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index a7a7113..fb21a29 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -149,7 +149,11 @@ fn main() {
         printq!("[SETUP] Accepting uploads at /upload");
         server = server.mount("/upload", routes![swerve::routing::mock_upload::to_file]);
     }
-    server = server.mount("/", routes![serve_root, serve_files]);;
+    server = server.mount("/", routes![
+		serve_root,
+		serve_files,
+		routing::scripting::route_script
+	]);
 
     if !args.flag_quiet {
         server = server.attach(rocket::fairing::AdHoc::on_launch(move |rckt| {
diff --git a/src/routing/mod.rs b/src/routing/mod.rs
index 25ef89d..b7054b1 100644
--- a/src/routing/mod.rs
+++ b/src/routing/mod.rs
@@ -1,2 +1,3 @@
 pub mod mock_upload;
-pub mod request;
\ No newline at end of file
+pub mod request;
+pub mod scripting;
\ No newline at end of file
diff --git a/src/routing/scripting.rs b/src/routing/scripting.rs
new file mode 100644
index 0000000..165c007
--- /dev/null
+++ b/src/routing/scripting.rs
@@ -0,0 +1,8 @@
+use scripting::run_script;
+use std::path::PathBuf;
+
+#[get("/__testing__/run-script")]
+pub fn route_script() -> String {
+	let path = PathBuf::from("example/.swerve/get_user_by_id.rhai");
+	run_script(path).unwrap_or(String::from("No script"))
+}
\ No newline at end of file
diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs
new file mode 100644
index 0000000..e7d9876
--- /dev/null
+++ b/src/scripting/mod.rs
@@ -0,0 +1,3 @@
+mod run_script;
+
+pub use self::run_script::run_script;
\ No newline at end of file
diff --git a/src/scripting/run_script.rs b/src/scripting/run_script.rs
new file mode 100644
index 0000000..96d9f57
--- /dev/null
+++ b/src/scripting/run_script.rs
@@ -0,0 +1,46 @@
+//use dyon::{Runtime,Module,load_str,Variable,Dfn,Type};
+//use dyon::ast::convert;
+use std::convert::AsRef;
+use std::path::Path;
+use std::fs::File;
+use std::sync::Arc;
+use std::io::Read;
+use std::collections::HashMap;
+
+const SCRIPT_FOOTER: &'static str = "
+fn main() {
+    println(\"Don't directly execute the module, nimrod\")
+}";
+
+
+pub fn run_script<P: AsRef<Path>>(path: P) -> Option<String> {
+//    let mut resolution: Option<Variable> = None;
+//    dyon_fn!(fn resolve(val: Variable) {
+//           resolution = Some(val); // if let Ok(val) = runtime.pop() { Some(val) } else { None };
+//    });
+//
+//    println!("{:?}", resolution);
+
+    let mut file = File::open(&path).unwrap();
+    let mut buf = String::new();
+
+    file.read_to_string(&mut buf);
+    buf.push_str(SCRIPT_FOOTER);
+//    let mut script_module = Module::new();
+//
+//    {
+//        load_str(path.as_ref().to_str().unwrap(), Arc::new(buf), &mut script_module);
+//    }
+    Some(buf)
+//    script_module.add(Arc::new("resolve".into()), resolve, Dfn {
+//        lts: vec![],
+//        tys: vec![Type::Object],
+//        ret: Type::Void,
+//    });
+//
+//    let mut hashmap: HashMap<Arc<String>, Variable> = HashMap::new();
+
+//    runtime.call_str("handle", &[Variable::f64(123f64)], &Arc::new(script_module));
+
+//    None
+}
\ No newline at end of file
-- 
GitLab