Skip to content

Commit eebf3b6

Browse files
committed
feat: implement format clone by clone method
1 parent 88d9ce6 commit eebf3b6

File tree

3 files changed

+12
-31
lines changed

3 files changed

+12
-31
lines changed

rust/src/wrapper/format.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::sync::{Arc, Mutex, MutexGuard};
33
use rust_xlsxwriter as xlsx;
44
use wasm_bindgen::prelude::*;
55

6-
use crate::{error::XlsxError, wrapper::utils, DeepClone};
6+
use crate::{DeepClone};
77

8-
use super::{color::Color, WasmResult};
8+
use super::{color::Color};
99

1010
/// The `Format` struct is used to define cell formatting for data in a
1111
/// worksheet.
@@ -178,32 +178,15 @@ pub struct Format {
178178
pub(crate) inner: Arc<Mutex<xlsx::Format>>,
179179
}
180180

181-
#[wasm_bindgen]
182-
extern "C" {
183-
#[wasm_bindgen(typescript_type = "Format")]
184-
pub type JsFormat;
185-
}
186-
187181
#[wasm_bindgen]
188182
impl Format {
189183
/// Create a new Format object.
190184
///
191185
/// Create a new Format object to use with worksheet formatting.
192-
/// If a Format obejct is provided as an argument, it will be cloned.
193186
#[wasm_bindgen(constructor)]
194-
pub fn new(format: Option<JsFormat>) -> WasmResult<Format> {
195-
if let Some(format) = format {
196-
// HACK: Since wasm-bindgen does not supports Option<&RustStruct>, we cast JsValue as Format.
197-
let format = utils::format_of_jsval(&format).ok_or(XlsxError::Type(
198-
"Expected a value of type Format".to_string(),
199-
))?;
200-
// Clone the inner object
201-
let format = format.deep_clone();
202-
Ok(format)
203-
} else {
204-
Ok(Format {
205-
inner: Arc::new(Mutex::new(xlsx::Format::new())),
206-
})
187+
pub fn new() -> Format {
188+
Format {
189+
inner: Arc::new(Mutex::new(xlsx::Format::new())),
207190
}
208191
}
209192

@@ -815,6 +798,11 @@ impl Format {
815798
inner: Arc::clone(&self.inner),
816799
}
817800
}
801+
802+
#[wasm_bindgen(js_name = "clone")]
803+
pub fn clone_format(&self) -> Format {
804+
self.deep_clone()
805+
}
818806
}
819807

820808
impl DeepClone for Format {

rust/src/wrapper/utils.rs

-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use wasm_bindgen::convert::RefFromWasmAbi;
77
use wasm_bindgen::prelude::*;
88
use wasm_bindgen::JsCast;
99

10-
use super::format::Format;
1110
use super::formula::Formula;
1211
use super::url::Url;
1312

@@ -85,12 +84,6 @@ pub fn formula_of_jsval(js: &JsValue) -> Option<Formula> {
8584
.map(|f| f.clone())
8685
}
8786

88-
pub fn format_of_jsval(js: &JsValue) -> Option<Format> {
89-
generic_of_jsval::<Format>(js, "Format")
90-
.ok()
91-
.map(|f| f.clone())
92-
}
93-
9487
pub fn url_of_jsval(js: &JsValue) -> Option<Url> {
9588
generic_of_jsval::<Url>(js, "Url").ok().map(|f| f.clone())
9689
}

test/format.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ describe("xlsx-wasm test", () => {
213213
// Act
214214
const worksheet = workbook.addWorksheet();
215215
const baseFormat = new Format().setBold();
216-
const format1 = new Format(baseFormat).setItalic();
217-
const format2 = new Format(baseFormat).setFontColor(Color.red());
216+
const format1 = baseFormat.clone().setItalic();
217+
const format2 = baseFormat.clone().setFontColor(Color.red());
218218

219219
worksheet.writeStringWithFormat(0, 0, "bold", baseFormat);
220220
worksheet.writeStringWithFormat(0, 1, "bold italic", format1);

0 commit comments

Comments
 (0)