Skip to content

Commit

Permalink
serde: update serialization documentation and examples
Browse files Browse the repository at this point in the history
Feature request: #63
  • Loading branch information
jmcnamara committed Dec 31, 2023
1 parent aca115c commit d18a049
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 131 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ name = "doc_worksheet_serialize_intro"
path = "examples/doc_worksheet_serialize_intro.rs"
required-features = ["serde"]

[[example]]
name = "doc_worksheet_serialize_intro2"
path = "examples/doc_worksheet_serialize_intro2.rs"
required-features = ["serde"]

[[example]]
name = "doc_worksheet_serialize_vectors"
path = "examples/doc_worksheet_serialize_vectors.rs"
Expand Down
19 changes: 9 additions & 10 deletions examples/doc_worksheet_serialize_headers3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// Copyright 2022-2023, John McNamara, [email protected]

//! The following example demonstrates serializing instances of a Serde derived
//! data structure to a worksheet using different methods.
//! data structure to a worksheet using different methods (both serialization
//! and deserialization).
use rust_xlsxwriter::{
CustomSerializeHeader, Format, FormatBorder, SerializeHeadersOptions, Workbook, XlsxError,
};
use serde::Serialize;
use serde::{Deserialize, Serialize};

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
Expand All @@ -30,7 +31,7 @@ fn main() -> Result<(), XlsxError> {
let currency_format = Format::new().set_num_format("$0.00");

// Create a serializable test struct.
#[derive(Serialize)]
#[derive(Deserialize, Serialize)]
struct Produce {
fruit: &'static str,
cost: f64,
Expand All @@ -52,9 +53,9 @@ fn main() -> Result<(), XlsxError> {
cost: 0.75,
};

// 1. Set the serialization location and headers with `serialize_headers()`
// 1. Set the serialization location and headers with `deserialize_headers()`
// and serialize some data.
worksheet.serialize_headers(0, 0, &item1)?;
worksheet.deserialize_headers::<Produce>(0, 0)?;

worksheet.serialize(&item1)?;
worksheet.serialize(&item2)?;
Expand Down Expand Up @@ -82,17 +83,15 @@ fn main() -> Result<(), XlsxError> {
.set_custom_headers(&custom_headers);

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

worksheet.serialize(&item1)?;
worksheet.serialize(&item2)?;
worksheet.serialize(&item3)?;

// 4. Set the serialization location and headers with custom headers. We use
// 4. Set the serialization location and headers with custom options. We use
// the customization to turn off the headers.
let header_options = SerializeHeadersOptions::new()
.hide_headers(true)
.set_custom_headers(&custom_headers);
let header_options = SerializeHeadersOptions::new().hide_headers(true);

// Set the serialization location and custom headers.
worksheet.serialize_headers_with_options(0, 9, &item1, &header_options)?;
Expand Down
6 changes: 3 additions & 3 deletions examples/doc_worksheet_serialize_headers_rename1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! using Serde field attributes.
use rust_xlsxwriter::{Workbook, XlsxError};
use serde::Serialize;
use serde::{Deserialize, Serialize};

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
Expand All @@ -15,7 +15,7 @@ fn main() -> Result<(), XlsxError> {
let worksheet = workbook.add_worksheet();

// Create a serializable test struct. Note the serde attributes.
#[derive(Serialize)]
#[derive(Deserialize, Serialize)]
struct Produce {
#[serde(rename = "Item")]
fruit: &'static str,
Expand All @@ -41,7 +41,7 @@ fn main() -> Result<(), XlsxError> {
};

// Set the serialization location and headers.
worksheet.serialize_headers(0, 0, &item1)?;
worksheet.deserialize_headers::<Produce>(0, 0)?;

// Serialize the data.
worksheet.serialize(&item1)?;
Expand Down
9 changes: 4 additions & 5 deletions examples/doc_worksheet_serialize_headers_rename2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// Copyright 2022-2023, John McNamara, [email protected]

//! The following example demonstrates renaming fields during serialization by
//! specifying custom headers and renaming them there. You must still specify
//! the actual field name to serialize in the `new()` constructor.
//! specifying custom headers and renaming them there.
//!
use rust_xlsxwriter::{CustomSerializeHeader, SerializeHeadersOptions, Workbook, XlsxError};
use serde::Serialize;
use serde::{Deserialize, Serialize};

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
Expand All @@ -16,7 +15,7 @@ fn main() -> Result<(), XlsxError> {
let worksheet = workbook.add_worksheet();

// Create a serializable test struct.
#[derive(Serialize)]
#[derive(Deserialize, Serialize)]
struct Produce {
fruit: &'static str,
cost: f64,
Expand Down Expand Up @@ -46,7 +45,7 @@ fn main() -> Result<(), XlsxError> {
let header_options = SerializeHeadersOptions::new().set_custom_headers(&custom_headers);

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

// Serialize the data.
worksheet.serialize(&item1)?;
Expand Down
8 changes: 4 additions & 4 deletions examples/doc_worksheet_serialize_headers_skip1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! to tell rustc not emit a `dead_code` warning.
use rust_xlsxwriter::{Workbook, XlsxError};
use serde::Serialize;
use serde::{Deserialize, Serialize};

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
Expand All @@ -16,12 +16,12 @@ fn main() -> Result<(), XlsxError> {
let worksheet = workbook.add_worksheet();

// Create a serializable test struct. Note the serde attribute.
#[derive(Serialize)]
#[derive(Deserialize, Serialize)]
struct Produce {
fruit: &'static str,
cost: f64,

#[serde(skip_serializing)]
#[serde(skip)]
#[allow(dead_code)]
in_stock: bool,
}
Expand All @@ -46,7 +46,7 @@ fn main() -> Result<(), XlsxError> {
};

// Set the serialization location and headers.
worksheet.serialize_headers(0, 0, &item1)?;
worksheet.deserialize_headers::<Produce>(0, 0)?;

// Serialize the data.
worksheet.serialize(&item1)?;
Expand Down
13 changes: 8 additions & 5 deletions examples/doc_worksheet_serialize_headers_skip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

//! The following example demonstrates skipping fields during serialization by
//! omitting them from the serialization headers. To do this we need to specify
//! custom headers.
//! custom headers and set `use_custom_headers_only()`.
use rust_xlsxwriter::{CustomSerializeHeader, SerializeHeadersOptions, Workbook, XlsxError};
use serde::Serialize;
use serde::{Deserialize, Serialize};

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
Expand All @@ -16,7 +16,7 @@ fn main() -> Result<(), XlsxError> {
let worksheet = workbook.add_worksheet();

// Create a serializable test struct.
#[derive(Serialize)]
#[derive(Deserialize, Serialize)]
struct Produce {
fruit: &'static str,
cost: f64,
Expand All @@ -42,17 +42,20 @@ fn main() -> Result<(), XlsxError> {
in_stock: false,
};

// Set up the custom headers.
// Only set up the custom headers we want and omit "in_stock".
let custom_headers = [
CustomSerializeHeader::new("fruit"),
CustomSerializeHeader::new("cost"),
];

// Note the use of "use_custom_headers_only" to only serialize the named
// custom headers.
let header_options = SerializeHeadersOptions::new()
.use_custom_headers_only(true)
.set_custom_headers(&custom_headers);

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

// Serialize the data.
worksheet.serialize(&item1)?;
Expand Down
17 changes: 7 additions & 10 deletions examples/doc_worksheet_serialize_headers_skip3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! explicitly skipping them via custom headers.
use rust_xlsxwriter::{CustomSerializeHeader, SerializeHeadersOptions, Workbook, XlsxError};
use serde::Serialize;
use serde::{Deserialize, Serialize};

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
Expand All @@ -15,7 +15,7 @@ fn main() -> Result<(), XlsxError> {
let worksheet = workbook.add_worksheet();

// Create a serializable test struct.
#[derive(Serialize)]
#[derive(Deserialize, Serialize)]
struct Produce {
fruit: &'static str,
cost: f64,
Expand All @@ -41,16 +41,13 @@ fn main() -> Result<(), XlsxError> {
in_stock: false,
};

// Set up the custom headers.
let custom_headers = [
CustomSerializeHeader::new("fruit"),
CustomSerializeHeader::new("cost"),
CustomSerializeHeader::new("in_stock").skip(true),
];
let header_options = SerializeHeadersOptions::new().set_custom_headers(&custom_headers);
// We only need to set a custom header for the field we want to skip.
let header_options = SerializeHeadersOptions::new()
.set_custom_headers(&[CustomSerializeHeader::new("in_stock").skip(true)]);

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

// Serialize the data.
worksheet.serialize(&item1)?;
worksheet.serialize(&item2)?;
Expand Down
55 changes: 55 additions & 0 deletions examples/doc_worksheet_serialize_intro2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2023, John McNamara, [email protected]

//! The following example demonstrates serializing instances of a Serde derived
//! data structure to a worksheet. This version uses header deserialization.
use rust_xlsxwriter::{Format, FormatBorder, Workbook, XlsxError};
use serde::{Deserialize, Serialize};

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

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

// Add some formats to use with the serialization data.
let header_format = Format::new()
.set_bold()
.set_border(FormatBorder::Thin)
.set_background_color("C6E0B4");

// Create a serializable test struct.
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
struct Student<'a> {
name: &'a str,
age: u8,
id: u32,
}

let students = [
Student {
name: "Aoife",
age: 25,
id: 564351,
},
Student {
name: "Caoimhe",
age: 21,
id: 443287,
},
];

// Set up the start location and headers of the data to be serialized.
worksheet.deserialize_headers_with_format::<Student>(1, 3, &header_format)?;

// Serialize the data.
worksheet.serialize(&students)?;

// Save the file.
workbook.save("serialize.xlsx")?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2399,8 +2399,8 @@ impl From<String> for Format {
}
}

/// The `Color` enum defines Excel colors the can be used throughout the
/// `rust_xlsxwriter`.
/// The `Color` enum defines Excel colors that can be used throughout the
/// `rust_xlsxwriter` APIs.
///
/// There are 3 types of colors within the enum:
///
Expand Down
Loading

0 comments on commit d18a049

Please sign in to comment.