From f47f7c83f215654171b78231c8d7a4192b5e9288 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 20 Feb 2024 22:38:40 +0100 Subject: [PATCH] allow both paragraphs and headers in same header (displays par 252ff in abgb) --- src/law/mod.rs | 90 +++++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/src/law/mod.rs b/src/law/mod.rs index a5cc57d..5213bcb 100644 --- a/src/law/mod.rs +++ b/src/law/mod.rs @@ -58,15 +58,13 @@ impl Law { fn print_md(header: &Heading, level: usize) { println!("{} {}", "#".repeat(level), header); - match &header.content { - HeadingContent::Heading(h) => { - for child in h { - Self::print_md(child, level + 1); + for content in &header.content { + match content { + HeadingContent::Heading(h) => { + Self::print_md(h, level + 1); } - } - HeadingContent::Paragraph(p) => { - for par in p { - println!("{} {par}", "#".repeat(level + 1)); + HeadingContent::Paragraph(p) => { + println!("{} {p}", "#".repeat(level + 1)); } } } @@ -96,7 +94,7 @@ impl From for Law { pub struct Heading { pub name: String, //1. Hauptstück; 3. Theil; ... pub desc: Option, - pub content: HeadingContent, // 1. Theil; 1. Subtheil; ... + pub content: Vec, // 1. Theil; 1. Subtheil; ... } impl Display for Heading { @@ -111,26 +109,27 @@ impl Display for Heading { #[derive(Debug, Serialize, Deserialize, PartialEq)] pub enum HeadingContent { - Paragraph(Vec
), - Heading(Vec), + Paragraph(Section), + Heading(Heading), } -impl From for HeadingContent { - fn from(value: ClassifierInstance) -> Self { - if value.sections.is_empty() { - let mut ret = Vec::new(); - for child in value.children { - ret.push(Heading { - name: child.borrow().name.clone(), - desc: child.borrow().desc.clone(), - content: child.borrow().clone().into(), - }); - } +impl From for Vec { + fn from(value: ClassifierInstance) -> Vec { + let mut ret = Vec::new(); - Self::Heading(ret) - } else { - Self::Paragraph(value.sections) + for subsection in value.sections { + ret.push(HeadingContent::Paragraph(subsection)); } + + for child in value.children { + ret.push(HeadingContent::Heading(Heading { + name: child.borrow().name.clone(), + desc: child.borrow().desc.clone(), + content: child.borrow().clone().into(), + })); + } + + ret } } @@ -227,6 +226,8 @@ impl LawBuilder { .responsible_classifier(name) .unwrap_or_else(|| panic!("No classifier for '{name}'")); + debug!("Responsible_class = {responsible_class:#?}"); + let mut heading: ClassifierInstance = name.into(); if let Some(last_instance) = &self.last_instance { @@ -480,12 +481,33 @@ impl Display for Content { } } -// Content::Item([ -// ContentPart::Multiple([ -// ContentPart::Text("(1) Behörden im Sinne...sind:") -// ContentPart::List([ -// ContentPart::Text("1.") -// ContentPart::Text("2.") -// ]) -// ]) -// ]) +#[cfg(test)] +mod tests { + use std::fs; + + use crate::{config::Config, law::Content}; + + use super::Law; + + #[test] + fn allow_both_paragraphs_and_headers_in_same_header() { + let configs = fs::read_dir("./data/configs").expect("No folder with config files"); + + for config in configs { + let path = format!("{}", config.unwrap().path().display()); + if path.contains("abgb") { + let (_law_id, mut builder, _) = Config::load(&path).unwrap(); + builder.new_header("Nullter Theil. Einleitung"); + builder.new_par("§ 1.".into(), Content::Text("My test law text.".into())); + builder.new_header("Erster Hauptstück. Einleitung"); + builder.new_par("§ 252.".into(), Content::Text("My test law text.".into())); + + let law: Law = builder.into(); + + let text = format!("{law:#?}"); + + assert!(text.contains(" 252.")); + } + } + } +}