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)]
enum HeadingContent {
Paragraph(Vec<Section>),
Heading(Vec<Box<Heading>>),
Heading(Vec<Heading>),
}
fn add_from_node(cur: &ClassifierInstance, builder: &LawBuilder) -> Heading {
let children = builder.get_by_parent(&cur.name);
if children.len() > 0 {
if !children.is_empty() {
let mut ret = Vec::new();
for child in children {
ret.push(Box::new(add_from_node(&child, builder)));
ret.push(add_from_node(&child, builder));
}
Heading {
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 {
match instance_name.trim().as_bytes().get(0) {
Some(c) if c.is_ascii_digit() => true,
_ => false,
}
matches!(instance_name.trim().as_bytes().first(), Some(c) if c.is_ascii_digit())
}
/// 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)
{
class.add_parent(
&self.classifiers[self.classifiers[self.last_header_index.unwrap()]
self.classifiers[self.classifiers[self.last_header_index.unwrap()]
.instances
.last()
.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)]
pub(crate) struct ClassifierInstance {
pub(crate) name: String,
@ -415,10 +391,6 @@ impl Classifier {
self.parent_index = Some(parent);
}
fn contains(&self, name: &str) -> bool {
name.contains(&self.name)
}
fn add_instance(&mut self, name: ClassifierInstance) {
self.instances.push(name);
}
@ -439,8 +411,8 @@ impl Classifier {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub(crate) enum Content {
Text(String), //This is my direct law text
Item(Vec<Box<Content>>), //(1) This is general law. (2) This is more specific law
List(Vec<Box<Content>>),
Item(Vec<Content>), //(1) This is general law. (2) This is more specific law
List(Vec<Content>),
}
#[cfg(test)]

View File

@ -2,11 +2,10 @@
mod parser;
use log::info;
use roxmltree::Node;
use serde::Deserialize;
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.
fn current_date() -> String {
@ -83,5 +82,5 @@ pub(crate) fn parse_from_str(
if !wrapper.ogd_search_result.has_next_page() {
return Ok((false, ret));
}
return Ok((true, ret));
Ok((true, ret))
}

View File

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