From 1933b47e4694751972a4da6657a088b5d4070133 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 6 Feb 2024 11:21:52 +0100 Subject: [PATCH] clean code with clippy --- src/law/mod.rs | 6 +++++ src/risparser/paragraph/mod.rs | 44 +++++++--------------------------- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/src/law/mod.rs b/src/law/mod.rs index 4fe41fd..4368352 100644 --- a/src/law/mod.rs +++ b/src/law/mod.rs @@ -140,6 +140,12 @@ impl PartialEq for LawBuilder { } } +impl Default for LawBuilder { + fn default() -> Self { + Self::new() + } +} + impl LawBuilder { /// Creates a new law builder. Adds classifier for known law texts. pub fn new() -> Self { diff --git a/src/risparser/paragraph/mod.rs b/src/risparser/paragraph/mod.rs index b003b5a..656651c 100644 --- a/src/risparser/paragraph/mod.rs +++ b/src/risparser/paragraph/mod.rs @@ -12,6 +12,12 @@ pub struct Parser { replace: Vec<(String, String)>, } +impl Default for Parser { + fn default() -> Self { + Self::new() + } +} + impl Parser { pub fn new() -> Self { Self { @@ -33,7 +39,7 @@ impl Parser { info!("Parsing {url}"); let xml = fetch(url)?; - let xml = xml.replace("\u{a0}", " "); + let xml = xml.replace('\u{a0}', " "); self.parse_from_str(&xml, builder) } @@ -84,45 +90,13 @@ fn fetch(url: &str) -> Result { #[cfg(test)] mod tests { - use std::{fs, sync::Arc}; + use std::{fs}; use crate::{ config::Config, - law::{ - responsible::{contains, starts_with_number}, - Classifier, LawBuilder, - }, - risparser::paragraph::Parser, }; 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::>(); - - 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::>(); - - assert_eq!(actual, &expected); - } - #[test] fn all_configs_produce_expected_output() { let configs = fs::read_dir("./data/configs").expect("No folder with config files"); @@ -150,7 +124,7 @@ mod tests { let actual = &builder.history; 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::>(); assert_eq!(actual, &expected);