unexist.dev

Changeset 187:817d1da569d6

Added #ObjectBuilder
author unexist <christoph@unexist.dev>
date Tue, 21 Jul 2020 14:35:26 +0200
parents 4dc8331e27a7
children 62b2cb072edf
files rubtle-lib/src/lib.rs rubtle-lib/src/object.rs rubtle-lib/src/object_builder.rs rubtle-lib/src/tests/object.rs
diffstat 4 files changed, 49 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/rubtle-lib/src/lib.rs	Fri Jul 17 15:36:26 2020 +0200
+++ b/rubtle-lib/src/lib.rs	Tue Jul 21 14:35:26 2020 +0200
@@ -16,7 +16,7 @@
 
 mod rubtle;
 mod value;
-mod object;
+mod object_builder;
 mod invocation;
 mod error;
 mod types;
@@ -26,7 +26,7 @@
 
 pub use rubtle::Rubtle;
 pub use value::Value;
-pub use object::Object;
+pub use object_builder::ObjectBuilder;
 pub use invocation::Invocation;
 pub use error::Error;
 pub use types::{Callback, Result};
--- a/rubtle-lib/src/object.rs	Fri Jul 17 15:36:26 2020 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-///
-/// @package Rubtle-Lib
-///
-/// @file Object functions
-/// @copyright 2020 Christoph Kappel <unexist@subforge.org>
-/// @version $Id$
-///
-/// This program can be distributed under the terms of the GNU GPLv2.
-/// See the file LICENSE for details.
-//
-use std::fmt;
-
-#[derive(Debug)]
-pub struct Object {}
-
-impl Object {
-    pub fn new() -> Object {
-        Object {}
-    }
-}
-
-impl fmt::Display for Object {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "Object")
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rubtle-lib/src/object_builder.rs	Tue Jul 21 14:35:26 2020 +0200
@@ -0,0 +1,47 @@
+use std::collections::HashMap;
+///
+/// @package Rubtle-Lib
+///
+/// @file ObjectBuilder functions
+/// @copyright 2020 Christoph Kappel <unexist@subforge.org>
+/// @version $Id$
+///
+/// This program can be distributed under the terms of the GNU GPLv2.
+/// See the file LICENSE for details.
+//
+
+type ObjectBuilderCall<T> = Box<dyn FnMut(T)>;
+
+pub struct ObjectBuilder<T> {
+    pub user_data: Option<T>,
+    pub constructor: Option<ObjectBuilderCall<T>>,
+    pub methods: HashMap<&'static str, ObjectBuilderCall<T>>,
+}
+
+impl<T> ObjectBuilder<T> {
+    pub fn new() -> ObjectBuilder<T> {
+        ObjectBuilder {
+            user_data: None,
+            constructor: None,
+            methods: HashMap::new(),
+        }
+    }
+
+    pub fn set_constructor<F>(&mut self, func: F)
+    where
+        F: 'static + FnMut(T),
+    {
+        self.constructor = Some(Box::new(func) as ObjectBuilderCall<T>);
+    }
+
+    pub fn set_user_data(&mut self, user_data: T) {
+        self.user_data = Some(user_data);
+    }
+
+    pub fn add_method<F>(&mut self, name: &'static str, func: F)
+    where
+        F: 'static + FnMut(T),
+    {
+        self.methods.insert(name, Box::new(func) as ObjectBuilderCall<T>);
+    }
+}
--- a/rubtle-lib/src/tests/object.rs	Fri Jul 17 15:36:26 2020 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,16 +0,0 @@
-///
-/// @package Rubtle-Lib
-///
-/// @file Value tests
-/// @copyright 2020 Christoph Kappel <unexist@subforge.org>
-/// @version $Id$
-///
-/// This program can be distributed under the terms of the GNU GPLv2.
-/// See the file LICENSE for details.
-///
-use crate::Object;
-
-#[test]
-fn create_object() {
-    let obj: Object = Object::new();
-}
\ No newline at end of file