This commit is contained in:
philipp 2023-11-04 15:31:56 +01:00
parent 5a238f6521
commit 93c43d3448
4 changed files with 61 additions and 26 deletions

23
Cargo.lock generated
View File

@ -47,6 +47,12 @@ dependencies = [
"powerfmt", "powerfmt",
] ]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.28" version = "1.0.28"
@ -132,6 +138,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "pretty_assertions"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
dependencies = [
"diff",
"yansi",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.69" version = "1.0.69"
@ -168,6 +184,7 @@ dependencies = [
name = "risp" name = "risp"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"pretty_assertions",
"roxmltree", "roxmltree",
"serde", "serde",
"serde_json", "serde_json",
@ -451,3 +468,9 @@ name = "xmlparser"
version = "0.13.6" version = "0.13.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4" checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4"
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

View File

@ -11,3 +11,6 @@ time = { version = "0.3", features = [ "formatting" ] }
serde = { version = "1.0", features = [ "derive" ] } serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0" serde_json = "1.0"
roxmltree = "0.18" roxmltree = "0.18"
[dev-dependencies]
pretty_assertions = "1.4"

View File

@ -1,24 +1,29 @@
use crate::overview; use crate::overview;
pub(crate) struct Law { // pub(crate) struct Law {
name: String, //ABGB, UrhG // name: String, //ABGB, UrhG
section: Vec<Section>, // § 1, § 2, ... // section: Vec<Section>, // § 1, § 2, ...
} // }
impl Law { // impl Law {
pub(crate) fn new(name: &str) -> Self { // pub(crate) fn new(name: &str) -> Self {
Self { // Self {
name: name.into(), // name: name.into(),
section: Vec::new(), // section: Vec::new(),
} // }
} // }
} // }
/// Is used to generate a law struct. It's organized mainly by classifier.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub(crate) struct LawBuilder { pub(crate) struct LawBuilder {
/// Name of the law
name: String, //ABGB, UrhG name: String, //ABGB, UrhG
/// Structure of the law text
classifiers: Vec<Classifier>, classifiers: Vec<Classifier>,
cur_classifier_index: Option<usize>,
/// Stores the header of the next paragraph
next_para_header: Option<String>, next_para_header: Option<String>,
} }
@ -33,15 +38,19 @@ impl LawBuilder {
let mut abschnitt = Classifier::new("Abschnitt"); let mut abschnitt = Classifier::new("Abschnitt");
abschnitt.set_parent(hauptstueck); abschnitt.set_parent(hauptstueck);
classifiers.push(abschnitt); classifiers.push(abschnitt);
} else if name == "no-headers" {
let mut class = Classifier::new("");
class.add_instance(ClassifierInstance::new(""));
classifiers.push(class);
} }
Self { Self {
name: name.into(), name: name.into(),
classifiers, classifiers,
cur_classifier_index: None,
next_para_header: None, next_para_header: None,
} }
} }
/// Creates a new law builder. Adds classifier for known law texts.
pub(crate) fn new(name: &str) { pub(crate) fn new(name: &str) {
//TODO: return Law (not LawBuilder) //TODO: return Law (not LawBuilder)
let mut classifiers = Vec::new(); let mut classifiers = Vec::new();
@ -60,13 +69,13 @@ impl LawBuilder {
let mut builder = Self { let mut builder = Self {
name: name.into(), name: name.into(),
classifiers, classifiers,
cur_classifier_index: None,
next_para_header: None, next_para_header: None,
}; };
overview::parse(law_id.unwrap(), &mut builder); overview::parse(law_id.unwrap(), &mut builder);
} }
/// Sets a new header.
pub(crate) fn new_header(&mut self, name: &str) { pub(crate) fn new_header(&mut self, name: &str) {
let classifier_index = self let classifier_index = self
.classifiers .classifiers
@ -75,26 +84,28 @@ impl LawBuilder {
match classifier_index { match classifier_index {
Some(index) => { Some(index) => {
self.classifiers[index].add_instance(ClassifierInstance::new(name)); self.classifiers[index].add_instance(ClassifierInstance::new(name));
self.cur_classifier_index = Some(index);
} }
None => panic!("No classifier for {name}"), None => panic!("No classifier for {name}"),
} }
} }
/// Sets a new description for the last classifier.
pub(crate) fn new_desc(&mut self, desc: &str) { pub(crate) fn new_desc(&mut self, desc: &str) {
if let Some(index) = self.cur_classifier_index { if let Some(class) = self.classifiers.last_mut() {
self.classifiers[index].set_desc(desc); class.set_desc(desc);
} }
} }
/// Adds a new paragraph.
pub(crate) fn new_par(&mut self, par: Content) { pub(crate) fn new_par(&mut self, par: Content) {
if let Some(index) = self.cur_classifier_index { if let Some(class) = self.classifiers.last_mut() {
self.classifiers[index].add_par(par); class.add_par(par);
} else { } else {
panic!("Expected at least one classifier"); panic!("Expected at least one classifier");
} }
} }
/// Next paragraph has a header, store its name.
pub(crate) fn new_next_para_header(&mut self, header: &str) { pub(crate) fn new_next_para_header(&mut self, header: &str) {
self.next_para_header = Some(header.into()); self.next_para_header = Some(header.into());
} }
@ -198,6 +209,7 @@ pub(crate) enum Content {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use pretty_assertions::assert_eq;
#[test] #[test]
fn test() { fn test() {
@ -234,8 +246,7 @@ mod tests {
}], }],
}, },
], ],
cur_classifier_index: Some(1), next_para_header: None,
next_para_header: Some("2. Abschnitt".into()),
}; };
assert_eq!(builder, expected); assert_eq!(builder, expected);
} }

View File

@ -331,8 +331,6 @@ impl Layoutdaten {
mod tests { mod tests {
use std::{fs::File, io::Read}; use std::{fs::File, io::Read};
use crate::law::Law;
use super::*; use super::*;
#[test] #[test]
@ -341,7 +339,7 @@ mod tests {
let mut xml = String::new(); let mut xml = String::new();
file.read_to_string(&mut xml).unwrap(); file.read_to_string(&mut xml).unwrap();
let mut builder = LawBuilder::test("a"); let mut builder = LawBuilder::test("no-headers");
let risdok = Risdok::from_str(&xml, &mut builder); let risdok = Risdok::from_str(&xml, &mut builder);
if risdok.is_err() { if risdok.is_err() {
println!("{:#?}", risdok.as_ref().err()); println!("{:#?}", risdok.as_ref().err());
@ -364,7 +362,7 @@ mod tests {
let mut file = File::open("data/par/wucher7.xml").unwrap(); let mut file = File::open("data/par/wucher7.xml").unwrap();
let mut xml = String::new(); let mut xml = String::new();
file.read_to_string(&mut xml).unwrap(); file.read_to_string(&mut xml).unwrap();
let mut builder = LawBuilder::test("a"); let mut builder = LawBuilder::test("no-headers");
let risdok = Risdok::from_str(&xml, &mut builder); let risdok = Risdok::from_str(&xml, &mut builder);
if risdok.is_err() { if risdok.is_err() {