clippy :-)

This commit is contained in:
philipp 2023-11-06 14:10:08 +01:00
parent f646bd6d12
commit 9beaaeea67
3 changed files with 96 additions and 170 deletions

View File

@ -20,15 +20,15 @@ struct Heading {
#[derive(Debug, Serialize, Deserialize, PartialEq)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
enum HeadingContent { enum HeadingContent {
Paragraph(Vec<Section>), Paragraph(Vec<Section>),
Heading(Vec<Box<Heading>>), Heading(Vec<Heading>),
} }
fn add_from_node(cur: &ClassifierInstance, builder: &LawBuilder) -> Heading { fn add_from_node(cur: &ClassifierInstance, builder: &LawBuilder) -> Heading {
let children = builder.get_by_parent(&cur.name); let children = builder.get_by_parent(&cur.name);
if children.len() > 0 { if !children.is_empty() {
let mut ret = Vec::new(); let mut ret = Vec::new();
for child in children { for child in children {
ret.push(Box::new(add_from_node(&child, builder))); ret.push(add_from_node(&child, builder));
} }
Heading { Heading {
name: cur.name.clone(), name: cur.name.clone(),
@ -74,10 +74,7 @@ pub(crate) fn contains(classifier_name: &str, instance_name: &str) -> bool {
} }
fn starts_with_number(_classifier_name: &str, instance_name: &str) -> bool { fn starts_with_number(_classifier_name: &str, instance_name: &str) -> bool {
match instance_name.trim().as_bytes().get(0) { matches!(instance_name.trim().as_bytes().first(), Some(c) if c.is_ascii_digit())
Some(c) if c.is_ascii_digit() => true,
_ => false,
}
} }
/// Is used to generate a law struct. It's organized mainly by classifier. /// Is used to generate a law struct. It's organized mainly by classifier.
@ -217,7 +214,7 @@ impl LawBuilder {
.is_some_and(|x| x == 9999) .is_some_and(|x| x == 9999)
{ {
class.add_parent( class.add_parent(
&self.classifiers[self.classifiers[self.last_header_index.unwrap()] self.classifiers[self.classifiers[self.last_header_index.unwrap()]
.instances .instances
.last() .last()
.unwrap() .unwrap()
@ -321,27 +318,6 @@ pub(crate) struct Section {
// } // }
//} //}
#[derive(Clone)]
pub(crate) struct Header {
classifier: Classifier, // Hauptstück, Theil, Abschnitt, ol
name: String, // 1. Hauptstück, 3. Theil, 7. Abschnitt, li
parent: Option<Box<Header>>,
}
impl Header {
fn get_all_classifiers(&self) -> Vec<Classifier> {
let mut ret = Vec::new();
ret.push(self.classifier.clone());
if let Some(parent) = &self.parent {
for a in parent.get_all_classifiers() {
ret.push(a);
}
}
ret
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub(crate) struct ClassifierInstance { pub(crate) struct ClassifierInstance {
pub(crate) name: String, pub(crate) name: String,
@ -415,10 +391,6 @@ impl Classifier {
self.parent_index = Some(parent); self.parent_index = Some(parent);
} }
fn contains(&self, name: &str) -> bool {
name.contains(&self.name)
}
fn add_instance(&mut self, name: ClassifierInstance) { fn add_instance(&mut self, name: ClassifierInstance) {
self.instances.push(name); self.instances.push(name);
} }
@ -439,8 +411,8 @@ impl Classifier {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub(crate) enum Content { pub(crate) enum Content {
Text(String), //This is my direct law text Text(String), //This is my direct law text
Item(Vec<Box<Content>>), //(1) This is general law. (2) This is more specific law Item(Vec<Content>), //(1) This is general law. (2) This is more specific law
List(Vec<Box<Content>>), List(Vec<Content>),
} }
#[cfg(test)] #[cfg(test)]

View File

@ -2,11 +2,10 @@
mod parser; mod parser;
use log::info; use log::info;
use roxmltree::Node;
use serde::Deserialize; use serde::Deserialize;
use time::{format_description, OffsetDateTime}; use time::{format_description, OffsetDateTime};
use crate::{law::LawBuilder, overview::parser::OgdSearchResult, Error}; use crate::{overview::parser::OgdSearchResult, Error};
/// Returns the current date in YYYY-MM-DD format. Needed for RIS API query to get current version of the overview. /// Returns the current date in YYYY-MM-DD format. Needed for RIS API query to get current version of the overview.
fn current_date() -> String { fn current_date() -> String {
@ -83,5 +82,5 @@ pub(crate) fn parse_from_str(
if !wrapper.ogd_search_result.has_next_page() { if !wrapper.ogd_search_result.has_next_page() {
return Ok((false, ret)); return Ok((false, ret));
} }
return Ok((true, ret)); Ok((true, ret))
} }

View File

@ -6,10 +6,7 @@ use crate::{
}; };
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub(crate) struct Risdok { pub(crate) struct Risdok {}
metadaten: Metadaten,
layoutdaten: Layoutdaten,
}
impl Risdok { impl Risdok {
pub(crate) fn parse(n: Node, builder: &mut LawBuilder) -> bool { pub(crate) fn parse(n: Node, builder: &mut LawBuilder) -> bool {
@ -17,12 +14,12 @@ impl Risdok {
let mut c = n.children(); let mut c = n.children();
let metadaten = Metadaten::parse(c.next().unwrap()); Metadaten::parse(c.next().unwrap());
let nutzdaten = Nutzdaten::parse(c.next().unwrap(), builder); let nutzdaten = Nutzdaten::parse(c.next().unwrap(), builder);
if !nutzdaten { if !nutzdaten {
return false; return false;
} }
let layoutdaten = Layoutdaten::parse(c.next().unwrap()); Layoutdaten::parse(c.next().unwrap());
assert_eq!(c.next(), None); assert_eq!(c.next(), None);
@ -30,11 +27,10 @@ impl Risdok {
} }
pub(crate) fn from_str(xml: &str, builder: &mut LawBuilder) -> Result<bool, Error> { pub(crate) fn from_str(xml: &str, builder: &mut LawBuilder) -> Result<bool, Error> {
let doc = roxmltree::Document::parse(&xml)?; let doc = roxmltree::Document::parse(xml)?;
let root = doc.root(); let root = doc.root();
assert_eq!(root.children().into_iter().count(), 1); assert_eq!(root.children().count(), 1);
let continue_parsing = Self::parse(root.children().next().unwrap(), builder); Ok(Self::parse(root.children().next().unwrap(), builder))
Ok(continue_parsing)
} }
} }
@ -80,9 +76,7 @@ impl Abschnitt {
Fzinhalt::parse(c.next().unwrap()); Fzinhalt::parse(c.next().unwrap());
// Skip all UeberschriftTitle and Absatz // Skip all UeberschriftTitle and Absatz
loop { while let Some(child) = c.peek() {
match c.peek() {
Some(child) => {
if Ueberschrift::test(child, "titel") { if Ueberschrift::test(child, "titel") {
c.next(); c.next();
continue; continue;
@ -93,32 +87,24 @@ impl Abschnitt {
} }
break; break;
} }
None => break,
}
}
loop { while let Some(child) = c.peek() {
match c.peek() { if Ueberschrift::test(child, "g1") {
Some(child) => {
if Ueberschrift::test(&child, "g1") {
let ueberschrift = Ueberschrift::parse(c.next().unwrap(), "g1"); let ueberschrift = Ueberschrift::parse(c.next().unwrap(), "g1");
if ueberschrift.content.trim().starts_with("Artikel") { if ueberschrift.content.trim().starts_with("Artikel") {
return false; return false;
} }
builder.new_header(&ueberschrift.content); builder.new_header(&ueberschrift.content);
} else if Ueberschrift::test(&child, "g2") { } else if Ueberschrift::test(child, "g2") {
let ueberschrift = Ueberschrift::parse(c.next().unwrap(), "g2"); let ueberschrift = Ueberschrift::parse(c.next().unwrap(), "g2");
builder.new_desc(&ueberschrift.content); builder.new_desc(&ueberschrift.content);
} else if Ueberschrift::test(&child, "g1min") { } else if Ueberschrift::test(child, "g1min") {
let ueberschrift = Ueberschrift::parse(c.next().unwrap(), "g1min"); let ueberschrift = Ueberschrift::parse(c.next().unwrap(), "g1min");
builder.new_header(&ueberschrift.content); builder.new_header(&ueberschrift.content);
} else { } else {
break; break;
} }
} }
None => break,
}
}
if let Some(child) = c.peek() { if let Some(child) = c.peek() {
if Ueberschrift::test(child, "para") { if Ueberschrift::test(child, "para") {
@ -145,8 +131,8 @@ impl Abschnitt {
if Liste::test(child) { if Liste::test(child) {
let liste = Liste::parse(c.next().unwrap()); let liste = Liste::parse(c.next().unwrap());
absatze.push(Content::List(vec![ absatze.push(Content::List(vec![
Content::Text(absatz.content.replace("\u{a0}", " ")).into(), Content::Text(absatz.content.replace('\u{a0}', " ")),
liste.get_content().into(), liste.get_content(),
])); ]));
} else if Table::test(child) { } else if Table::test(child) {
// If there's a "table" after an "absatz", the "table" should be part of the "absatz" // If there's a "table" after an "absatz", the "table" should be part of the "absatz"
@ -155,69 +141,62 @@ impl Abschnitt {
if Absatz::test_with_typ(child, "erltext") { if Absatz::test_with_typ(child, "erltext") {
let after_absatz = Absatz::parse(c.next().unwrap()); let after_absatz = Absatz::parse(c.next().unwrap());
absatze.push(Content::List(vec![ absatze.push(Content::List(vec![
Content::Text(absatz.content.replace("\u{a0}", " ")).into(), Content::Text(absatz.content.replace('\u{a0}', " ")),
Content::List(table.get_list()).into(), Content::List(table.get_list()),
Content::Text(after_absatz.content).into(), Content::Text(after_absatz.content),
])) ]))
} else { } else {
absatze.push(Content::List(vec![ absatze.push(Content::List(vec![
Content::Text(absatz.content.replace("\u{a0}", " ")).into(), Content::Text(absatz.content.replace('\u{a0}', " ")),
Content::List(table.get_list()).into(), Content::List(table.get_list()),
])); ]));
} }
} }
} else { } else {
absatze.push(Content::Text(absatz.content.replace("\u{a0}", " ").clone())); absatze.push(Content::Text(absatz.content.replace('\u{a0}', " ").clone()));
} }
} else { } else {
absatze.push(Content::Text(absatz.content.replace("\u{a0}", " ").clone())); absatze.push(Content::Text(absatz.content.replace('\u{a0}', " ").clone()));
} }
//TODO: Continue here, (2) and (3) is somehow skipped //TODO: Continue here, (2) and (3) is somehow skipped
//There can be as many 'Absätze' as our lovely lawsetter wants //There can be as many 'Absätze' as our lovely lawsetter wants
loop { while let Some(child) = c.peek() {
match c.peek() {
Some(child) => {
if AbsatzAbs::test(child) { if AbsatzAbs::test(child) {
let abs = AbsatzAbs::parse(c.next().unwrap()); let abs = AbsatzAbs::parse(c.next().unwrap());
// If there's a "liste" after an "absatz", the "liste" should be part of the "absatz" // If there's a "liste" after an "absatz", the "liste" should be part of the "absatz"
if let Some(child) = c.peek() { if let Some(child) = c.peek() {
if Liste::test(&child) { if Liste::test(child) {
let liste = Liste::parse(c.next().unwrap()); let liste = Liste::parse(c.next().unwrap());
absatze.push(Content::List(vec![ absatze.push(Content::List(vec![
Content::Text(abs.content.replace("\u{a0}", " ")).into(), Content::Text(abs.content.replace('\u{a0}', " ")),
liste.get_content().into(), liste.get_content(),
])); ]));
} else { } else {
absatze.push(Content::Text(abs.content.replace("\u{a0}", " "))); absatze.push(Content::Text(abs.content.replace('\u{a0}', " ")));
} }
} else { } else {
absatze.push(Content::Text(abs.content.replace("\u{a0}", " "))); absatze.push(Content::Text(abs.content.replace('\u{a0}', " ")));
} }
continue; continue;
} }
break; break;
} }
None => break,
}
}
if absatze.len() == 1 { if absatze.len() == 1 {
builder.new_par(par_id, absatze[0].clone()); builder.new_par(par_id, absatze[0].clone());
} else { } else {
let mut contents = Vec::new(); let mut contents = Vec::new();
for a in &absatze { for a in &absatze {
contents.push(Box::new(a.clone())); contents.push(a.clone());
} }
builder.new_par(par_id, Content::Item(contents)); builder.new_par(par_id, Content::Item(contents));
} }
// Skip all UeberschriftTitle and Absatz // Skip all UeberschriftTitle and Absatz
loop { while let Some(child) = c.peek() {
match c.peek() {
Some(child) => {
if Ueberschrift::test(child, "titel") { if Ueberschrift::test(child, "titel") {
c.next(); c.next();
continue; continue;
@ -228,9 +207,6 @@ impl Abschnitt {
} }
break; break;
} }
None => break,
}
}
assert_eq!(c.next(), None); assert_eq!(c.next(), None);
@ -261,10 +237,6 @@ pub(crate) struct Listelem {
text: String, text: String,
} }
impl Listelem { impl Listelem {
pub(crate) fn test(n: &Node) -> bool {
n.tag_name().name() == "listelem"
}
pub(crate) fn parse(n: Node) -> Self { pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "listelem"); assert!(n.tag_name().name() == "listelem");
@ -308,12 +280,9 @@ impl Ziffernliste {
let mut elems = Vec::new(); let mut elems = Vec::new();
for elem in &self.listelems { for elem in &self.listelems {
elems.push( elems.push(Content::Text(
Content::Text( format!("{} {}", elem.symbol.content, elem.text).replace('\u{a0}', " "),
format!("{} {}", elem.symbol.content, elem.text).replace("\u{a0}", " "), ));
)
.into(),
);
} }
Content::List(elems) Content::List(elems)
@ -347,8 +316,7 @@ impl Tr {
let mut tds = Vec::new(); let mut tds = Vec::new();
let mut c = n.children(); for child in n.children() {
for child in c {
tds.push(Td::parse(&child)); tds.push(Td::parse(&child));
} }
@ -368,15 +336,14 @@ impl Table {
assert!(Self::test(&n)); assert!(Self::test(&n));
let mut trs = Vec::new(); let mut trs = Vec::new();
let mut c = n.children(); for child in n.children() {
for child in c {
trs.push(Tr::parse(&child)); trs.push(Tr::parse(&child));
} }
Self { trs } Self { trs }
} }
pub(crate) fn get_list(&self) -> Vec<Box<Content>> { pub(crate) fn get_list(&self) -> Vec<Content> {
let mut ret = Vec::new(); let mut ret = Vec::new();
for tr in &self.trs { for tr in &self.trs {
@ -385,9 +352,7 @@ impl Table {
txt.push_str(&format!("{} ", td.absatz.content)); txt.push_str(&format!("{} ", td.absatz.content));
} }
ret.push(Box::new(Content::Text( ret.push(Content::Text(format!("- {txt}",).replace('\u{a0}', " ")));
format!("- {txt}",).replace("\u{a0}", " "),
)));
} }
ret ret
@ -429,24 +394,20 @@ impl Liste {
let mut c = n.children().peekable(); let mut c = n.children().peekable();
content.push(Ziffernliste::parse(c.next().unwrap()).get_content().into()); content.push(Ziffernliste::parse(c.next().unwrap()).get_content());
loop { while let Some(child) = c.peek() {
if let Some(child) = c.peek() {
if Ziffernliste::test(child) { if Ziffernliste::test(child) {
content.push(Ziffernliste::parse(c.next().unwrap()).get_content().into()); content.push(Ziffernliste::parse(c.next().unwrap()).get_content());
} else if Schlussteil::test(child) { } else if Schlussteil::test(child) {
content.push(Content::Text( content.push(Content::Text(
Schlussteil::parse(c.next().unwrap()) Schlussteil::parse(c.next().unwrap())
.content .content
.replace("\u{a0}", " "), .replace('\u{a0}', " "),
)); ));
} else { } else {
break; break;
} }
} else {
break;
}
} }
assert_eq!(c.next(), None); assert_eq!(c.next(), None);
@ -455,13 +416,7 @@ impl Liste {
} }
pub(crate) fn get_content(&self) -> Content { pub(crate) fn get_content(&self) -> Content {
Content::List( Content::List(self.content.clone())
self.content
.clone()
.into_iter()
.map(|c| Box::new(c))
.collect(),
)
} }
} }
@ -483,7 +438,7 @@ impl AbsatzAbs {
let gldsym = match c.peek() { let gldsym = match c.peek() {
Some(child) => { Some(child) => {
if Leaf::test(child, "gldsym".into()) { if Leaf::test(child, "gldsym".into()) {
Some(Leaf::parse(c.next().unwrap(), "gldsym".into()).replace("\u{a0}", " ")) Some(Leaf::parse(c.next().unwrap(), "gldsym".into()).replace('\u{a0}', " "))
} else { } else {
None None
} }
@ -508,12 +463,12 @@ pub(crate) struct Leaf {
} }
impl Leaf { impl Leaf {
pub(crate) fn test(n: &Node, name: String) -> bool { pub(crate) fn test(n: &Node, name: String) -> bool {
n.tag_name().name() == name && n.children().into_iter().count() == 1 n.tag_name().name() == name && n.children().count() == 1
} }
pub(crate) fn parse(n: Node, name: String) -> String { pub(crate) fn parse(n: Node, name: String) -> String {
assert!(n.tag_name().name() == name); assert!(n.tag_name().name() == name);
assert_eq!(n.children().into_iter().count(), 1); assert_eq!(n.children().count(), 1);
n.text().unwrap().into() n.text().unwrap().into()
} }