Skip to content

Commit

Permalink
fix!: detach format lifetime from worksheet
Browse files Browse the repository at this point in the history
At the moment the lifetime of `Format` inside
`IntoExcelData::write_to_format` is bound to the lifetime of the
worksheet. However, this is not actually required anywhere in the code,
and with this constraint it is not possible to create abstractions with
an internal formatter because, once the ownership of the data is taken
by `write_with_format`, the stored format cannot outlive the call.

This breaking change should simplify the management of formatters.
  • Loading branch information
dodomorandi authored and jmcnamara committed Feb 21, 2024
1 parent 2941d5b commit 6fea23e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/app_write_generic_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl IntoExcelData for UnixTime {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
// Convert the Unix time to an Excel datetime.
let datetime = 25569.0 + (self.seconds as f64 / (24.0 * 60.0 * 60.0));
Expand Down
34 changes: 17 additions & 17 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ impl Worksheet {
row: RowNum,
col: ColNum,
data: T,
format: &'a Format,
) -> Result<&mut Worksheet, XlsxError>
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError>
where
T: IntoExcelData,
{
Expand Down Expand Up @@ -12817,7 +12817,7 @@ pub trait IntoExcelData {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError>;
}

Expand All @@ -12838,7 +12838,7 @@ macro_rules! write_string_trait_impl {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
worksheet.store_string(row, col, self.into(), Some(format))
}
Expand All @@ -12864,7 +12864,7 @@ macro_rules! write_number_trait_impl {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
worksheet.store_number(row, col, self, Some(format))
}
Expand All @@ -12891,7 +12891,7 @@ macro_rules! write_number_trait_impl {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
worksheet.store_number(row, col, self as f64, Some(format))
}
Expand All @@ -12915,7 +12915,7 @@ impl IntoExcelData for bool {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
worksheet.store_boolean(row, col, self, Some(format))
}
Expand All @@ -12937,7 +12937,7 @@ impl IntoExcelData for &ExcelDateTime {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
let number = self.to_excel();
worksheet.store_datetime(row, col, number, Some(format))
Expand All @@ -12960,7 +12960,7 @@ impl IntoExcelData for ExcelDateTime {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
let number = self.to_excel_serial_date();
worksheet.store_datetime(row, col, number, Some(format))
Expand All @@ -12985,7 +12985,7 @@ impl IntoExcelData for &NaiveDateTime {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
let number = ExcelDateTime::chrono_datetime_to_excel(self);
worksheet.store_datetime(row, col, number, Some(format))
Expand All @@ -13010,7 +13010,7 @@ impl IntoExcelData for &NaiveDate {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
let number = ExcelDateTime::chrono_date_to_excel(self);
worksheet.store_datetime(row, col, number, Some(format))
Expand All @@ -13035,7 +13035,7 @@ impl IntoExcelData for &NaiveTime {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
let number = ExcelDateTime::chrono_time_to_excel(self);
worksheet.store_datetime(row, col, number, Some(format))
Expand All @@ -13057,7 +13057,7 @@ impl IntoExcelData for Formula {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
worksheet.store_formula(row, col, self, Some(format))
}
Expand All @@ -13078,7 +13078,7 @@ impl IntoExcelData for &Formula {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
worksheet.store_formula(row, col, (*self).clone(), Some(format))
}
Expand All @@ -13099,7 +13099,7 @@ impl IntoExcelData for Url {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
worksheet.store_url(row, col, self, Some(format))
}
Expand All @@ -13123,7 +13123,7 @@ impl<T: IntoExcelData> IntoExcelData for Option<T> {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
match self {
Some(data) => worksheet.write_with_format(row, col, data, format),
Expand All @@ -13150,7 +13150,7 @@ impl<T: IntoExcelData, E: IntoExcelData> IntoExcelData for Result<T, E> {
worksheet: &'a mut Worksheet,
row: RowNum,
col: ColNum,
format: &'a Format,
format: &Format,
) -> Result<&'a mut Worksheet, XlsxError> {
match self {
Ok(data) => worksheet.write_with_format(row, col, data, format),
Expand Down

0 comments on commit 6fea23e

Please sign in to comment.