Skip to content

Commit 4348101

Browse files
make CommentOptions and RowColOptions to rust struct
1 parent 5bb9743 commit 4348101

File tree

4 files changed

+139
-32
lines changed

4 files changed

+139
-32
lines changed

libxlsxwriter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "xlsxwriter"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
authors = ["OKAMURA, Yasunobu <[email protected]>"]
55
edition = "2018"
66
readme = "../README.md"

libxlsxwriter/src/format.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::Workbook;
22
use std::ffi::CString;
33

44
#[allow(clippy::unreadable_literal)]
5-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
66
pub enum FormatColor {
77
Black,
88
Blue,
@@ -48,7 +48,7 @@ impl FormatColor {
4848
}
4949
}
5050

51-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
51+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
5252
pub enum FormatUnderline {
5353
Single,
5454
Double,
@@ -76,7 +76,7 @@ impl FormatUnderline {
7676
}
7777
}
7878

79-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
79+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
8080
pub enum FormatScript {
8181
SuperScript,
8282
SubScript,
@@ -92,7 +92,7 @@ impl FormatScript {
9292
}
9393
}
9494

95-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
95+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
9696
pub enum FormatAlignment {
9797
None,
9898
Left,
@@ -144,7 +144,7 @@ impl FormatAlignment {
144144
}
145145
}
146146

147-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
147+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
148148
pub enum FormatPatterns {
149149
None,
150150
Solid,
@@ -222,7 +222,7 @@ impl FormatPatterns {
222222
}
223223
}
224224

