summaryrefslogtreecommitdiff
path: root/src/sheet
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-07-25 01:32:50 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-07-25 01:32:50 +0200
commitc5173c96fa13159ad12982dfefecf1cb78fded05 (patch)
tree01aaef93a61a07671ff4edb27a6747c7a3808967 /src/sheet
parent58b305b9f6d13007d2ea62775054c95177f81092 (diff)
use call return schema on lua evaluation
Diffstat (limited to 'src/sheet')
-rw-r--r--src/sheet/cell.rs9
-rw-r--r--src/sheet/mod.rs71
2 files changed, 69 insertions, 11 deletions
diff --git a/src/sheet/cell.rs b/src/sheet/cell.rs
index 413e299..53a4b04 100644
--- a/src/sheet/cell.rs
+++ b/src/sheet/cell.rs
@@ -110,14 +110,6 @@ impl CellRef {
.clone();
}
- pub fn set_value(&mut self, value: Cell) {
- Register::get(self.sheet_id)
- .unwrap()
- .write()
- .unwrap()
- .set_cell(self.row, self.column, value);
- }
-
pub fn left(&self) -> Option<Self> {
if self.column > 0 {
Self::new(self.sheet_id, self.row, self.column - 1)
@@ -185,7 +177,6 @@ where
fields.add_field_method_get("row", |_, this| Ok(this.row));
fields.add_field_method_get("column", |_, this| Ok(this.column));
fields.add_field_method_get("value", |_, this| Ok(this.value()));
- fields.add_field_method_set("value", |_, this, value| Ok(this.set_value(value)));
fields.add_field_method_get("left", |lua, this| this.left().into_lua(lua));
fields.add_field_method_get("right", |lua, this| this.right().into_lua(lua));
fields.add_field_method_get("up", |lua, this| this.up().into_lua(lua));
diff --git a/src/sheet/mod.rs b/src/sheet/mod.rs
index 015e3d8..22b73fe 100644
--- a/src/sheet/mod.rs
+++ b/src/sheet/mod.rs
@@ -1,14 +1,17 @@
+use std::{sync::{Arc, Mutex, RwLock}, thread::JoinHandle};
+
use cell::{Cell, CellRef};
-use mlua::{IntoLua, UserData, Value};
+use mlua::{Function, IntoLua, UserData, Value};
use register::{Register, SheetId};
pub mod cell;
pub mod register;
-#[derive(Debug, Default, Clone)]
+#[derive(Debug, Default)]
pub struct Sheet {
id: register::SheetId,
rows: Vec<Vec<Cell>>,
+ progress: Mutex<u8>,
}
impl Sheet {
@@ -16,6 +19,7 @@ impl Sheet {
Self {
id,
rows: vec![vec![Cell::new_empty(); width]; height],
+ progress: Mutex::new(0),
}
}
@@ -63,6 +67,24 @@ impl Sheet {
pub fn id(&self) -> register::SheetId {
self.id
}
+
+ pub fn apply(&mut self, other: Sheet) {
+ self.rows = other.rows;
+ }
+
+ pub fn progress(&self) -> u8 {
+ *self.progress.lock().unwrap()
+ }
+}
+
+impl Clone for Sheet {
+ fn clone(&self) -> Self {
+ Sheet {
+ id: self.id,
+ rows: self.rows.clone(),
+ progress: Mutex::new(0),
+ }
+ }
}
struct SheetLuaRef {
@@ -96,3 +118,48 @@ impl UserData for SheetLuaRef {
})
}
}
+
+
+pub trait EvalFunction {
+ fn eval_function(&self, func: String, range: Vec<(usize, usize)>) -> JoinHandle<Result<Sheet, String>>;
+}
+
+impl EvalFunction for Arc<RwLock<Sheet>> {
+ fn eval_function(
+ &self,
+ func_text: String,
+ range: Vec<(usize, usize)>,
+ ) -> JoinHandle<Result<Sheet, String>> {
+ let this = Arc::clone(self);
+ std::thread::spawn(move || {
+ let read_sheet = this.read().unwrap();
+ let mut sheet = read_sheet.clone();
+ *read_sheet.progress.lock().unwrap() = 0;
+ let lua = crate::lua::new_instance().unwrap();
+ let result = lua
+ .load(func_text.clone())
+ .set_name("Temp Script")
+ .eval::<Function>();
+
+ match result {
+ Ok(func) => {
+ for (i, (row, column)) in range.iter().enumerate() {
+
+ *read_sheet.progress.lock().unwrap() = (i * 100 / range.len()) as u8;
+ let cellref = read_sheet.get_ref(*row, *column);
+
+ match func.call::<_, Cell>(cellref) {
+ Ok(value) => {
+ sheet.set_cell(*row, *column, value)
+ },
+ Err(error) => return Err(error.to_string()),
+ }
+ }
+ Ok(sheet)
+ }
+ Err(error) => Err(error.to_string()),
+ }
+ })
+ }
+
+}