use expect struct instead of asserts
All checks were successful
CI/CD Pipeline / test (push) Successful in 4m54s

This commit is contained in:
philipp 2024-02-27 12:01:16 +01:00
parent b3b17561a8
commit 256763425b
2 changed files with 41 additions and 22 deletions

View File

@ -22,7 +22,7 @@ use roxmltree::{Children, Node};
use crate::law;
use crate::paragraph::parser::absatz::Absatz;
use crate::paragraph::parser::{AbsatzAbs, Content, Fzinhalt, Kzinhalt, Ueberschrift};
use crate::paragraph::parser::{AbsatzAbs, Content, Expect, Fzinhalt, Kzinhalt, Ueberschrift};
#[derive(Debug, PartialEq, Default)]
pub(crate) struct Abschnitt {
@ -32,7 +32,7 @@ pub(crate) struct Abschnitt {
impl Abschnitt {
pub(crate) fn parse(n: Node, builder: &mut law::Builder) -> Abschnitt {
assert!(n.tag_name().name() == "abschnitt");
Expect::from(&n).tag("abschnitt");
let mut ret = Abschnitt::default();
let mut c = n.children().peekable();

View File

@ -19,7 +19,7 @@ mod abschnitt;
mod liste;
mod table;
use std::iter::Peekable;
use std::{fmt::Display, iter::Peekable};
use abschnitt::Abschnitt;
use log::trace;
@ -42,14 +42,36 @@ impl<'a> From<&'a Node<'a, 'a>> for Expect<'a> {
}
impl<'a> Expect<'a> {
fn tag(&self, value: &str) {
fn tag(self, value: &str) -> Self {
assert!(
self.node.tag_name().name() == value,
"Expected tag '{value}', got {} (tag: {}, content: {:?})",
"Expected tag '{value}', got {} ({})",
self.node.tag_name().name(),
self.node.tag_name().name(),
self.node.text(),
self,
);
self
}
fn amount_children(self, value: usize) -> Self {
assert!(
self.node.children().count() == value,
"Expected {value} children elements for tag '{value}', got {} ({})",
self.node.children().count(),
self
);
self
}
}
impl Display for Expect<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
"tag: {}, content: {:?}",
self.node.tag_name().name(),
self.node.text()
))
.unwrap();
Ok(())
}
}
@ -58,7 +80,7 @@ pub(crate) struct Risdok {}
impl Risdok {
pub(crate) fn parse(n: Node, builder: &mut law::Builder) -> bool {
assert!(n.tag_name().name() == "risdok");
Expect::from(&n).tag("risdok");
let mut c = n.children();
@ -87,7 +109,7 @@ impl Risdok {
pub(crate) struct Metadaten;
impl Metadaten {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "metadaten");
Expect::from(&n).tag("metadaten");
assert_eq!(n.children().next(), None);
@ -99,7 +121,7 @@ impl Metadaten {
pub(crate) struct Nutzdaten {}
impl Nutzdaten {
pub(crate) fn parse(n: Node, builder: &mut law::Builder) -> bool {
assert!(n.tag_name().name() == "nutzdaten");
Expect::from(&n).tag("nutzdaten");
let mut c = n.children();
@ -118,8 +140,7 @@ pub(crate) struct Symbol {
}
impl Symbol {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "symbol");
assert_eq!(n.children().count(), 1);
Expect::from(&n).tag("symbol").amount_children(1);
let stellen = n.attribute("stellen").unwrap().into();
let content = n.text().unwrap().into();
@ -135,7 +156,7 @@ pub(crate) struct Listelem {
}
impl Listelem {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "listelem");
Expect::from(&n).tag("listelem");
let mut c = n.children();
@ -245,7 +266,7 @@ pub(crate) struct Td {
}
impl Td {
pub(crate) fn parse(n: &Node) -> Self {
assert!(n.tag_name().name() == "td");
Expect::from(n).tag("td");
let mut c = n.children();
let absatz = Absatz::parse(c.next().unwrap());
@ -262,7 +283,7 @@ pub(crate) struct Tr {
}
impl Tr {
pub(crate) fn parse(n: &Node) -> Self {
assert!(n.tag_name().name() == "tr");
Expect::from(n).tag("tr");
let mut tds = Vec::new();
@ -344,9 +365,7 @@ impl Leaf {
n.tag_name().name() == name && n.children().count() == 1
}
pub(crate) fn parse(n: Node, name: &str) -> String {
assert!(n.tag_name().name() == name);
assert_eq!(n.children().count(), 1);
Expect::from(&n).tag(name).amount_children(1);
n.text().unwrap().into()
}
@ -362,7 +381,7 @@ impl Ueberschrift {
}
pub(crate) fn parse(n: Node, typ: &str) -> Self {
assert!(n.tag_name().name() == "ueberschrift");
Expect::from(&n).tag("ueberschrift");
assert_eq!(n.attribute("typ").unwrap(), typ);
@ -377,7 +396,7 @@ impl Ueberschrift {
pub(crate) struct Kzinhalt;
impl Kzinhalt {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "kzinhalt");
Expect::from(&n).tag("kzinhalt");
//TODO parse if necessary
Self {}
}
@ -387,7 +406,7 @@ impl Kzinhalt {
pub(crate) struct Fzinhalt;
impl Fzinhalt {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "fzinhalt");
Expect::from(&n).tag("fzinhalt");
//TODO parse if necessary
Self {}
}
@ -397,7 +416,7 @@ impl Fzinhalt {
pub(crate) struct Layoutdaten;
impl Layoutdaten {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "layoutdaten");
Expect::from(&n).tag("layoutdaten");
assert_eq!(n.children().next(), None);