Clean up interface a bit

Isaac Clayton created

Change summary

crates/runner/src/lib.rs     | 32 +++++++++++---------------------
crates/runner/src/main.rs    | 10 ++++++++++
crates/runner/src/runtime.rs | 17 ++++++-----------
3 files changed, 27 insertions(+), 32 deletions(-)

Detailed changes

crates/runner/src/lib.rs 🔗

@@ -1,12 +1,12 @@
-use mlua::{Error, FromLua, Lua, ToLua, UserData};
+use std::collections::{HashMap, HashSet};
+
+use mlua::{Error, FromLua, Function, Lua, ToLua, UserData, Value};
 
 pub mod runtime;
 pub use runtime::*;
 
 impl Runtime for Lua {
     type Module = String;
-    // type Error = Error;
-    type Interface = LuaInterface;
 
     fn init(module: Self::Module) -> Option<Self> {
         let lua = Lua::new();
@@ -14,24 +14,14 @@ impl Runtime for Lua {
         return Some(lua);
     }
 
-    fn interface(&self) -> Self::Interface {
-        todo!()
-    }
-
-    fn val<'lua, K: ToLua<'lua>, V: FromLua<'lua>>(&'lua self, key: K) -> Option<V> {
-        self.globals().get(key).ok()
-    }
-}
-
-pub struct LuaInterface {
-    funs: Vec<String>,
-    vals: Vec<String>,
-}
-
-impl Interface for LuaInterface {
-    type Handle = String;
+    fn interface(&self) -> Interface {
+        let mut globals = HashSet::new();
+        for pair in self.globals().pairs::<String, Value>() {
+            if let Ok((k, _)) = pair {
+                globals.insert(k);
+            }
+        }
 
-    fn handles(&self) -> &[Self::Handle] {
-        todo!()
+        globals
     }
 }

crates/runner/src/main.rs 🔗

@@ -4,4 +4,14 @@ use runner::*;
 
 pub fn main() {
     let lua: Lua = Runtime::init("x = 7".to_string()).unwrap();
+    println!("{:?}", lua.interface());
+}
+
+struct InterfaceX;
+
+impl InterfaceX {
+    pub fn get_x<T: Runtime>(runtime: T) -> usize {
+        // runtime.get("x")
+        todo!()
+    }
 }

crates/runner/src/runtime.rs 🔗

@@ -1,22 +1,17 @@
-use mlua::{FromLua, ToLua};
+use std::collections::HashSet;
 
-pub trait FromRuntime: Sized {
-    fn from_runtime() -> Option<Self>;
-}
+use mlua::{FromLua, Lua, ToLua, Value};
 
-pub trait Interface {
-    type Handle;
-    fn handles(&self) -> &[Self::Handle];
-}
+pub type Interface = HashSet<String>;
 
 pub trait Runtime
 where
     Self: Sized,
 {
     type Module;
-    type Interface: Interface;
 
     fn init(plugin: Self::Module) -> Option<Self>;
-    fn interface(&self) -> Self::Interface;
-    fn val<'lua, K: ToLua<'lua>, V: FromLua<'lua>>(&'lua self, key: K) -> Option<V>;
+    fn interface(&self) -> Interface;
+    // fn val<'a, T>(&'a self, name: String) -> Option<T>;
+    // fn call<'a, A: MyFromLua<'a>, R>(&'a mut self, name: String, arg: A) -> Option<R>;
 }