allow both paragraphs and headers in same header (displays par 252ff in abgb)
All checks were successful
CI/CD Pipeline / test (push) Successful in 1m56s

This commit is contained in:
philipp 2024-02-20 22:38:40 +01:00
parent 39f39e49d6
commit f47f7c83f2

View File

@ -58,15 +58,13 @@ impl Law {
fn print_md(header: &Heading, level: usize) { fn print_md(header: &Heading, level: usize) {
println!("{} {}", "#".repeat(level), header); println!("{} {}", "#".repeat(level), header);
match &header.content { for content in &header.content {
match content {
HeadingContent::Heading(h) => { HeadingContent::Heading(h) => {
for child in h { Self::print_md(h, level + 1);
Self::print_md(child, level + 1);
}
} }
HeadingContent::Paragraph(p) => { HeadingContent::Paragraph(p) => {
for par in p { println!("{} {p}", "#".repeat(level + 1));
println!("{} {par}", "#".repeat(level + 1));
} }
} }
} }
@ -96,7 +94,7 @@ impl From<LawBuilder> for Law {
pub struct Heading { pub struct Heading {
pub name: String, //1. Hauptstück; 3. Theil; ... pub name: String, //1. Hauptstück; 3. Theil; ...
pub desc: Option<String>, pub desc: Option<String>,
pub content: HeadingContent, // 1. Theil; 1. Subtheil; ... pub content: Vec<HeadingContent>, // 1. Theil; 1. Subtheil; ...
} }
impl Display for Heading { impl Display for Heading {
@ -111,26 +109,27 @@ impl Display for Heading {
#[derive(Debug, Serialize, Deserialize, PartialEq)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum HeadingContent { pub enum HeadingContent {
Paragraph(Vec<Section>), Paragraph(Section),
Heading(Vec<Heading>), Heading(Heading),
} }
impl From<ClassifierInstance> for HeadingContent { impl From<ClassifierInstance> for Vec<HeadingContent> {
fn from(value: ClassifierInstance) -> Self { fn from(value: ClassifierInstance) -> Vec<HeadingContent> {
if value.sections.is_empty() {
let mut ret = Vec::new(); let mut ret = Vec::new();
for subsection in value.sections {
ret.push(HeadingContent::Paragraph(subsection));
}
for child in value.children { for child in value.children {
ret.push(Heading { ret.push(HeadingContent::Heading(Heading {
name: child.borrow().name.clone(), name: child.borrow().name.clone(),
desc: child.borrow().desc.clone(), desc: child.borrow().desc.clone(),
content: child.borrow().clone().into(), content: child.borrow().clone().into(),
}); }));
} }
Self::Heading(ret) ret
} else {
Self::Paragraph(value.sections)
}
} }
} }
@ -227,6 +226,8 @@ impl LawBuilder {
.responsible_classifier(name) .responsible_classifier(name)
.unwrap_or_else(|| panic!("No classifier for '{name}'")); .unwrap_or_else(|| panic!("No classifier for '{name}'"));
debug!("Responsible_class = {responsible_class:#?}");
let mut heading: ClassifierInstance = name.into(); let mut heading: ClassifierInstance = name.into();
if let Some(last_instance) = &self.last_instance { if let Some(last_instance) = &self.last_instance {
@ -480,12 +481,33 @@ impl Display for Content {
} }
} }
// Content::Item([ #[cfg(test)]
// ContentPart::Multiple([ mod tests {
// ContentPart::Text("(1) Behörden im Sinne...sind:") use std::fs;
// ContentPart::List([
// ContentPart::Text("1.") use crate::{config::Config, law::Content};
// ContentPart::Text("2.")
// ]) 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."));
}
}
}
}