From ad611b37642b01acdec942445931af5365832301 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 12 Apr 2025 08:22:29 -0700 Subject: [PATCH] more builder removal --- rust/pspp/src/output/pivot/mod.rs | 65 ++++++++++++++---------------- rust/pspp/src/output/pivot/test.rs | 44 ++++++++++---------- 2 files changed, 53 insertions(+), 56 deletions(-) diff --git a/rust/pspp/src/output/pivot/mod.rs b/rust/pspp/src/output/pivot/mod.rs index 97427729c5..212c950ddd 100644 --- a/rust/pspp/src/output/pivot/mod.rs +++ b/rust/pspp/src/output/pivot/mod.rs @@ -271,6 +271,36 @@ impl Iterator for AxisIterator { } impl PivotTable { + pub fn with_look(mut self, look: Arc) -> Self { + self.look = look; + self + } + pub fn insert_number(&mut self, data_indexes: &[usize], number: Option, class: Class) { + let format = match class { + Class::Other => Settings::global().default_format, + Class::Integer => Format::F40, + Class::Correlations => Format::F40_3, + Class::Significance => Format::F40_3, + Class::Percent => Format::PCT40_1, + Class::Residual => Format::F40_2, + Class::Count => Format::F40, // XXX + }; + let value = Value::new(ValueInner::Number { + show: None, + format, + honor_small: class == Class::Other, + value: number, + var_name: None, + value_label: None, + }); + self.insert(data_indexes, value); + } + + pub fn with_footnotes(mut self, footnotes: Footnotes) -> Self { + debug_assert!(self.footnotes.is_empty()); + self.footnotes = footnotes; + self + } fn axis_values(&self, axis: Axis3) -> AxisIterator { AxisIterator { indexes: repeat_n(0, self.axes[axis].dimensions.len()).collect(), @@ -472,41 +502,6 @@ impl PivotTableBuilder { footnotes: Footnotes::new(), } } - pub fn with_look(mut self, look: Arc) -> Self { - self.look = look; - self - } - pub fn insert_number(&mut self, data_indexes: &[usize], number: Option, class: Class) { - let format = match class { - Class::Other => Settings::global().default_format, - Class::Integer => Format::F40, - Class::Correlations => Format::F40_3, - Class::Significance => Format::F40_3, - Class::Percent => Format::PCT40_1, - Class::Residual => Format::F40_2, - Class::Count => Format::F40, // XXX - }; - let value = Value::new(ValueInner::Number { - show: None, - format, - honor_small: class == Class::Other, - value: number, - var_name: None, - value_label: None, - }); - self.insert(data_indexes, value); - } - pub fn insert(&mut self, data_indexes: &[usize], value: Value) { - self.cells.insert( - cell_index(data_indexes, self.dimensions.iter().map(|d| d.len())), - value, - ); - } - pub fn with_footnotes(mut self, footnotes: Footnotes) -> Self { - debug_assert!(self.footnotes.is_empty()); - self.footnotes = footnotes; - self - } pub fn build(self) -> PivotTable { let mut table = PivotTable::new(self.title, self.look.clone()); table.dimensions = self.dimensions; diff --git a/rust/pspp/src/output/pivot/test.rs b/rust/pspp/src/output/pivot/test.rs index dace8ff009..e3a50ff2e0 100644 --- a/rust/pspp/src/output/pivot/test.rs +++ b/rust/pspp/src/output/pivot/test.rs @@ -30,11 +30,13 @@ fn d1(title: &str, axis: Axis3) -> PivotTable { .with("a2") .with("a3"), ); - let mut pt = PivotTableBuilder::new(Value::new_text(title), vec![(axis, dimension)]); + let mut pt = PivotTableBuilder::new(Value::new_text(title), vec![(axis, dimension)]) + .build() + .with_look(Arc::new(test_look())); for i in 0..3 { pt.insert(&[i], Value::new_integer(Some(i as f64))); } - pt.with_look(Arc::new(test_look())).build() + pt } #[test] @@ -95,7 +97,8 @@ fn d2(title: &str, axes: [Axis3; 2], dimension_labels: Option) -> .with("b3"), ); - let mut pt = PivotTableBuilder::new(Value::new_text(title), vec![(axes[0], d1), (axes[1], d2)]); + let mut pt = + PivotTableBuilder::new(Value::new_text(title), vec![(axes[0], d1), (axes[1], d2)]).build(); let mut i = 0; for b in 0..3 { for a in 0..3 { @@ -107,7 +110,7 @@ fn d2(title: &str, axes: [Axis3; 2], dimension_labels: Option) -> Some(position) => test_look().with_row_label_position(position), None => test_look(), }; - pt.with_look(Arc::new(look)).build() + pt.with_look(Arc::new(look)) } #[track_caller] @@ -575,7 +578,8 @@ fn footnotes() { let mut pt = PivotTableBuilder::new( Value::new_text("Pivot Table with Alphabetic Subscript Footnotes").with_footnote(&f0), vec![a, d], - ); + ) + .build(); pt.insert(&[0, 0], Value::new_number(Some(0.0))); pt.insert(&[1, 0], Value::new_number(Some(1.0)).with_footnote(&f0)); pt.insert(&[0, 1], Value::new_number(Some(2.0)).with_footnote(&f1)); @@ -588,7 +592,6 @@ fn footnotes() { let pt = pt .with_look(Arc::new(look)) .with_footnotes(footnotes) - .build() .with_caption(Value::new_text("Caption").with_footnote(&f0)) .with_corner_text( Value::new_text("Corner") @@ -617,8 +620,8 @@ b. Second footnote #[test] fn no_dimension() { let pivot_table = PivotTableBuilder::new("No Dimensions", vec![]) - .with_look(Arc::new(test_look())) - .build(); + .build() + .with_look(Arc::new(test_look())); assert_rendering( &pivot_table, "No Dimensions @@ -634,15 +637,15 @@ fn empty_dimensions() { let d1 = (Axis3::X, Dimension::new(Group::new("a"))); let pivot_table = PivotTableBuilder::new("One Empty Dimension", vec![d1]) - .with_look(look.clone()) - .build(); + .build() + .with_look(look.clone()); assert_rendering(&pivot_table, "One Empty Dimension\n"); let d1 = (Axis3::X, Dimension::new(Group::new("a"))); let d2 = (Axis3::X, Dimension::new(Group::new("b").with_label_shown())); let pivot_table = PivotTableBuilder::new(Value::new_text("Two Empty Dimensions"), vec![d1, d2]) - .with_look(look.clone()) - .build(); + .build() + .with_look(look.clone()); assert_rendering(&pivot_table, "Two Empty Dimensions\n"); let d1 = (Axis3::X, Dimension::new(Group::new("a"))); @@ -652,8 +655,8 @@ fn empty_dimensions() { Dimension::new(Group::new("c").with("c1").with("c2")), ); let pivot_table = PivotTableBuilder::new("Three Dimensions, Two Empty", vec![d1, d2, d3]) - .with_look(look.clone()) - .build(); + .build() + .with_look(look.clone()); assert_rendering(&pivot_table, "Three Dimensions, Two Empty\n"); } @@ -669,7 +672,7 @@ fn empty_groups() { Dimension::new(Group::new("b").with(Group::new("b1")).with("b2").with("b3")), ); - let mut pt = PivotTableBuilder::new("Empty Groups", vec![d1, d2]); + let mut pt = PivotTableBuilder::new("Empty Groups", vec![d1, d2]).build(); let mut i = 0; for b in 0..2 { for a in 0..2 { @@ -677,9 +680,7 @@ fn empty_groups() { i += 1; } } - let pivot_table = pt - .with_look(Arc::new(test_look().with_omit_empty(false))) - .build(); + let pivot_table = pt.with_look(Arc::new(test_look().with_omit_empty(false))); assert_rendering( &pivot_table, "\ @@ -736,6 +737,7 @@ fn d4( ), ); let mut pivot_table = PivotTableBuilder::new(title, vec![a, b, c, d]) + .build() .with_look(Arc::new(test_look().with_borders(borders))); let mut i = 0; for d in 0..3 { @@ -748,7 +750,7 @@ fn d4( } } } - pivot_table.build() + pivot_table } #[test] @@ -1081,7 +1083,7 @@ fn small_numbers() { .with_label_shown(), ), ); - let mut pt = PivotTableBuilder::new("small numbers", vec![exponent, sign, rc]); + let mut pt = PivotTableBuilder::new("small numbers", vec![exponent, sign, rc]).build(); pt.insert_number(&[0, 0, 0], Some(1.0), Class::Other); pt.insert_number(&[1, 0, 0], Some(0.1), Class::Other); pt.insert_number(&[2, 0, 0], Some(0.01), Class::Other); @@ -1122,7 +1124,7 @@ fn small_numbers() { pt.insert_number(&[7, 1, 1], Some(-0.0000001), Class::Residual); pt.insert_number(&[8, 1, 1], Some(-0.00000001), Class::Residual); pt.insert_number(&[9, 1, 1], Some(-0.000000001), Class::Residual); - let pivot_table = pt.with_look(Arc::new(test_look())).build(); + let pivot_table = pt.with_look(Arc::new(test_look())); assert_rendering( &pivot_table, "\ -- 2.30.2