Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Result<T, E> serialization not working #74

Closed
lucatrv opened this issue Jan 14, 2024 · 1 comment
Closed

Bug: Result<T, E> serialization not working #74

lucatrv opened this issue Jan 14, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@lucatrv
Copy link
Contributor

lucatrv commented Jan 14, 2024

Current behavior

The attached code reproduces this issue, only the headers are saved to the xlsx file while all fields are empty.

This issue was initially reported here.

Expected behavior

The saved xlsx file should contain two rows with values underneath the headers row.

Sample code to reproduce

use rust_xlsxwriter::{Format, FormatBorder, Workbook, XlsxError};
use serde::{Serialize, Deserialize};

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 struct.
    #[derive(Deserialize, Serialize)]
    #[serde(rename_all = "PascalCase")]
    struct Student<'a> {
        name: &'a str,
        age: Result<f64, String>,
        id: Result<f64, String>,
    }

    let students = [
        Student {
            name: "Aoife",
            age: Ok(1.0),
            id: Err(String::from("564351")),
        },
        Student {
            name: "Caoimhe",
            age: Err(String::new()),
            id: Ok(443287.0),
        },
    ];

    // 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(())
}


### Environment

- `rust_xlsxwriter` version: 0.61.0


### Any other information

_No response_
@lucatrv lucatrv added the bug Something isn't working label Jan 14, 2024
@jmcnamara jmcnamara self-assigned this Jan 14, 2024
@jmcnamara
Copy link
Owner

Fixed on main with a test. Here is the output from your sample program with the fix:

screenshot

The green triangle is a standard Excel warning about a number stored as text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants