Skip to content

Commit

Permalink
serde: fix stale fields after new serialize_headers()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Jan 2, 2024
1 parent 52155ed commit 4f76a68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,41 +1164,42 @@ use serde::{ser, Deserialize, Deserializer, Serialize};
// information in the serializer.
// -----------------------------------------------------------------------
pub(crate) struct SerializerState {
pub(crate) headers: HashMap<(String, String), CustomSerializeHeader>,
pub(crate) structs: HashMap<String, HashMap<String, CustomSerializeHeader>>,
pub(crate) current_struct: String,
pub(crate) current_field: String,
pub(crate) current_col: ColNum,
pub(crate) current_row: RowNum,
pub(crate) cell_format: Option<Format>,
pub(crate) value_format: Option<Format>,
}

impl SerializerState {
// Create a new SerializerState struct.
pub(crate) fn new() -> SerializerState {
SerializerState {
headers: HashMap::new(),
structs: HashMap::new(),
current_struct: String::new(),
current_field: String::new(),
current_col: 0,
current_row: 0,
cell_format: None,
value_format: None,
}
}

// Check if the current struct/field have been selected to be serialized by
// the user. If it has then set the row/col values for the next write() call.
pub(crate) fn is_known_field(&mut self) -> bool {
let Some(field) = self
.headers
.get_mut(&(self.current_struct.clone(), self.current_field.clone()))
else {
let Some(fields) = self.structs.get_mut(&self.current_struct) else {
return false;
};

let Some(field) = fields.get_mut(&self.current_field) else {
return false;
};

// Set the "current" cell values used to write the serialized data.
self.current_col = field.col;
self.current_row = field.row;
self.cell_format = field.value_format.clone();
self.value_format = field.value_format.clone();

// Increment the row number for the next worksheet.write().
field.row += 1;
Expand Down
17 changes: 9 additions & 8 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6675,6 +6675,9 @@ impl Worksheet {
)));
}

// HashMap<String, CustomSerializeHeader>
let mut field_map = HashMap::new();

let col_initial = col;
for (col_offset, custom_header) in header_options.custom_headers.iter().enumerate() {
if custom_header.skip {
Expand Down Expand Up @@ -6713,15 +6716,13 @@ impl Worksheet {
custom_header.row += 1;
}

self.serializer_state.headers.insert(
(
header_options.struct_name.clone(),
custom_header.field_name.clone(),
),
custom_header,
);
field_map.insert(custom_header.field_name.clone(), custom_header);
}

self.serializer_state
.structs
.insert(header_options.struct_name.clone(), field_map);

Ok(self)
}

Expand All @@ -6748,7 +6749,7 @@ impl Worksheet {
let row = self.serializer_state.current_row;
let col = self.serializer_state.current_col;

match &self.serializer_state.cell_format.clone() {
match &self.serializer_state.value_format.clone() {
Some(format) => self.write_with_format(row, col, data, format)?,
None => self.write(row, col, data)?,
};
Expand Down

0 comments on commit 4f76a68

Please sign in to comment.