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) {
println!("{} {}", "#".repeat(level), header);
match &header.content {
for content in &header.content {
match content {
HeadingContent::Heading(h) => {
for child in h {
Self::print_md(child, level + 1);
}
Self::print_md(h, level + 1);
}
HeadingContent::Paragraph(p) => {
for par in p {
println!("{} {par}", "#".repeat(level + 1));
println!("{} {p}", "#".repeat(level + 1));
}
}
}
@ -96,7 +94,7 @@ impl From<LawBuilder> for Law {
pub struct Heading {
pub name: String, //1. Hauptstück; 3. Theil; ...
pub desc: Option<String>,
pub content: HeadingContent, // 1. Theil; 1. Subtheil; ...
pub content: Vec<HeadingContent>, // 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<Section>),
Heading(Vec<Heading>),
Paragraph(Section),
Heading(Heading),
}
impl From<ClassifierInstance> for HeadingContent {
fn from(value: ClassifierInstance) -> Self {
if value.sections.is_empty() {
impl From<ClassifierInstance> for Vec<HeadingContent> {
fn from(value: ClassifierInstance) -> Vec<HeadingContent> {
let mut ret = Vec::new();
for subsection in value.sections {
ret.push(HeadingContent::Paragraph(subsection));
}
for child in value.children {
ret.push(Heading {
ret.push(HeadingContent::Heading(Heading {
name: child.borrow().name.clone(),
desc: child.borrow().desc.clone(),
content: child.borrow().clone().into(),
});
}));
}
Self::Heading(ret)
} else {
Self::Paragraph(value.sections)
}
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."));
}
}
}
}