use crate::coordinate::Range;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PivotAxis {
Row,
Column,
Page,
Values,
#[default]
Unused,
}
impl PivotAxis {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"axisRow" => Self::Row,
"axisCol" => Self::Column,
"axisPage" => Self::Page,
"axisValues" => Self::Values,
_ => Self::Unused,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Subtotal {
#[default]
Sum,
Count,
Average,
Max,
Min,
Product,
CountNums,
StdDev,
StdDevP,
Var,
VarP,
}
impl Subtotal {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"count" => Self::Count,
"average" => Self::Average,
"max" => Self::Max,
"min" => Self::Min,
"product" => Self::Product,
"countNums" => Self::CountNums,
"stdDev" => Self::StdDev,
"stdDevp" => Self::StdDevP,
"var" => Self::Var,
"varp" => Self::VarP,
_ => Self::Sum,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Sum => "sum",
Self::Count => "count",
Self::Average => "average",
Self::Max => "max",
Self::Min => "min",
Self::Product => "product",
Self::CountNums => "countNums",
Self::StdDev => "stdDev",
Self::StdDevP => "stdDevp",
Self::Var => "var",
Self::VarP => "varp",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PivotField {
pub axis: PivotAxis,
pub data_field: bool,
pub default_subtotal: bool,
pub show_all: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DataField {
pub name: Option<String>,
pub field: u32,
pub subtotal: Subtotal,
pub number_format: Option<u32>,
}
#[expect(
clippy::struct_excessive_bools,
reason = "one field per attribute of `<pivotTableStyleInfo>`"
)]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PivotStyleInfo {
pub name: Option<String>,
pub show_row_headers: bool,
pub show_column_headers: bool,
pub show_row_stripes: bool,
pub show_column_stripes: bool,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct PivotTable {
pub name: String,
pub cache_id: u32,
pub location: Option<Range>,
pub first_data_row: u32,
pub first_data_col: u32,
pub fields: Vec<PivotField>,
pub row_fields: Vec<i32>,
pub column_fields: Vec<i32>,
pub page_fields: Vec<i32>,
pub data_fields: Vec<DataField>,
pub row_grand_totals: bool,
pub column_grand_totals: bool,
pub style: PivotStyleInfo,
pub origin: Option<PivotOrigin>,
}
impl PivotTable {
#[must_use]
pub fn is_unchanged(&self) -> bool {
self.origin.as_ref().is_some_and(|o| {
let mut read = (*o.read).clone();
read.origin.clone_from(&self.origin);
read == *self
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PivotOrigin {
pub(crate) part: String,
pub(crate) read: Box<PivotTable>,
}
impl PivotOrigin {
#[must_use]
pub fn part(&self) -> &str {
&self.part
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CacheSource {
pub sheet: Option<String>,
pub range: Option<Range>,
pub name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CacheField {
pub name: String,
pub number_format: Option<u32>,
pub shared_items: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct PivotCache {
pub id: u32,
pub source: CacheSource,
pub fields: Vec<CacheField>,
pub definition_part: String,
pub origin: Option<Box<Self>>,
}
impl PivotCache {
#[must_use]
pub fn is_unchanged(&self) -> bool {
self.origin.as_deref().is_some_and(|read| {
read.id == self.id
&& read.source == self.source
&& read.fields == self.fields
&& read.definition_part == self.definition_part
})
}
}