diff --git a/src/law/mod.rs b/src/law/mod.rs index 5213bcb..9d3dbab 100644 --- a/src/law/mod.rs +++ b/src/law/mod.rs @@ -165,7 +165,7 @@ impl PartialEq for LawBuilder { impl Default for LawBuilder { fn default() -> Self { - Self::new("".into()) + Self::new(String::new()) } } @@ -347,7 +347,7 @@ impl fmt::Display for Section { format!("{}\n{}", self.symb, self.content) }; if let Some(note) = &self.par_note { - to_write.push_str(&format!("\nBeachte: {}\n", note)); + to_write.push_str(&format!("\nBeachte: {note}\n")); } f.write_str(&to_write) diff --git a/src/overview/mod.rs b/src/overview/mod.rs index 7cb40a9..384d535 100644 --- a/src/overview/mod.rs +++ b/src/overview/mod.rs @@ -104,28 +104,26 @@ fn fetch_page(overview_id: usize, page: usize) -> Result { use std::fs; let expected_filename = format!("{}law-{overview_id}-{page}", get_cache_dir()?); - - match fs::read_to_string(&expected_filename) { - Ok(data) => Ok(data), - Err(_) => { - info!("Not finding law_id {overview_id} (page {page}) in the cache, downloading..."); - let data = ureq::post("https://data.bka.gv.at/ris/api/v2.6/Bundesrecht") - .send_form(&[ - ("Applikation", "BrKons"), - ("Gesetzesnummer", &format!("{overview_id}")), - ("DokumenteProSeite", "OneHundred"), - ("Seitennummer", &format!("{page}")), - ("Fassung.FassungVom", ¤t_date()), - ])? - .into_string()?; - let path = Path::new(&expected_filename); - if let Some(parent) = path.parent() { - // Try to create the directory (and any necessary parent directories) - fs::create_dir_all(parent).expect("Unable to create directory"); - } - fs::write(expected_filename, &data).expect("Unable to write file"); - Ok(data) + if let Ok(data) = fs::read_to_string(&expected_filename) { + Ok(data) + } else { + info!("Not finding law_id {overview_id} (page {page}) in the cache, downloading..."); + let data = ureq::post("https://data.bka.gv.at/ris/api/v2.6/Bundesrecht") + .send_form(&[ + ("Applikation", "BrKons"), + ("Gesetzesnummer", &format!("{overview_id}")), + ("DokumenteProSeite", "OneHundred"), + ("Seitennummer", &format!("{page}")), + ("Fassung.FassungVom", ¤t_date()), + ])? + .into_string()?; + let path = Path::new(&expected_filename); + if let Some(parent) = path.parent() { + // Try to create the directory (and any necessary parent directories) + fs::create_dir_all(parent).expect("Unable to create directory"); } + fs::write(expected_filename, &data).expect("Unable to write file"); + Ok(data) } } diff --git a/src/paragraph/mod.rs b/src/paragraph/mod.rs index 1d39de3..8499a7d 100644 --- a/src/paragraph/mod.rs +++ b/src/paragraph/mod.rs @@ -25,7 +25,7 @@ use std::{ path::Path, }; -use log::{info}; +use log::info; use crate::{ law::LawBuilder, @@ -87,7 +87,7 @@ impl Parser { } let xml = if self.move_para_headers_into_content { - Self::do_move_para_headers_into_content(xml) + Self::do_move_para_headers_into_content(&xml) } else { xml }; @@ -95,8 +95,8 @@ impl Parser { Risdok::from_str(&xml, builder) } - fn do_move_para_headers_into_content(xml: String) -> String { - let mut result = String::from(&xml); + fn do_move_para_headers_into_content(xml: &str) -> String { + let mut result = String::from(xml); let ueberschrift_regex = Regex::new( "(§.*?)", ) @@ -116,7 +116,7 @@ impl Parser { // Insert the tag with the ueberschrift content into the result string result.insert_str( insert_point, - &format!("{}", ueberschrift_content), + &format!("{ueberschrift_content}"), ); } @@ -133,20 +133,18 @@ fn fetch(url: &str) -> Result { let hash = format!("{:x}", hasher.finish()); let expected_filename = format!("{}par-{hash}", get_cache_dir()?); - - match fs::read_to_string(&expected_filename) { - Ok(data) => Ok(data), - Err(_) => { - info!("Not finding url {url} in the cache, downloading..."); - let data = fetch_with_retries(url)?; - let path = Path::new(&expected_filename); - if let Some(parent) = path.parent() { - // Try to create the directory (and any necessary parent directories) - fs::create_dir_all(parent).expect("Unable to create directory"); - } - fs::write(expected_filename, &data).expect("Unable to write file"); - Ok(data) + if let Ok(data) = fs::read_to_string(&expected_filename) { + Ok(data) + } else { + info!("Not finding url {url} in the cache, downloading..."); + let data = fetch_with_retries(url)?; + let path = Path::new(&expected_filename); + if let Some(parent) = path.parent() { + // Try to create the directory (and any necessary parent directories) + fs::create_dir_all(parent).expect("Unable to create directory"); } + fs::write(expected_filename, &data).expect("Unable to write file"); + Ok(data) } } diff --git a/src/paragraph/parser/absatz.rs b/src/paragraph/parser/absatz.rs index 322b245..7cd19da 100644 --- a/src/paragraph/parser/absatz.rs +++ b/src/paragraph/parser/absatz.rs @@ -62,7 +62,7 @@ impl Absatz { { // After a 'absatz' there can be a ' par_id, - None => panic!("First paragraph needs to have an id, not found"), + let Some(par_id) = par_id else { + panic!("First paragraph needs to have an id, not found") }; absatze.push(first_abs); @@ -118,9 +117,10 @@ impl Abschnitt { } } value = value.trim().into(); - if value.is_empty() { - panic!("Expected at least on erltext-absatz after title meta-data"); - } + assert!( + !value.is_empty(), + "Expected at least on erltext-absatz after title meta-data" + ); // We want ot use this information in our markdown output. // TODO: Use all metadata, instead of this specific call @@ -146,7 +146,7 @@ impl Abschnitt { // we have optionally headers. Such as "Einleitung", "Von den bürgerlichen Gesetzen üerhaupt," // etc. If we have headers which indicate that we are done and we want to stop parsing // ("anlage" + "Artikel" we indicate this wish by returning false. - fn handle_headers(&self, c: &mut Peekable, builder: &mut LawBuilder) -> bool { + fn handle_headers(c: &mut Peekable, builder: &mut LawBuilder) -> bool { while let Some(child) = c.peek() { // Schiffahrtsgesetz: stop @ anlagen (for now) if Ueberschrift::test(child, "anlage") { diff --git a/src/paragraph/parser/mod.rs b/src/paragraph/parser/mod.rs index 4bfb830..319a0de 100644 --- a/src/paragraph/parser/mod.rs +++ b/src/paragraph/parser/mod.rs @@ -43,14 +43,13 @@ impl<'a> From<&'a Node<'a, 'a>> for Expect<'a> { impl<'a> Expect<'a> { fn tag(&self, value: &str) { - if self.node.tag_name().name() != value { - panic!( - "Expected tag '{value}', got {} (tag: {}, content: {:?})", - self.node.tag_name().name(), - self.node.tag_name().name(), - self.node.text(), - ); - } + assert!( + !(self.node.tag_name().name() != value), + "Expected tag '{value}', got {} (tag: {}, content: {:?})", + self.node.tag_name().name(), + self.node.tag_name().name(), + self.node.text(), + ); } }