mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 13:17:41 +00:00
Merge pull request #2090 from dotCooCoo/fix/xls-testing-sheet-count
craft-xls: the fixture writer refuses sheet counts the reader cannot read
This commit is contained in:
@@ -16,7 +16,7 @@ use crate::records::{Cursor, Record, Records};
|
||||
use crate::{Error, Limits, Read, Warning, cond, drawing, notes, styles};
|
||||
|
||||
const MAX_STRINGS: usize = 4_000_000;
|
||||
const MAX_SHEETS: usize = 4096;
|
||||
pub(crate) const MAX_SHEETS: usize = 4096;
|
||||
const MAX_NAMES: usize = 65_536;
|
||||
const MAX_XFS: usize = 65_536;
|
||||
const MAX_MERGES: usize = 100_000;
|
||||
|
||||
@@ -54,13 +54,24 @@ fn number(cell: &str) -> Option<(u16, f64)> {
|
||||
/// Number records, a cell `date:<serial>` is a Number with the date format `m/d/yyyy` (built-in
|
||||
/// format 14), an empty cell is left out, and everything else, including a `date:` cell whose
|
||||
/// serial is not a number, goes through the shared-string table (as UTF-16 when it isn't
|
||||
/// Latin-1). A nonempty cell past row 65,535 or column 255 (zero-based) returns
|
||||
/// [`craft_cfb::Error::Invalid`] naming its sheet, row and column, and a compound file that cannot
|
||||
/// be written returns the writer's error.
|
||||
/// Latin-1). No sheets, or more than the 4,096 sheets [`crate::read()`] accepts, returns
|
||||
/// [`craft_cfb::Error::Invalid`] naming the count. A nonempty cell past row 65,535 or column 255
|
||||
/// (zero-based) returns [`craft_cfb::Error::Invalid`] naming its sheet, row and column, and a
|
||||
/// compound file that cannot be written returns the writer's error.
|
||||
///
|
||||
/// shortcut: characters outside the Basic Multilingual Plane are dropped, sheet names are cut at
|
||||
/// 255 characters and strings at 4,000 (so each fits one record); lift when a fixture needs more.
|
||||
pub fn workbook(sheets: &[(&str, Vec<Vec<String>>)]) -> Result<Vec<u8>, craft_cfb::Error> {
|
||||
if sheets.is_empty() {
|
||||
return Err(craft_cfb::Error::Invalid("fixture has no sheets; craft-xls reads a workbook only when it has a worksheet".into()));
|
||||
}
|
||||
if sheets.len() > crate::read::MAX_SHEETS {
|
||||
return Err(craft_cfb::Error::Invalid(format!(
|
||||
"fixture has {} sheets; craft-xls can read at most {}",
|
||||
sheets.len(),
|
||||
crate::read::MAX_SHEETS
|
||||
)));
|
||||
}
|
||||
let mut sst: Vec<&str> = Vec::new();
|
||||
let mut references = 0usize;
|
||||
for (name, rows) in sheets {
|
||||
|
||||
@@ -634,6 +634,25 @@ fn the_fixture_writer_reads_back() {
|
||||
assert_eq!(v(&wb, 1, "A1"), text("Name"));
|
||||
}
|
||||
|
||||
/// A fixture holds 1 to 4,096 sheets, the counts the reader accepts; any other count is an error
|
||||
/// naming it, returned before anything is written.
|
||||
#[test]
|
||||
fn the_fixture_writer_rejects_a_sheet_count_the_reader_refuses() {
|
||||
let names: Vec<String> = (0..4097).map(|i| format!("S{i}")).collect();
|
||||
let sheets: Vec<(&str, Vec<Vec<String>>)> = names.iter().map(|n| (n.as_str(), Vec::new())).collect();
|
||||
match craft_xls::testing::workbook(&sheets) {
|
||||
Err(craft_cfb::Error::Invalid(m)) => assert!(m.contains("4097 sheets") && m.contains("at most 4096"), "{m}"),
|
||||
other => panic!("{:?}", other.map(|b| b.len())),
|
||||
}
|
||||
match craft_xls::testing::workbook(&[]) {
|
||||
Err(craft_cfb::Error::Invalid(m)) => assert!(m.contains("no sheets"), "{m}"),
|
||||
other => panic!("{:?}", other.map(|b| b.len())),
|
||||
}
|
||||
let read = craft_xls::read(&craft_xls::testing::workbook(&sheets[..4096]).unwrap()).unwrap();
|
||||
assert_eq!(read.warnings, []);
|
||||
assert!(read.workbook.sheets.iter().map(|s| s.name.as_str()).eq(names[..4096].iter().map(String::as_str)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fixture_that_cannot_be_written_returns_the_writer_error() {
|
||||
assert_eq!(craft_xls::testing::compound("", b"x"), Err(craft_cfb::Error::BadName(String::new())));
|
||||
|
||||
Reference in New Issue
Block a user