cleaner code:
All checks were successful
CI/CD Pipeline / test (push) Successful in 1m51s

This commit is contained in:
2024-02-16 09:21:21 +01:00
parent ac20dfcb48
commit bb8e5641f6
4 changed files with 153 additions and 98 deletions

View File

@ -4,8 +4,9 @@ use std::iter::Peekable;
use roxmltree::{Children, Node};
use crate::law::LawBuilder;
use crate::paragraph::parser::liste::Liste;
use crate::paragraph::parser::{
Absatz, AbsatzAbs, Content, Fzinhalt, Kzinhalt, Liste, Table, Ueberschrift,
Absatz, AbsatzAbs, Content, Fzinhalt, Kzinhalt, Table, Ueberschrift,
};
#[derive(Debug, PartialEq, Default)]
@ -30,56 +31,13 @@ impl Abschnitt {
}
let mut absatze = Vec::new();
let absatz = AbsatzAbs::parse(c.next().expect("We need at least one 'Absatz'"));
let par_id = absatz
.gldsym
.clone()
.expect("First 'Absatz' needs to have § id");
// If there's a "liste" after an "absatz", the "liste" should be part of the "absatz"
if let Some(child) = c.peek() {
if Liste::test(child) {
let liste = Liste::parse(c.next().unwrap());
let mut to_add = vec![Content::Text(absatz.content), liste.get_content()];
if let Some(subchild) = c.peek() {
if Absatz::test_with_typ(subchild, "satz") {
// After a 'liste' there can be a '<absatz typ="satz"' which should be part of the list
// (e.g. 85 StGB)
to_add.push(Content::Text(Absatz::parse(c.next().unwrap()).content));
}
}
absatze.push(Content::List(to_add));
} else if Table::test(child) {
// If there's a "table" after an "absatz", the "table" should be part of the "absatz"
let table = Table::parse(c.next().unwrap());
if let Some(child) = c.peek() {
if Absatz::test_with_typ(child, "erltext") {
let after_absatz = Absatz::parse(c.next().unwrap());
absatze.push(Content::List(vec![
Content::Text(absatz.content),
Content::List(table.get_list()),
Content::Text(after_absatz.content),
]));
} else {
absatze.push(Content::List(vec![
Content::Text(absatz.content),
Content::List(table.get_list()),
]));
}
}
} else if Absatz::test_with_typ(child, "satz") {
// After a 'liste' there can be a '<absatz typ="satz"' which should be part of the list
// (e.g. 1209 ABGB)
absatze.push(Content::List(vec![
Content::Text(absatz.content.clone()),
Content::Text(Absatz::parse(c.next().unwrap()).content),
]));
} else {
absatze.push(Content::Text(absatz.content.clone()));
}
} else {
absatze.push(Content::Text(absatz.content.clone()));
}
let (par_id, first_abs) = ret.parse_absatz(&mut c);
let par_id = match par_id {
Some(par_id) => par_id,
None => panic!("First paragraph needs to have an id, not found"),
};
absatze.push(first_abs);
//There can be as many 'Absätze' as our lovely lawsetter wants
while let Some(child) = c.peek() {
@ -229,4 +187,69 @@ impl Abschnitt {
}
true
}
// Parses one logical 'Absatz'. If there's a List or Table after the Absatz, RIS assumes this
// one to be included in the paragraph
//
// # Returns
// - String: (optional) paragraph id
// - Content: content of the paragraph
fn parse_absatz(&self, c: &mut Peekable<Children>) -> (Option<String>, Content) {
let absatz = AbsatzAbs::parse(c.next().unwrap());
let par_id = absatz.gldsym;
// If there's a "liste" after an "absatz", the "liste" should be part of the "absatz"
if let Some(child) = c.peek() {
if Liste::test(child) {
println!("11@");
(
par_id,
Content::List(vec![
Content::Text(absatz.content),
Liste::parse_real(c).get_content(),
]),
)
} else if Table::test(child) {
// If there's a "table" after an "absatz", the "table" should be part of the "absatz"
let table = Table::parse(c.next().unwrap());
if let Some(child) = c.peek() {
if Absatz::test_with_typ(child, "erltext") {
let after_absatz = Absatz::parse(c.next().unwrap());
(
par_id,
Content::List(vec![
Content::Text(absatz.content),
Content::List(table.get_list()),
Content::Text(after_absatz.content),
]),
)
} else {
(
par_id,
Content::List(vec![
Content::Text(absatz.content),
Content::List(table.get_list()),
]),
)
}
} else {
(par_id, Content::Text("THIS SHOULD NOT HAPPEN".into()))
}
} else if Absatz::test_with_typ(child, "satz") {
// After a 'liste' there can be a '<absatz typ="satz"' which should be part of the list
// (e.g. 1209 ABGB)
(
par_id,
Content::List(vec![
Content::Text(absatz.content.clone()),
Content::Text(Absatz::parse(c.next().unwrap()).content),
]),
)
} else {
(par_id, Content::Text(absatz.content.clone()))
}
} else {
(par_id, Content::Text(absatz.content.clone()))
}
}
}

