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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
use std::fmt::Display;
use mlua::{FromLua, IntoLua, UserData, Value};
use super::register::{Register, SheetId};
#[derive(Debug, Clone)]
pub enum Cell {
String(String),
Number(f64),
}
impl Cell {
pub fn new_empty() -> Self {
Cell::String("".to_string())
}
}
impl<'lua> IntoLua<'lua> for Cell {
fn into_lua(self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
match self {
Cell::String(s) => s.into_lua(lua),
Cell::Number(n) => n.into_lua(lua),
}
}
}
impl<'lua> FromLua<'lua> for Cell {
fn from_lua(value: mlua::Value<'lua>, _: &'lua mlua::Lua) -> mlua::Result<Self> {
match value {
Value::Nil => Ok(Cell::new_empty()),
Value::Boolean(b) => Ok(Cell::String(b.to_string())),
Value::Integer(n) => Ok(Cell::Number(n as f64)),
Value::Number(n) => Ok(Cell::Number(n)),
Value::String(s) => Ok(Cell::String(s.to_str()?.to_string())),
_ => Err(mlua::Error::runtime(
"cell content must be number or string",
)),
}
}
}
impl Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Cell::String(s) => s.fmt(f),
Cell::Number(n) => n.fmt(f),
}
}
}
impl From<String> for Cell {
fn from(value: String) -> Self {
Cell::String(value.clone())
}
}
impl From<&str> for Cell {
fn from(value: &str) -> Self {
Cell::String(value.to_string())
}
}
macro_rules! cell_from_integer {
($($number:ty),+ $(,)?) => {
$(
impl From<$number> for Cell {
fn from(value: $number) -> Self {
Cell::Number(value as f64)
}
}
)*
};
}
#[rustfmt::skip]
cell_from_integer!(
i8, i16, i32, i64, i128, isize,
u8, u16, u32, u64, u128, usize,
f32, f64
);
#[derive(Debug, Clone)]
pub struct CellRef {
row: usize,
column: usize,
sheet_id: SheetId,
value: Cell,
}
impl CellRef {
/// # Safety
///
/// This function shall only be called by the Sheet instance itsself or
/// if it has been assured that the `SheetId` is valid.
pub unsafe fn new(sheet_id: SheetId, row: usize, column: usize, cell: Cell) -> Self {
Self {
sheet_id,
row,
column,
value: cell
}
}
pub fn fetch_new(sheet_id: SheetId, row: usize, column: usize) -> Option<Self> {
Register::get(sheet_id)
.map(|sheet| {
sheet.read().unwrap().get_cell(row, column).map(|cell| Self {
sheet_id,
row,
column,
value: cell.clone(),
})
})
.unwrap_or(None)
}
pub fn value(&self) -> Cell {
self.value.clone()
}
pub fn set_value(&mut self, cell: Cell) {
self.value = cell.clone();
Register::get(self.sheet_id)
.unwrap()
.write()
.unwrap()
.set_cell(self.row, self.column, cell);
}
pub fn left(&self) -> Option<Self> {
if self.column > 0 {
Self::fetch_new(self.sheet_id, self.row, self.column - 1)
} else {
None
}
}
pub fn right(&self) -> Option<Self> {
Self::fetch_new(self.sheet_id, self.row, self.column + 1)
}
pub fn up(&self) -> Option<Self> {
if self.row > 0 {
Self::fetch_new(self.sheet_id, self.row - 1, self.column)
} else {
None
}
}
pub fn down(&self) -> Option<Self> {
Self::fetch_new(self.sheet_id, self.row + 1, self.column)
}
pub fn begin(&self) -> Option<Self> {
Self::fetch_new(self.sheet_id, self.row, 0)
}
pub fn end(&self) -> Option<Self> {
Self::fetch_new(
self.sheet_id,
self.row,
Register::get(self.sheet_id)
.unwrap()
.read()
.unwrap()
.width()
- 1,
)
}
pub fn top(&self) -> Option<Self> {
Self::fetch_new(self.sheet_id, 0, self.column)
}
pub fn bottom(&self) -> Option<Self> {
Self::fetch_new(
self.sheet_id,
Register::get(self.sheet_id)
.unwrap()
.read()
.unwrap()
.height()
- 1,
self.column,
)
}
}
impl UserData for CellRef
where
Self: Sized,
{
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
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_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));
fields.add_field_method_get("down", |lua, this| this.down().into_lua(lua));
fields.add_field_method_get("begin", |lua, this| this.begin().into_lua(lua));
fields.add_field_method_get("end", |lua, this| this.end().into_lua(lua));
fields.add_field_method_get("top", |lua, this| this.top().into_lua(lua));
fields.add_field_method_get("bottom", |lua, this| this.bottom().into_lua(lua));
}
}
|