-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serde: update serialization documentation and examples
Feature request: #63
- Loading branch information
Showing
32 changed files
with
1,010 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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. | ||
use rust_xlsxwriter::{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(); | ||
|
||
// Create a serializable struct. | ||
#[derive(Deserialize, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct Produce { | ||
fruit: &'static str, | ||
cost: f64, | ||
} | ||
|
||
// Create some data instances. | ||
let item1 = Produce { | ||
fruit: "Peach", | ||
cost: 1.05, | ||
}; | ||
let item2 = Produce { | ||
fruit: "Plum", | ||
cost: 0.15, | ||
}; | ||
let item3 = Produce { | ||
fruit: "Pear", | ||
cost: 0.75, | ||
}; | ||
|
||
// Set up the start location and headers of the data to be serialized. | ||
worksheet.deserialize_headers::<Produce>(0, 0)?; | ||
|
||
// Serialize the data. | ||
worksheet.serialize(&item1)?; | ||
worksheet.serialize(&item2)?; | ||
worksheet.serialize(&item3)?; | ||
|
||
// Save the file. | ||
workbook.save("serialize.xlsx")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
use rust_xlsxwriter::{Format, 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(); | ||
|
||
// Add a simple format for the headers. | ||
let format = Format::new().set_bold(); | ||
|
||
// Create a serializable struct. | ||
#[derive(Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct Produce { | ||
fruit: &'static str, | ||
cost: f64, | ||
} | ||
|
||
// Create some data instances. | ||
let item1 = Produce { | ||
fruit: "Peach", | ||
cost: 1.05, | ||
}; | ||
let item2 = Produce { | ||
fruit: "Plum", | ||
cost: 0.15, | ||
}; | ||
let item3 = Produce { | ||
fruit: "Pear", | ||
cost: 0.75, | ||
}; | ||
|
||
// Set up the start location and headers of the data to be serialized using | ||
// any temporary or valid instance. | ||
worksheet.serialize_headers_with_format(0, 0, &item1, &format)?; | ||
|
||
// Serialize the data. | ||
worksheet.serialize(&item1)?; | ||
worksheet.serialize(&item2)?; | ||
worksheet.serialize(&item3)?; | ||
|
||
// Save the file. | ||
workbook.save("serialize.xlsx")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.