clean code with clippy
All checks were successful
CI/CD Pipeline / test (push) Successful in 2m30s

This commit is contained in:
philipp 2024-02-06 11:21:52 +01:00
parent c92d72c580
commit 1933b47e46
2 changed files with 15 additions and 35 deletions

View File

@ -140,6 +140,12 @@ impl PartialEq for LawBuilder {
} }
} }
impl Default for LawBuilder {
fn default() -> Self {
Self::new()
}
}
impl LawBuilder { impl LawBuilder {
/// Creates a new law builder. Adds classifier for known law texts. /// Creates a new law builder. Adds classifier for known law texts.
pub fn new() -> Self { pub fn new() -> Self {

View File

@ -12,6 +12,12 @@ pub struct Parser {
replace: Vec<(String, String)>, replace: Vec<(String, String)>,
} }
impl Default for Parser {
fn default() -> Self {
Self::new()
}
}
impl Parser { impl Parser {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
@ -33,7 +39,7 @@ impl Parser {
info!("Parsing {url}"); info!("Parsing {url}");
let xml = fetch(url)?; let xml = fetch(url)?;
let xml = xml.replace("\u{a0}", " "); let xml = xml.replace('\u{a0}', " ");
self.parse_from_str(&xml, builder) self.parse_from_str(&xml, builder)
} }
@ -84,45 +90,13 @@ fn fetch(url: &str) -> Result<String, Error> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs, sync::Arc}; use std::{fs};
use crate::{ use crate::{
config::Config, config::Config,
law::{
responsible::{contains, starts_with_number},
Classifier, LawBuilder,
},
risparser::paragraph::Parser,
}; };
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
fn test(law_id: &str, builder: &mut LawBuilder, parser: Parser) {
let paragraph_path = format!("./data/expected/overview/{law_id}");
let expected_path = format!("./data/expected/par/{law_id}");
let pars =
fs::read_to_string(paragraph_path).expect("Could not read file {paragraph_path}.");
let pars = pars.trim().split('\n').collect::<Vec<&str>>();
for par in pars {
println!("{par}");
let cont = parser.parse(par, builder).unwrap();
if !cont {
break;
}
}
let actual = &builder.history;
println!("{actual:?}");
let expected = fs::read_to_string(&expected_path)
.expect(&format!("Could not read file {expected_path}."));
let expected = expected.trim().split('\n').collect::<Vec<&str>>();
assert_eq!(actual, &expected);
}
#[test] #[test]
fn all_configs_produce_expected_output() { fn all_configs_produce_expected_output() {
let configs = fs::read_dir("./data/configs").expect("No folder with config files"); let configs = fs::read_dir("./data/configs").expect("No folder with config files");
@ -150,7 +124,7 @@ mod tests {
let actual = &builder.history; let actual = &builder.history;
let expected = fs::read_to_string(&expected_path) let expected = fs::read_to_string(&expected_path)
.expect(&format!("Could not read file {expected_path}.")); .unwrap_or_else(|_| panic!("Could not read file {expected_path}."));
let expected = expected.trim().split('\n').collect::<Vec<&str>>(); let expected = expected.trim().split('\n').collect::<Vec<&str>>();
assert_eq!(actual, &expected); assert_eq!(actual, &expected);