diff --git a/src/law/mod.rs b/src/law/mod.rs index b793195..177c169 100644 --- a/src/law/mod.rs +++ b/src/law/mod.rs @@ -1,6 +1,7 @@ /// This module contains everything everything, to convert the given JSON file into Rust structs using serde. mod parser; +use serde::Deserialize; use time::{format_description, OffsetDateTime}; use crate::{law::parser::OgdSearchResult, Error}; @@ -13,7 +14,7 @@ fn current_date() -> String { } /// Fetches the json content of the given law (`law_id`) from the RIS API. -/// +/// /// # Errors /// Fails if `ureq` can't create a connection, probably because there's no internet connection? (Or RIS is not online.) fn fetch_page(law_id: usize) -> Result { @@ -30,10 +31,20 @@ fn fetch_page(law_id: usize) -> Result { ) } +#[derive(Deserialize)] +#[serde(rename_all = "PascalCase")] +pub(crate) struct Wrapper { + ogd_search_result: OgdSearchResult, +} + pub(crate) fn parse(law_id: usize) -> Result<(), Error> { let json = fetch_page(law_id)?; - let ogd_search_result: OgdSearchResult = serde_json::from_str(&json)?; + let wrapper: Wrapper = serde_json::from_str(&json)?; + + for par in wrapper.ogd_search_result.get_par() { + crate::par::parse(&par).unwrap(); + } Ok(()) } diff --git a/src/law/parser.rs b/src/law/parser.rs index b46b148..132ce46 100644 --- a/src/law/parser.rs +++ b/src/law/parser.rs @@ -20,6 +20,24 @@ impl OgdSearchResult { fn has_next_page(&self) -> bool { todo!(); } + + pub(crate) fn get_par(&self) -> Vec { + let mut ret = Vec::new(); + for doc_ref in &self.ogd_document_results.ogd_document_reference { + for urls in &doc_ref + .data + .document_list + .content_reference + .urls + .content_url + { + if urls.data_type == "Xml" { + ret.push(urls.url.clone()); + } + } + } + ret + } } #[derive(Deserialize)] diff --git a/src/main.rs b/src/main.rs index d9fbcf2..93e00c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,6 @@ impl From for Error { } fn main() { - //law::parse(10001905); - par::parse("https://www.ris.bka.gv.at/Dokumente/Bundesnormen/NOR12025172/NOR12025172.xml"); + law::parse(10001899).unwrap(); + //par::parse("https://www.ris.bka.gv.at/Dokumente/Bundesnormen/NOR12025172/NOR12025172.xml"); } diff --git a/src/par/mod.rs b/src/par/mod.rs index a62d267..b03e2fa 100644 --- a/src/par/mod.rs +++ b/src/par/mod.rs @@ -10,5 +10,7 @@ pub(crate) fn parse(url: &str) -> Result<(), Error> { let xml = fetch_page(url)?; let risdok = Risdok::from_str(&xml)?; + println!("{risdok}"); + Ok(()) } diff --git a/src/par/parser.rs b/src/par/parser.rs index 4ec0175..defcb77 100644 --- a/src/par/parser.rs +++ b/src/par/parser.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use roxmltree::Node; use crate::Error; @@ -34,6 +36,23 @@ impl Risdok { } } +impl Display for Risdok { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if let Some(par_header) = &self.nutzdaten.abschnitt.ueberschriftPara { + f.write_str(&format!("# {}", par_header.content))?; + } + for abs in &self.nutzdaten.abschnitt.absatze { + let mut w = String::new(); + if let Some(symb) = &abs.gldsym { + w.push_str(&format!("\n{symb} ")); + } + w.push_str(&format!("{}\n", abs.content)); + f.write_str(&w)?; + } + Ok(()) + } +} + #[derive(Debug, PartialEq)] pub(crate) struct Metadaten; impl Metadaten {