Skip to content

Commit

Permalink
worksheet: add IntoExcelData trait for Result<T, E>
Browse files Browse the repository at this point in the history
Add support for handling Result<T, E> values in the write() method.

Feature request #64
  • Loading branch information
jmcnamara committed Jan 2, 2024
1 parent 4f76a68 commit cce07d3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
49 changes: 38 additions & 11 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,10 @@ impl Worksheet {
/// - [`ExcelDateTime`].
/// - [`Formula`].
/// - [`Url`].
/// - [`Option<T>`]: If [`Some`] and `T` is a supported type then the `data`
/// is written. If [`None`] then nothing is written.
/// - [`Option<T>`]: If `T` is a supported type then write the [`Some`]
/// value but ignore the [`None`].
/// - [`Result<T, E>`]: If `T` and `E` are supported types then write `T`
/// or `E` depending on the result.
///
/// If the `chrono` feature is enabled you can use the following types:
///
Expand All @@ -576,12 +578,9 @@ impl Worksheet {
/// - [`chrono::NaiveTime`].
///
/// [`Chrono`]: https://docs.rs/chrono/latest/chrono/index.html
/// [`chrono::NaiveDate`]:
/// https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html
/// [`chrono::NaiveTime`]:
/// https://docs.rs/chrono/latest/chrono/naive/struct.NaiveTime.html
/// [`chrono::NaiveDateTime`]:
/// https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html
/// [`chrono::NaiveDate`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html
/// [`chrono::NaiveTime`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveTime.html
/// [`chrono::NaiveDateTime`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html
///
/// Users can also use this method to write their own data types to Excel by
/// implementing the [`IntoExcelData`] trait.
Expand Down Expand Up @@ -623,9 +622,10 @@ impl Worksheet {
/// - [`ExcelDateTime`].
/// - [`Formula`].
/// - [`Url`].
/// - [`Option<T>`]: If [`Some`] and `T` is a supported type then the `data`
/// is written. If [`None`] then a formatted blank cell is written using
/// [`Worksheet::write_blank()`].
/// - [`Option<T>`]: If `T` is a supported type then write the [`Some`]
/// value or [`None`] as a formatted blank cell.
/// - [`Result<T, E>`]: If `T` and `E` are supported types then write `T`
/// or `E` depending on the result.
///
/// If the `chrono` feature is enabled you can use the following types:
///
Expand Down Expand Up @@ -12401,6 +12401,33 @@ impl<T: IntoExcelData> IntoExcelData for Option<T> {
}
}

impl<T: IntoExcelData, E: IntoExcelData> IntoExcelData for Result<T, E> {
fn write(
self,
worksheet: &mut Worksheet,
row: RowNum,
col: ColNum,
) -> Result<&mut Worksheet, XlsxError> {
match self {
Ok(data) => worksheet.write(row, col, data),
Err(data) => worksheet.write(row, col, data),
}
}

fn write_with_format<'a>(
self,
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
) -> Result<&'a mut Worksheet, XlsxError> {
match self {
Ok(data) => worksheet.write_with_format(row, col, data, format),
Err(data) => worksheet.write_with_format(row, col, data, format),
}
}
}

// -----------------------------------------------------------------------
// Helper enums/structs/functions.
// -----------------------------------------------------------------------
Expand Down
56 changes: 54 additions & 2 deletions tests/integration/bootstrap06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,36 @@ fn create_new_xlsx_file_2(filename: &str) -> Result<(), XlsxError> {
Ok(())
}

// Test with Result<T, E>.
fn create_new_xlsx_file_3(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();

let result: Result<&str, &str> = Ok("Hello");

worksheet.write(0, 0, result)?;

workbook.save(filename)?;

Ok(())
}

// Test with Result<T, E>.
fn create_new_xlsx_file_4(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();

let result: Result<&str, &str> = Err("Hello");

worksheet.write(0, 0, result)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn bootstrap06_write_string_with_format_1() {
fn bootstrap06_write_string_1() {
let test_runner = common::TestRunner::new()
.set_name("bootstrap06")
.set_function(create_new_xlsx_file_1)
Expand All @@ -45,7 +73,7 @@ fn bootstrap06_write_string_with_format_1() {
}

#[test]
fn bootstrap06_write_string_with_format_2() {
fn bootstrap06_write_string_2() {
let test_runner = common::TestRunner::new()
.set_name("bootstrap06")
.set_function(create_new_xlsx_file_2)
Expand All @@ -55,3 +83,27 @@ fn bootstrap06_write_string_with_format_2() {
test_runner.assert_eq();
test_runner.cleanup();
}

#[test]
fn bootstrap06_write_string_3() {
let test_runner = common::TestRunner::new()
.set_name("bootstrap06")
.set_function(create_new_xlsx_file_3)
.unique("3")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}

#[test]
fn bootstrap06_write_string_4() {
let test_runner = common::TestRunner::new()
.set_name("bootstrap06")
.set_function(create_new_xlsx_file_4)
.unique("4")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}

0 comments on commit cce07d3

Please sign in to comment.