category and dimension borders 1
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 11 Apr 2025 04:20:24 +0000 (21:20 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 11 Apr 2025 04:20:24 +0000 (21:20 -0700)
rust/pspp/src/output/pivot/mod.rs
rust/pspp/src/output/pivot/test.rs

index f00cdd77f69e58d13500dcc529ae63ac1ffe4b90..d629cc29e0ceb5b8b463acdd6979af90fa0ae473 100644 (file)
@@ -1007,6 +1007,8 @@ pub struct Color {
 impl Color {
     pub const BLACK: Color = Color::new(0, 0, 0);
     pub const WHITE: Color = Color::new(255, 255, 255);
+    pub const RED: Color = Color::new(255, 0, 0);
+    pub const BLUE: Color = Color::new(0, 0, 255);
     pub const TRANSPARENT: Color = Color::new(0, 0, 0).with_alpha(0);
 
     const fn new(r: u8, g: u8, b: u8) -> Self {
@@ -1413,8 +1415,8 @@ pub struct PivotTable {
 }
 
 impl PivotTable {
-    fn with_title(mut self, title: Value) -> Self {
-        self.title = Some(Box::new(title));
+    fn with_title(mut self, title: impl Into<Value>) -> Self {
+        self.title = Some(Box::new(title.into()));
         self.show_title = true;
         self
     }
@@ -1438,7 +1440,7 @@ impl PivotTable {
     fn with_layer(mut self, layer: &[usize]) -> Self {
         debug_assert_eq!(layer.len(), self.current_layer.len());
         if self.look.print_all_layers {
-            Arc::make_mut(&mut self.look).print_all_layers = false;
+            self.look_mut().print_all_layers = false;
         }
         self.current_layer.clear();
         self.current_layer.extend_from_slice(layer);
@@ -1447,10 +1449,14 @@ impl PivotTable {
 
     fn with_all_layers(mut self) -> Self {
         if !self.look.print_all_layers {
-            Arc::make_mut(&mut self.look).print_all_layers = true;
+            self.look_mut().print_all_layers = true;
         }
         self
     }
+
+    fn look_mut(&mut self) -> &mut Look {
+        Arc::make_mut(&mut self.look)
+    }
 }
 
 impl Default for PivotTable {
index 115d6daf52a91a8ba6a3473c8222cb97dfbdeb84..3e66720ee1d359c684ac1f5fa9a98d7d1a0cde24 100644 (file)
@@ -669,12 +669,7 @@ fn borders() {
         Border::Dimension(RowColBorder(HeadingRegion::Rows, Axis2::X))
         | Border::Dimension(RowColBorder(HeadingRegion::Columns, Axis2::Y)) => BorderStyle {
             stroke: Stroke::Solid,
-            color: Color {
-                alpha: 255,
-                r: 0,
-                g: 0,
-                b: 255,
-            },
+            color: Color::BLUE,
         },
         _ => BorderStyle::none(),
     });
@@ -716,6 +711,89 @@ dg1 d1     c1  0│ 1  2│ 3│ 4  5│ 6│ 7  8
       ╶─────────┼─────┼──┼─────┼──┼─────
        cg1 c2 63│64 65│66│67 68│69│70 71
            c3 72│73 74│75│76 77│78│79 80
+",
+    );
+
+    let mut pivot_table = pivot_table.with_title("Category Borders 1");
+    pivot_table.look_mut().borders = EnumMap::from_fn(|border| match border {
+        Border::Category(RowColBorder(HeadingRegion::Rows, Axis2::X))
+        | Border::Category(RowColBorder(HeadingRegion::Columns, Axis2::Y)) => BorderStyle {
+            stroke: Stroke::Dashed,
+            color: Color::RED,
+        },
+        _ => BorderStyle::none(),
+    });
+    assert_rendering(
+        &pivot_table,
+        "\
+Category Borders 1
+                           b
+                     bg1       ┊
+                 b1   ┊   b2   ┊   b3
+                  a   ┊    a   ┊    a
+                ┊ ag1 ┊  ┊ ag1 ┊  ┊ ag1
+d      c      a1┊a2┊a3┊a1┊a2┊a3┊a1┊a2┊a3
+dg1 d1     c1  0┊ 1┊ 2┊ 3┊ 4┊ 5┊ 6┊ 7┊ 8
+      ╶╌╌╌╌╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+       cg1 c2  9┊10┊11┊12┊13┊14┊15┊16┊17
+          ╶╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+           c3 18┊19┊20┊21┊22┊23┊24┊25┊26
+   ╶╌╌╌╌╌╌╌╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+    d2     c1 27┊28┊29┊30┊31┊32┊33┊34┊35
+      ╶╌╌╌╌╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+       cg1 c2 36┊37┊38┊39┊40┊41┊42┊43┊44
+          ╶╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+           c3 45┊46┊47┊48┊49┊50┊51┊52┊53
+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+    d3     c1 54┊55┊56┊57┊58┊59┊60┊61┊62
+      ╶╌╌╌╌╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+       cg1 c2 63┊64┊65┊66┊67┊68┊69┊70┊71
+          ╶╌╌╌╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌+╌╌
+           c3 72┊73┊74┊75┊76┊77┊78┊79┊80
+",
+    );
+
+    let mut pivot_table = pivot_table.with_title("Category and Dimension Borders 1");
+    pivot_table.look_mut().borders = EnumMap::from_fn(|border| match border {
+        Border::Dimension(RowColBorder(HeadingRegion::Rows, Axis2::X))
+        | Border::Dimension(RowColBorder(HeadingRegion::Columns, Axis2::Y)) => BorderStyle {
+            stroke: Stroke::Solid,
+            color: Color::BLUE,
+        },
+        Border::Category(RowColBorder(HeadingRegion::Rows, Axis2::X))
+        | Border::Category(RowColBorder(HeadingRegion::Columns, Axis2::Y)) => BorderStyle {
+            stroke: Stroke::Dashed,
+            color: Color::RED,
+        },
+        _ => BorderStyle::none(),
+    });
+    assert_rendering(
+        &pivot_table,
+        "\
+Category and Dimension Borders 1
+                           b
+                     bg1       │
+                 b1   │   b2   │   b3
+                  a   │    a   │    a
+                │ ag1 │  │ ag1 │  │ ag1
+d      c      a1│a2┊a3│a1│a2┊a3│a1│a2┊a3
+dg1 d1     c1  0│ 1┊ 2│ 3│ 4┊ 5│ 6│ 7┊ 8
+      ╶─────────┼──┼──┼──┼──┼──┼──┼──┼──
+       cg1 c2  9│10┊11│12│13┊14│15│16┊17
+          ╶╌╌╌╌╌┼╌╌+╌╌┼╌╌┼╌╌+╌╌┼╌╌┼╌╌+╌╌
+           c3 18│19┊20│21│22┊23│24│25┊26
+   ╶────────────┼──┼──┼──┼──┼──┼──┼──┼──
+    d2     c1 27│28┊29│30│31┊32│33│34┊35
+      ╶─────────┼──┼──┼──┼──┼──┼──┼──┼──
+       cg1 c2 36│37┊38│39│40┊41│42│43┊44
+          ╶╌╌╌╌╌┼╌╌+╌╌┼╌╌┼╌╌+╌╌┼╌╌┼╌╌+╌╌
+           c3 45│46┊47│48│49┊50│51│52┊53
+────────────────┼──┼──┼──┼──┼──┼──┼──┼──
+    d3     c1 54│55┊56│57│58┊59│60│61┊62
+      ╶─────────┼──┼──┼──┼──┼──┼──┼──┼──
+       cg1 c2 63│64┊65│66│67┊68│69│70┊71
+          ╶╌╌╌╌╌┼╌╌+╌╌┼╌╌┼╌╌+╌╌┼╌╌┼╌╌+╌╌
+           c3 72│73┊74│75│76┊77│78│79┊80
 ",
     );
 }