Skip to content

Commit a51423d

Browse files
authored
Merge pull request #13 from tamaroning/fix/reference
fix Url, Formula, and DocProperties
2 parents 4bc2ff9 + 0a52d4b commit a51423d

File tree

8 files changed

+138
-295
lines changed

8 files changed

+138
-295
lines changed

rust/src/wrapper/doc_properties.rs

+30-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::{Arc, Mutex};
2+
13
use rust_xlsxwriter as xlsx;
24
use wasm_bindgen::prelude::*;
35

@@ -20,16 +22,32 @@ use wasm_bindgen::prelude::*;
2022
#[derive(Clone)]
2123
#[wasm_bindgen]
2224
pub struct DocProperties {
23-
pub(crate) inner: xlsx::DocProperties,
25+
pub(crate) inner: Arc<Mutex<xlsx::DocProperties>>,
26+
}
27+
28+
macro_rules! impl_method {
29+
($self:ident.$method:ident($($arg:expr),*)) => {
30+
let mut lock = $self.inner.lock().unwrap();
31+
let mut inner = std::mem::take(&mut *lock);
32+
inner = inner.$method($($arg),*);
33+
let _ = std::mem::replace(&mut *lock, inner);
34+
return DocProperties {
35+
inner: Arc::clone(&$self.inner),
36+
}
37+
};
2438
}
2539

2640
#[wasm_bindgen]
2741
impl DocProperties {
42+
pub(crate) fn lock(&self) -> std::sync::MutexGuard<'_, xlsx::DocProperties> {
43+
self.inner.lock().unwrap()
44+
}
45+
2846
/// Create a new `DocProperties` class.
2947
#[wasm_bindgen(constructor)]
3048
pub fn new() -> DocProperties {
3149
DocProperties {
32-
inner: xlsx::DocProperties::new(),
50+
inner: Arc::new(Mutex::new(xlsx::DocProperties::new())),
3351
}
3452
}
3553

