cleanup
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 4 Jan 2026 00:08:52 +0000 (16:08 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 4 Jan 2026 00:08:52 +0000 (16:08 -0800)
rust/pspp/src/spv/read/legacy_xml.rs

index 4150ab8e21a4e7ccb4ee3820d931e8772d2c27a2..f669223ca0907f26e77b356b4f2d66f414407989 100644 (file)
@@ -167,6 +167,42 @@ pub struct Visualization {
 }
 
 impl Visualization {
+    fn decode_labels(&self, graph: &Graph) -> (Option<Value>, Option<Value>, pivot::Footnotes) {
+        let mut labels = EnumMap::from_fn(|_| Vec::new());
+        for child in &self.children {
+            match child {
+                VisChild::LabelFrame(label_frame) => {
+                    if let Some(label) = &label_frame.label
+                        && let Some(purpose) = label.purpose
+                    {
+                        labels[purpose].push(label);
+                    }
+                }
+                VisChild::Container(c) => {
+                    for label_frame in &c.label_frames {
+                        if let Some(label) = &label_frame.label
+                            && let Some(purpose) = label.purpose
+                        {
+                            labels[purpose].push(label);
+                        }
+                    }
+                }
+                _ => (),
+            }
+        }
+        let caption_labels = if !labels[Purpose::SubTitle].is_empty() {
+            &labels[Purpose::SubTitle]
+        } else {
+            &labels[Purpose::Footnote]
+        };
+
+        let footnotes = self.decode_footnotes(graph, &labels[Purpose::Footnote]);
+        (
+            LabelFrame::decode_label(&labels[Purpose::Title], &footnotes),
+            LabelFrame::decode_label(caption_labels, &footnotes),
+            footnotes,
+        )
+    }
     fn decode_date(&self) -> Result<NaiveDate, LegacyXmlWarning> {
         NaiveDate::parse_from_str(&self.date, "%Y-%m-%d")
             .map_err(|_| LegacyXmlWarning::InvalidCreationDate(self.date.clone()))
@@ -569,52 +605,24 @@ impl Visualization {
         }
     }
 
+    pub fn graph(&self) -> Result<&Graph, super::Error> {
+        for child in &self.children {
+            match child {
+                VisChild::Graph(g) => return Ok(g),
+                _ => (),
+            }
+        }
+        Err(super::Error::LegacyMissingGraph)
+    }
+
     pub fn decode(
         &self,
         binary_data: IndexMap<String, IndexMap<String, Vec<Datum<String>>>>,
         look: Look,
         warn: &mut dyn FnMut(LegacyXmlWarning),
     ) -> Result<PivotTable, super::Error> {
-        let mut graph = None;
-        let mut labels = EnumMap::from_fn(|_| Vec::new());
-        for child in &self.children {
-            match child {
-                VisChild::Graph(g) => graph = Some(g),
-                VisChild::LabelFrame(label_frame) => {
-                    if let Some(label) = &label_frame.label
-                        && let Some(purpose) = label.purpose
-                    {
-                        labels[purpose].push(label);
-                    }
-                }
-                VisChild::Container(c) => {
-                    for label_frame in &c.label_frames {
-                        if let Some(label) = &label_frame.label
-                            && let Some(purpose) = label.purpose
-                        {
-                            labels[purpose].push(label);
-                        }
-                    }
-                }
-                VisChild::Style(_)
-                | VisChild::SourceVariable(_)
-                | VisChild::DerivedVariable(_)
-                | VisChild::Other => (),
-            }
-        }
-        let Some(graph) = graph else {
-            return Err(super::Error::LegacyMissingGraph);
-        };
-
-        let footnotes = self.decode_footnotes(graph, &labels[Purpose::Footnote]);
-
-        let title = LabelFrame::decode_label(&labels[Purpose::Title], &footnotes);
-        let caption_labels = if !labels[Purpose::SubTitle].is_empty() {
-            &labels[Purpose::SubTitle]
-        } else {
-            &labels[Purpose::Footnote]
-        };
-        let caption = LabelFrame::decode_label(caption_labels, &footnotes);
+        let graph = self.graph()?;
+        let (title, caption, footnotes) = self.decode_labels(graph);
 
         let series = self.decode_series(binary_data, warn);
         let (mut dims, current_layer) = self.decode_dimensions(graph, &series, &footnotes);