use serde::Serialize;
use super::grid::LegacyChartGrid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum SeriesAxis {
Rows,
Columns,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum SeriesAxisEvidence {
RowLabelsEchoed,
ColumnLabelsEchoed,
Inconclusive,
}
pub fn decide_series_axis(
contents: &[u8],
grid: &LegacyChartGrid,
) -> (SeriesAxis, SeriesAxisEvidence) {
let rows = grid.data_rows();
let cols = grid.data_cols();
if rows < 2 || cols < 2 {
return (SeriesAxis::Rows, SeriesAxisEvidence::Inconclusive);
}
let tail = match contents.get(grid.window.end..) {
Some(tail) => tail,
None => return (SeriesAxis::Rows, SeriesAxisEvidence::Inconclusive),
};
let row_hits = (1..=rows)
.filter(|row| echoed(grid.label_record(contents, *row as u16, 0), tail))
.count();
let col_hits = (1..=cols)
.filter(|col| echoed(grid.label_record(contents, 0, *col as u16), tail))
.count();
if row_hits == rows && col_hits < cols {
(SeriesAxis::Rows, SeriesAxisEvidence::RowLabelsEchoed)
} else if col_hits == cols && row_hits < rows {
(SeriesAxis::Columns, SeriesAxisEvidence::ColumnLabelsEchoed)
} else {
(SeriesAxis::Rows, SeriesAxisEvidence::Inconclusive)
}
}
fn echoed(record: Option<&[u8]>, tail: &[u8]) -> bool {
match record {
Some(record) if !record.is_empty() && record.len() <= tail.len() => {
tail.windows(record.len()).any(|window| window == record)
}
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ole_chart::grid::scan_legacy_grid;
use crate::ole_chart::grid::tests::{control_like_grid, synth_grid, Cell};
fn echo_labels(bytes: &[u8], cells: &[(u16, u16)]) -> Vec<u8> {
let grid = scan_legacy_grid(bytes).expect("scan");
let mut out = bytes.to_vec();
for (row, col) in cells {
let record = grid
.label_record(bytes, *row, *col)
.expect("label record")
.to_vec();
out.extend_from_slice(&record);
}
out
}
fn series_major_grid() -> Vec<u8> {
synth_grid(
4,
5,
&[
Cell::Text(2, "항목 1"),
Cell::Text(3, "항목 2"),
Cell::Text(4, "항목 3"),
Cell::Text(5, "항목 4"),
Cell::Text(6, "계열 1"),
Cell::Num(7, 4.3),
Cell::Num(8, 2.5),
Cell::Num(9, 3.5),
Cell::Num(10, 4.5),
Cell::Text(11, "계열 2"),
Cell::Num(12, 2.4),
Cell::Num(13, 4.4),
Cell::Num(14, 1.8),
Cell::Num(15, 2.8),
Cell::Text(16, "계열 3"),
Cell::Num(17, 2.0),
Cell::Num(18, 2.0),
Cell::Num(19, 3.0),
Cell::Num(20, 5.0),
],
)
}
#[test]
fn row_labels_echoed_decides_rows() {
let bytes = echo_labels(&series_major_grid(), &[(1, 0), (2, 0), (3, 0)]);
let grid = scan_legacy_grid(&bytes).expect("scan");
assert_eq!(
decide_series_axis(&bytes, &grid),
(SeriesAxis::Rows, SeriesAxisEvidence::RowLabelsEchoed)
);
}
#[test]
fn column_labels_echoed_decides_columns() {
let bytes = echo_labels(&control_like_grid(), &[(0, 1), (0, 2), (0, 3)]);
let grid = scan_legacy_grid(&bytes).expect("scan");
assert_eq!(
decide_series_axis(&bytes, &grid),
(SeriesAxis::Columns, SeriesAxisEvidence::ColumnLabelsEchoed)
);
}
#[test]
fn partial_echo_on_the_other_axis_does_not_block() {
let bytes = echo_labels(&control_like_grid(), &[(0, 1), (0, 2), (0, 3), (4, 0)]);
let grid = scan_legacy_grid(&bytes).expect("scan");
assert_eq!(
decide_series_axis(&bytes, &grid),
(SeriesAxis::Columns, SeriesAxisEvidence::ColumnLabelsEchoed)
);
}
#[test]
fn single_data_row_ignores_column_echo() {
let pie = synth_grid(
2,
5,
&[
Cell::Text(2, "1 분기"),
Cell::Text(3, "2 분기"),
Cell::Text(4, "3 분기"),
Cell::Text(5, "4 분기"),
Cell::Text(6, "판매"),
Cell::Num(7, 4.3),
Cell::Num(8, 2.5),
Cell::Num(9, 3.5),
Cell::Num(10, 4.5),
],
);
let bytes = echo_labels(&pie, &[(0, 1), (0, 2), (0, 3), (0, 4)]);
let grid = scan_legacy_grid(&bytes).expect("scan");
assert_eq!(
decide_series_axis(&bytes, &grid),
(SeriesAxis::Rows, SeriesAxisEvidence::Inconclusive)
);
}
#[test]
fn no_echo_is_inconclusive() {
let bytes = series_major_grid();
let grid = scan_legacy_grid(&bytes).expect("scan");
assert_eq!(
decide_series_axis(&bytes, &grid),
(SeriesAxis::Rows, SeriesAxisEvidence::Inconclusive)
);
}
#[test]
fn both_axes_echoed_is_inconclusive() {
let bytes = echo_labels(
&series_major_grid(),
&[(1, 0), (2, 0), (3, 0), (0, 1), (0, 2), (0, 3), (0, 4)],
);
let grid = scan_legacy_grid(&bytes).expect("scan");
assert_eq!(
decide_series_axis(&bytes, &grid),
(SeriesAxis::Rows, SeriesAxisEvidence::Inconclusive)
);
}
}