@@ -42,9 +60,7 @@ impl DocProperties {
4260
/// @returns {DocProperties} - The DocProperties object.
4361
#[wasm_bindgen(js_name = "setTitle", skip_jsdoc)]
4462
pub fn set_title(&self, title: &str) -> DocProperties {
45-
DocProperties {
46-
inner: self.clone().inner.set_title(title),
47-
}
63+
impl_method!(self.set_title(title));
4864
}
4965

5066
/// Set the Subject field of the document properties.
@@ -56,9 +72,7 @@ impl DocProperties {
5672
/// @returns {DocProperties} - The DocProperties object.
5773
#[wasm_bindgen(js_name = "setSubject", skip_jsdoc)]
5874
pub fn set_subject(&self, subject: &str) -> DocProperties {
59-
DocProperties {
60-
inner: self.clone().inner.set_subject(subject),
61-
}
75+
impl_method!(self.set_subject(subject));
6276
}
6377

6478
/// Set the Author field of the document properties.
@@ -70,9 +84,7 @@ impl DocProperties {
7084
/// @returns {DocProperties} - The DocProperties object.
7185
#[wasm_bindgen(js_name = "setAuthor", skip_jsdoc)]
7286
pub fn set_author(&self, author: &str) -> DocProperties {
73-
DocProperties {
74-
inner: self.clone().inner.set_author(author),
75-
}
87+
impl_method!(self.set_author(author));
7688
}
7789

7890
/// Set the Manager field of the document properties.
@@ -84,9 +96,7 @@ impl DocProperties {
8496
/// @returns {DocProperties} - The DocProperties object.
8597
#[wasm_bindgen(js_name = "setManager", skip_jsdoc)]
8698
pub fn set_manager(&self, manager: &str) -> DocProperties {
87-
DocProperties {
88-
inner: self.clone().inner.set_manager(manager),
89-
}
99+
impl_method!(self.set_manager(manager));
90100
}
91101

92102
/// Set the Company field of the document properties.
@@ -98,9 +108,7 @@ impl DocProperties {
98108
/// @returns {DocProperties} - The DocProperties object.
99109
#[wasm_bindgen(js_name = "setCompany", skip_jsdoc)]
100110
pub fn set_company(&self, company: &str) -> DocProperties {
101-
DocProperties {
102-
inner: self.clone().inner.set_company(company),
103-
}
111+
impl_method!(self.set_company(company));
104112
}
105113

106114
/// Set the Category field of the document properties.
@@ -112,9 +120,7 @@ impl DocProperties {
112120
/// @returns {DocProperties} - The DocProperties object.
113121
#[wasm_bindgen(js_name = "setCategory", skip_jsdoc)]
114122
pub fn set_category(&self, category: &str) -> DocProperties {
115-
DocProperties {
116-
inner: self.clone().inner.set_category(category),
117-
}
123+
impl_method!(self.set_category(category));
118124
}
119125

120126
/// Set the Keywords field of the document properties.
@@ -126,9 +132,7 @@ impl DocProperties {
126132
/// @returns {DocProperties} - The DocProperties object.
127133
#[wasm_bindgen(js_name = "setKeywords", skip_jsdoc)]
128134
pub fn set_keywords(&self, keywords: &str) -> DocProperties {
129-
DocProperties {
130-
inner: self.clone().inner.set_keywords(keywords),
131-
}
135+
impl_method!(self.set_keywords(keywords));
132136
}
133137

134138
/// Set the Comment field of the document properties.
@@ -141,9 +145,7 @@ impl DocProperties {
141145
/// @returns {DocProperties} - The DocProperties object.
142146
#[wasm_bindgen(js_name = "setComment", skip_jsdoc)]
143147
pub fn set_comment(&self, comment: &str) -> DocProperties {
144-
DocProperties {
145-
inner: self.clone().inner.set_comment(comment),
146-
}
148+
impl_method!(self.set_comment(comment));
147149
}
148150

149151
/// Set the Status field of the document properties.
@@ -155,9 +157,7 @@ impl DocProperties {
155157
/// @returns {DocProperties} - The DocProperties object.
156158
#[wasm_bindgen(js_name = "setStatus", skip_jsdoc)]
157159
pub fn set_status(&self, status: &str) -> DocProperties {
158-
DocProperties {
159-
inner: self.clone().inner.set_status(status),
160-
}
160+
impl_method!(self.set_status(status));
161161
}
162162

163163
/// Set the hyperlink base field of the document properties.
@@ -169,8 +169,6 @@ impl DocProperties {
169169
/// @returns {DocProperties} - The DocProperties object.
170170
#[wasm_bindgen(js_name = "setHyperlinkBase", skip_jsdoc)]
171171
pub fn set_hyperlink_base(&self, hyperlink_base: &str) -> DocProperties {
172-
DocProperties {
173-
inner: self.clone().inner.set_hyperlink_base(hyperlink_base),
174-
}
172+
impl_method!(self.set_hyperlink_base(hyperlink_base));
175173
}
176174
}

rust/src/wrapper/excel_data.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ impl xlsx::IntoExcelData for ExcelData {
168168
ExcelData::Number(n) => worksheet.write_number(row, col, n),
169169
ExcelData::Bool(b) => worksheet.write_boolean(row, col, b),
170170
ExcelData::DateTime(dt) => worksheet.write_datetime(row, col, dt),
171-
ExcelData::Formula(f) => worksheet.write_formula(row, col, &f.inner),
172-
ExcelData::Url(url) => worksheet.write_url(row, col, &url.inner),
173171
ExcelData::RichString(rich_string) => {
174172
let rich_string = rich_string.lock();
175173
let rich_string: Vec<_> =
176174
rich_string.iter().map(|(f, s)| (f, s.as_str())).collect();
177175
worksheet.write_rich_string(row, col, &rich_string)
178176
}
177+
ExcelData::Formula(f) => worksheet.write_formula(row, col, &*f.lock()),
178+
ExcelData::Url(url) => worksheet.write_url(row, col, &*url.lock()),
179179
}
180180
}
181181

@@ -193,15 +193,15 @@ impl xlsx::IntoExcelData for ExcelData {
193193
ExcelData::Bool(b) => worksheet.write_boolean_with_format(row, col, b, format),
194194
ExcelData::DateTime(dt) => worksheet.write_datetime_with_format(row, col, dt, format),
195195
ExcelData::Formula(f) => {
196-
worksheet.write_formula_with_format(row, col, &f.inner, format)
196+
worksheet.write_formula_with_format(row, col, &*f.lock(), format)
197197
}
198-
ExcelData::Url(url) => worksheet.write_url_with_format(row, col, &url.inner, format),
199198
ExcelData::RichString(rich_string) => {
200199
let rich_string = rich_string.lock();
201200
let rich_string: Vec<_> =
202201
rich_string.iter().map(|(f, s)| (f, s.as_str())).collect();
203202
worksheet.write_rich_string_with_format(row, col, &rich_string, format)
204203
}
204+
ExcelData::Url(url) => worksheet.write_url_with_format(row, col, &*url.lock(), format),
205205
}
206206
}
207207
}

0 commit comments

Comments
 (0)