summaryrefslogtreecommitdiff
path: root/src/sheet/mod.rs
blob: 22b73fea32fbd3d598b4f8ec4697911162304934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::{sync::{Arc, Mutex, RwLock}, thread::JoinHandle};

use cell::{Cell, CellRef};
use mlua::{Function, IntoLua, UserData, Value};
use register::{Register, SheetId};

pub mod cell;
pub mod register;

#[derive(Debug, Default)]
pub struct Sheet {
    id: register::SheetId,
    rows: Vec<Vec<Cell>>,
    progress: Mutex<u8>,
}

impl Sheet {
    pub(self) fn new(width: usize, height: usize, id: register::SheetId) -> Self {
        Self {
            id,
            rows: vec![vec![Cell::new_empty(); width]; height],
            progress: Mutex::new(0),
        }
    }

    pub fn set_cell(&mut self, row: usize, column: usize, cell: Cell) {
        if row < self.height() && column < self.width() {
            self.rows[row][column] = cell
        }
    }

    pub fn get_cell<'a>(&'a self, row: usize, column: usize) -> Option<&'a Cell> {
        if let Some(r) = self.rows.get(row) {
            if let Some(_) = r.get(column) {
                return Some(&self.rows[row][column]);
            }
        }

        None
    }

    pub fn get_ref(&self, row: usize, column: usize) -> Option<CellRef> {
        CellRef::new(self.id, row, column)
    }

    pub fn height(&self) -> usize {
        self.rows.len()
    }

    pub fn set_height(&mut self, mut height: usize) {
        height = height.max(1);
        self.rows
            .resize(height, vec![Cell::new_empty(); self.width()])
    }

    pub fn width(&self) -> usize {
        self.rows.get(0).map(|r| r.len()).unwrap_or(1)
    }

    pub fn set_width(&mut self, mut width: usize) {
        width = width.max(1);
        for row in self.rows.iter_mut() {
            row.resize(width, Cell::new_empty());
        }
    }

    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 {
    id: SheetId,
}

impl SheetLuaRef {
    pub fn new(id: SheetId) -> Self {
        Self { id }
    }
}

impl UserData for SheetLuaRef {
    fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
        fields.add_field_method_get("width", |_, luaref| {
            Ok(Register::get(luaref.id).unwrap().read().unwrap().width())
        });

        fields.add_field_method_get("height", |_, luaref| {
            Ok(Register::get(luaref.id).unwrap().read().unwrap().height())
        });
    }

    fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_method_mut("cell", |lua, luaref, (row, column): (usize, usize)| {
            if let Some(_) = Register::get(luaref.id) {
                Ok(CellRef::new(luaref.id, row, column).into_lua(lua).unwrap())
            } else {
                Ok(Value::Nil)
            }
        })
    }
}


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()),
            }
        })
    }

}