225-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
225+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
226226
pub enum FormatBorder {
227227
None,
228228
Thin,

libxlsxwriter/src/worksheet/filter.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,7 @@ mod test {
235235
value_string: None,
236236
};
237237
worksheet1.filter_column(0, &worksheet1_criteria)?;
238-
let mut hidden_row = RowColOptions {
239-
hidden: 1,
240-
level: 0,
241-
collapsed: 0,
242-
};
238+
let mut hidden_row = RowColOptions::new(true, 0, false);
243239
for i in 1..=10 {
244240
worksheet1.set_row_opt(i, 13.2, None, &mut hidden_row)?;
245241
}

libxlsxwriter/src/worksheet/mod.rs

Lines changed: 131 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ pub use filter::*;
1010
pub use table::*;
1111
pub use validation::*;
1212

13+
/// Integer data type to represent a column value. Equivalent to `u16`.
14+
///
15+
/// The maximum column in Excel is 16,384.
16+
pub type WorksheetCol = libxlsxwriter_sys::lxw_col_t;
17+
18+
/// Integer data type to represent a row value. Equivalent to `u32`.
19+
///
20+
/// The maximum row in Excel is 1,048,576.
21+
pub type WorksheetRow = libxlsxwriter_sys::lxw_row_t;
22+
1323
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
1424
pub struct DateTime {
1525
pub year: i16,
@@ -232,18 +242,114 @@ impl From<&Protection> for libxlsxwriter_sys::lxw_protection {
232242
}
233243
}
234244

235-
/// Integer data type to represent a column value. Equivalent to `u16`.
236-
///
237-
/// The maximum column in Excel is 16,384.
238-
pub type WorksheetCol = libxlsxwriter_sys::lxw_col_t;
245+
/// Options struct for the `set_column()` and `set_row()` functions.
246+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
247+
pub struct RowColOptions {
248+
hidden: bool,
249+
level: u8,
250+
collapsed: bool,
251+
}
239252

240-
/// Integer data type to represent a row value. Equivalent to `u32`.
241-
///
242-
/// The maximum row in Excel is 1,048,576.
243-
pub type WorksheetRow = libxlsxwriter_sys::lxw_row_t;
253+
impl RowColOptions {
254+
pub fn new(hidden: bool, level: u8, collapsed: bool) -> Self {
255+
RowColOptions {
256+
hidden,
257+
level,
258+
collapsed,
259+
}
260+
}
244261

245-
pub type CommentOptions = libxlsxwriter_sys::lxw_comment_options;
246-
pub type RowColOptions = libxlsxwriter_sys::lxw_row_col_options;
262+
pub(crate) fn into_internal(&self) -> libxlsxwriter_sys::lxw_row_col_options {
263+
libxlsxwriter_sys::lxw_row_col_options {
264+
hidden: convert_bool(self.hidden),
265+
level: self.level,
266+
collapsed: convert_bool(self.collapsed),
267+
}
268+
}
269+
}
270+
271+
impl Default for RowColOptions {
272+
fn default() -> Self {
273+
RowColOptions {
274+
hidden: false,
275+
level: 0,
276+
collapsed: false,
277+
}
278+
}
279+
}
280+
281+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
282+
pub enum CommentDisplayType {
283+
Default,
284+
Hidden,
285+
Visible,
286+
}
287+
288+
impl Default for CommentDisplayType {
289+
fn default() -> Self {
290+
CommentDisplayType::Default
291+
}
292+
}
293+
294+
impl CommentDisplayType {
295+
pub(crate) fn into_internal(self) -> libxlsxwriter_sys::lxw_comment_display_types {
296+
match self {
297+
CommentDisplayType::Default => {
298+
libxlsxwriter_sys::lxw_comment_display_types_LXW_COMMENT_DISPLAY_DEFAULT
299+
}
300+
CommentDisplayType::Hidden => {
301+
libxlsxwriter_sys::lxw_comment_display_types_LXW_COMMENT_DISPLAY_HIDDEN
302+
}
303+
CommentDisplayType::Visible => {
304+
libxlsxwriter_sys::lxw_comment_display_types_LXW_COMMENT_DISPLAY_VISIBLE
305+
}
306+
}
307+
}
308+
}
309+
310+
/// Options for modifying comments inserted via `write_comment_opt()`
311+
#[derive(Debug, Clone, PartialEq, PartialOrd)]
312+
pub struct CommentOptions {
313+
visible: CommentDisplayType,
314+
author: Option<String>,
315+
width: Option<u16>,
316+
height: Option<u16>,
317+
x_scale: Option<f64>,
318+
y_scale: Option<f64>,
319+
color: FormatColor,
320+
font_name: Option<String>,
321+
font_size: Option<f64>,
322+
font_family: Option<u8>,
323+
start_row: WorksheetRow,
324+
start_col: WorksheetCol,
325+
x_offset: i32,
326+
y_offset: i32,
327+
}
328+
329+
impl CommentOptions {
330+
pub(crate) fn into_internal(
331+
&self,
332+
workbook: &Workbook,
333+
) -> libxlsxwriter_sys::lxw_comment_options {
334+
libxlsxwriter_sys::lxw_comment_options {
335+
visible: self.visible.into_internal() as u8,
336+
author: workbook.register_option_str(self.author.as_deref()) as *mut std::ffi::c_char,
337+
width: self.width.unwrap_or_default(),
338+
height: self.height.unwrap_or_default(),
339+
x_scale: self.x_scale.unwrap_or_default(),
340+
y_scale: self.y_scale.unwrap_or_default(),
341+
color: self.color.value(),
342+
font_name: workbook.register_option_str(self.font_name.as_deref())
343+
as *mut std::ffi::c_char,
344+
font_size: self.font_size.unwrap_or_default(),
345+
font_family: self.font_family.unwrap_or_default(),
346+
start_row: self.start_row,
347+
start_col: self.start_col,
348+
x_offset: self.x_offset,
349+
y_offset: self.y_offset,
350+
}
351+
}
352+
}
247353

248354
pub const LXW_DEF_ROW_HEIGHT: f64 = 8.43;
249355
pub const LXW_DEF_ROW_HEIGHT_PIXELS: u32 = 20;
@@ -307,15 +413,16 @@ impl<'a> Worksheet<'a> {
307413
row: WorksheetRow,
308414
col: WorksheetCol,
309415
text: &str,
310-
options: &mut CommentOptions,
416+
options: &CommentOptions,
311417
) -> Result<(), XlsxError> {
418+
let mut options = options.into_internal(self._workbook);
312419
unsafe {
313420
let result = libxlsxwriter_sys::worksheet_write_comment_opt(
314421
self.worksheet,
315422
row,
316423
col,
317-
CString::new(text).unwrap().as_c_str().as_ptr(),
318-
options,
424+
self._workbook.register_str(text),
425+
&mut options,
319426
);
320427
if result == libxlsxwriter_sys::lxw_error_LXW_NO_ERROR {
321428
Ok(())
@@ -960,15 +1067,16 @@ impl<'a> Worksheet<'a> {
9601067
row: WorksheetRow,
9611068
height: f64,
9621069
format: Option<&Format>,
963-
options: &mut RowColOptions,
1070+
options: &RowColOptions,
9641071
) -> Result<(), XlsxError> {
9651072
unsafe {
1073+
let mut options = options.into_internal();
9661074
let result = libxlsxwriter_sys::worksheet_set_row_opt(
9671075
self.worksheet,
9681076
row,
9691077
height,
9701078
format.map(|x| x.format).unwrap_or(std::ptr::null_mut()),
971-
options,
1079+
&mut options,
9721080
);
9731081
if result == libxlsxwriter_sys::lxw_error_LXW_NO_ERROR {
9741082
Ok(())
@@ -1007,15 +1115,16 @@ impl<'a> Worksheet<'a> {
10071115
row: WorksheetRow,
10081116
pixels: u32,
10091117
format: Option<&Format>,
1010-
options: &mut RowColOptions,
1118+
options: &RowColOptions,
10111119
) -> Result<(), XlsxError> {
1120+
let mut options = options.into_internal();
10121121
unsafe {
10131122
let result = libxlsxwriter_sys::worksheet_set_row_pixels_opt(
10141123
self.worksheet,
10151124
row,
10161125
pixels,
10171126
format.map(|x| x.format).unwrap_or(std::ptr::null_mut()),
1018-
options,
1127+
&mut options,
10191128
);
10201129
if result == libxlsxwriter_sys::lxw_error_LXW_NO_ERROR {
10211130
Ok(())
@@ -1054,16 +1163,17 @@ impl<'a> Worksheet<'a> {
10541163
last_col: WorksheetCol,
10551164
width: f64,
10561165
format: Option<&Format>,
1057-
options: &mut RowColOptions,
1166+
options: &RowColOptions,
10581167
) -> Result<(), XlsxError> {
1168+
let mut options = options.into_internal();
10591169
unsafe {
10601170
let result = libxlsxwriter_sys::worksheet_set_column_opt(
10611171
self.worksheet,
10621172
first_col,
10631173
last_col,
10641174
width,
10651175
format.map(|x| x.format).unwrap_or(std::ptr::null_mut()),
1066-
options,
1176+
&mut options,
10671177
);
10681178
if result == libxlsxwriter_sys::lxw_error_LXW_NO_ERROR {
10691179
Ok(())
@@ -1104,14 +1214,15 @@ impl<'a> Worksheet<'a> {
11041214
format: Option<&Format>,
11051215
options: &mut RowColOptions,
11061216
) -> Result<(), XlsxError> {
1217+
let mut options = options.into_internal();
11071218
unsafe {
11081219
let result = libxlsxwriter_sys::worksheet_set_column_pixels_opt(
11091220
self.worksheet,
11101221
first_col,
11111222
last_col,
11121223
pixels,
11131224
format.map(|x| x.format).unwrap_or(std::ptr::null_mut()),
1114-
options,
1225+
&mut options,
11151226
);
11161227
if result == libxlsxwriter_sys::lxw_error_LXW_NO_ERROR {
11171228
Ok(())

0 commit comments

Comments
 (0)