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 99ad1bf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
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 99ad1bf

Please sign in to comment.