Skip to content

Commit

Permalink
derive: initial for ExcelSerialize
Browse files Browse the repository at this point in the history
Add support for ExcelSerialize trait via a proc macro. Support
rust_xlsxwriter attributes and also Serde attributes.

See #66
  • Loading branch information
jmcnamara committed Jan 7, 2024
1 parent 70c3eb7 commit a105465
Show file tree
Hide file tree
Showing 21 changed files with 1,479 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ polars= {version = "0.35.4", default-features = false, features = [], optional =
js-sys = {version = "0.3.64", optional = true}
wasm-bindgen = {version = "0.2.87", optional = true}
serde = { version = "1.0.193", features = ["derive"], optional = true }
rust_xlsxwriter_derive = { path = "rust_xlsxwriter_derive" }

[dev-dependencies]
pretty_assertions = "1.3.0"

[features]
# `default`: Includes all the standard functionality.
default = []
default = ["serde"]

# `zlib`: Adds a dependency on zlib and a C compiler. This includes the same
# features as `default` but is 1.5x faster for large files.
Expand Down
27 changes: 9 additions & 18 deletions examples/app_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@
//! Example of serializing Serde derived structs to an Excel worksheet using
//! `rust_xlsxwriter`.
use rust_xlsxwriter::{
CustomSerializeField, Format, FormatBorder, SerializeFieldOptions, Workbook, XlsxError,
};
use serde::{Deserialize, Serialize};
use rust_xlsxwriter::{ExcelSerialize, Workbook, XlsxError};
use serde::Serialize;

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();

// Set some formats.
let header_format = Format::new()
// Create a serializable struct.
#[derive(ExcelSerialize, Serialize)]
#[rust_xlsxwriter(header_format = Format::new()
.set_bold()
.set_border(FormatBorder::Thin)
.set_background_color("C6EFCE");

let value_format = Format::new().set_num_format("$0.00");

// Create a serializable struct.
#[derive(Deserialize, Serialize)]
.set_background_color("C6EFCE"))]
#[serde(rename_all = "PascalCase")]
struct Produce {
fruit: &'static str,

#[rust_xlsxwriter(value_format = Format::new().set_num_format("$0.00"))]
cost: f64,
}

Expand All @@ -48,13 +44,8 @@ fn main() -> Result<(), XlsxError> {
cost: 0.75,
};

// Set the custom headers.
let header_options = SerializeFieldOptions::new()
.set_header_format(&header_format)
.set_custom_headers(&[CustomSerializeField::new("Cost").set_value_format(&value_format)]);

// Set the serialization location and headers.
worksheet.deserialize_headers_with_options::<Produce>(0, 0, &header_options)?;
worksheet.set_serialize_headers::<Produce>(0, 0)?;

// Serialize the data.
worksheet.serialize(&item1)?;
Expand Down
3 changes: 2 additions & 1 deletion examples/doc_worksheet_serialize_headers_format8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ fn main() -> Result<(), XlsxError> {
CustomSerializeField::new("cost").rename("Price"),
CustomSerializeField::new("fruit").rename("Item"),
];
let header_options = SerializeFieldOptions::new()
let header_options = SerializeFieldOptions::new();
let header_options = header_options
.set_custom_headers(&custom_headers)
.use_custom_headers_only(true);

Expand Down
6 changes: 6 additions & 0 deletions rust_xlsxwriter_derive/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
/Cargo.lock
.DS_Store
.vscode
*.bak

11 changes: 11 additions & 0 deletions rust_xlsxwriter_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "rust_xlsxwriter_derive"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
quote = "1.0"
syn = {version = "2.0", features = ["extra-traits"]}
Loading

0 comments on commit a105465

Please sign in to comment.