View File

@ -0,0 +1,78 @@
use std::iter::Peekable;
use roxmltree::{Children, Node};
use crate::{
law::Content,
paragraph::parser::{Absatz, Expect, Schlussteil, Ziffernliste},
};
#[derive(Debug)]
pub(crate) struct Liste {
content: Vec<Content>,
}
impl Liste {
pub(crate) fn test(n: &Node) -> bool {
n.tag_name().name() == "liste"
}
//TODO: rename: parse_full
pub(crate) fn parse_real(n: &mut Peekable<Children>) -> Self {
Expect::from(n.peek().unwrap()).tag("liste");
let mut content = Vec::new();
let mut c = n.next().unwrap().children().peekable();
// Parse stuff inside <liste>
while let Some(child) = c.peek() {
if Ziffernliste::test(child) {
content.push(Ziffernliste::parse(c.next().unwrap()).get_content());
} else if Schlussteil::test(child) {
// 162 Schifffahrtsgesetz show use that a 'schlussteil' can be at the start of a list
content.push(Content::Text(Schlussteil::parse(c.next().unwrap()).content));
} else {
break;
}
}
// Parse stuff after <liste>, which logically still is for the list
while let Some(child) = n.peek() {
if Absatz::test_with_typ(child, "satz") {
content.push(Content::Text(Absatz::parse(n.next().unwrap()).content));
} else {
break;
}
}
Self { content }
}
pub(crate) fn parse(n: Node) -> Self {
Expect::from(&n).tag("liste");
let mut content = Vec::new();
let mut c = n.children().peekable();
while let Some(child) = c.peek() {
if Ziffernliste::test(child) {
content.push(Ziffernliste::parse(c.next().unwrap()).get_content());
} else if Schlussteil::test(child) {
// 162 Schifffahrtsgesetz show use that a 'schlussteil' can be at the start of a list
content.push(Content::Text(Schlussteil::parse(c.next().unwrap()).content));
} else if Absatz::test_with_typ(child, "satz") {
content.push(Content::Text(Absatz::parse(c.next().unwrap()).content));
} else {
break;
}
}
assert_eq!(c.next(), None);
Self { content }
}
pub(crate) fn get_content(&self) -> Content {
Content::List(self.content.clone())
}
}

View File

@ -15,6 +15,7 @@
// limitations under the Licence.
mod abschnitt;
mod liste;
use abschnitt::Abschnitt;
use roxmltree::Node;
@ -270,53 +271,6 @@ impl Schlussteil {
}
}
#[derive(Debug)]
pub(crate) struct Liste {
content: Vec<Content>,
}
impl Liste {
pub(crate) fn test(n: &Node) -> bool {
n.tag_name().name() == "liste"
}
pub(crate) fn parse(n: Node) -> Self {
assert!(Self::test(&n));
let mut content = Vec::new();
let mut c = n.children().peekable();
// 162 Schifffahrtsgesetz show use that a 'schlussteil' can be at the start of a list
while let Some(child) = c.peek() {
if Schlussteil::test(child) {
content.push(Content::Text(Schlussteil::parse(c.next().unwrap()).content));
} else {
break;
}
}
content.push(Ziffernliste::parse(c.next().unwrap()).get_content());
while let Some(child) = c.peek() {
if Ziffernliste::test(child) {
content.push(Ziffernliste::parse(c.next().unwrap()).get_content());
} else if Schlussteil::test(child) {
content.push(Content::Text(Schlussteil::parse(c.next().unwrap()).content));
} else {
break;
}
}
assert_eq!(c.next(), None);
Self { content }
}
pub(crate) fn get_content(&self) -> Content {
Content::List(self.content.clone())
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct AbsatzAbs {
gldsym: Option<String>,