Skip to content

Commit

Permalink
serde: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Jan 2, 2024
1 parent cce07d3 commit f8e140c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
20 changes: 8 additions & 12 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,9 +1167,7 @@ pub(crate) struct SerializerState {
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) value_format: Option<Format>,
}

impl SerializerState {
Expand All @@ -1179,32 +1177,30 @@ impl SerializerState {
structs: HashMap::new(),
current_struct: String::new(),
current_field: String::new(),
current_col: 0,
current_row: 0,
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 {
// the user. If it has then set the row value for the next write() call.
pub(crate) fn current_state(&mut self) -> Result<(RowNum, ColNum, Option<Format>), ()> {
let Some(fields) = self.structs.get_mut(&self.current_struct) else {
return false;
return Err(());
};

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

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

// Increment the row number for the next worksheet.write().
field.row += 1;

true
Ok((row, col, value_format))
}
}

Expand Down
23 changes: 11 additions & 12 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6742,19 +6742,18 @@ impl Worksheet {
&mut self,
data: impl IntoExcelData,
) -> Result<(), XlsxError> {
if !self.serializer_state.is_known_field() {
return Ok(());
let result = self.serializer_state.current_state();

match result {
Ok(result) => {
let (row, col, value_format) = result;
match &value_format {
Some(format) => self.write_with_format(row, col, data, format).map(|_| ()),
None => self.write(row, col, data).map(|_| ()),
}
}
Err(()) => Ok(()),
}

let row = self.serializer_state.current_row;
let col = self.serializer_state.current_col;

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

Ok(())
}

// -----------------------------------------------------------------------
Expand Down

0 comments on commit f8e140c

Please sign in to comment.