feat: parse LKR protocol file

This commit is contained in:
2024-06-11 19:30:25 +02:00
parent 66c02edccc
commit 6617ce76a0
3 changed files with 258 additions and 0 deletions

167
src/lkrexport.rs Normal file
View File

@ -0,0 +1,167 @@
/*
* This file is part of bzkf-rwdp-check
*
* Copyright (C) 2024 Comprehensive Cancer Center Mainfranken and contributors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
use std::fs;
use std::path::Path;
use std::str::FromStr;
use itertools::Itertools;
use regex::Regex;
pub struct LkrExportProtocolFile {
pub patients: Vec<Patient>,
}
impl LkrExportProtocolFile {
pub fn parse_file(path: &Path) -> Result<LkrExportProtocolFile, ()> {
let xml_file_content = fs::read_to_string(path).map_err(|_| ())?;
Self::parse(&xml_file_content)
}
pub fn parse(content: &str) -> Result<LkrExportProtocolFile, ()> {
let re = Regex::new(r"(?s)(?<patient><Patient>(.*?)</Patient>)").unwrap();
if re.is_match(content) {
let patients = re
.find_iter(content)
.map(|m| Patient {
raw_value: m.as_str().to_string(),
})
.collect_vec();
return Ok(LkrExportProtocolFile { patients });
}
Err(())
}
}
pub struct Patient {
pub raw_value: String,
}
impl Patient {
pub fn meldungen(&self) -> Vec<Meldung> {
let re = Regex::new(r"(?s)(?<meldung><Meldung(.*?)</Meldung>)").unwrap();
if re.is_match(&self.raw_value) {
return re
.find_iter(&self.raw_value)
.map(|m| Meldung {
raw_value: m.as_str().to_string(),
})
.collect_vec();
}
vec![]
}
}
pub struct Meldung {
pub raw_value: String,
}
impl FromStr for Meldung {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Meldung {
raw_value: s.to_string(),
})
}
}
impl Meldung {
pub fn id(&self) -> Option<String> {
let re = Regex::new(r#"Meldung_ID="(?<meldung_id>(.*?))""#).unwrap();
if re.is_match(&self.raw_value) {
let caps = re.captures(&self.raw_value).unwrap();
return Some(caps["meldung_id"].to_string());
}
None
}
pub fn database_id(&self) -> Option<String> {
return match self.id() {
Some(id) => {
let re1 = Regex::new(r"^(?<id>[0-9A-F]+)").unwrap();
let re2 = Regex::new(r"(?<id>[0-9]+)$").unwrap();
if re1.is_match(&id) {
match re1.find(&id).map(|m| m.as_str().to_string()) {
Some(val) => match u64::from_str_radix(&val, 16) {
Ok(val) => Some(val.to_string()),
_ => None,
},
_ => None,
}
} else if re2.is_match(&id) {
re2.find(&id).map(|m| m.as_str().to_string())
} else {
None
}
}
_ => None,
};
}
}
#[cfg(test)]
mod tests {
use crate::lkrexport::LkrExportProtocolFile;
#[test]
fn should_read_xml_file_content() {
let actual = LkrExportProtocolFile::parse(include_str!("../testdaten/testdaten_1.xml"));
assert!(actual.is_ok());
assert_eq!(actual.unwrap().patients.len(), 2);
}
#[test]
fn should_get_meldungen() {
let actual = LkrExportProtocolFile::parse(include_str!("../testdaten/testdaten_1.xml"));
assert!(actual.is_ok());
let patients = actual.unwrap().patients;
assert_eq!(patients[0].meldungen().len(), 1);
assert_eq!(patients[1].meldungen().len(), 1);
}
#[test]
fn should_get_meldung_database_id() {
let actual = LkrExportProtocolFile::parse(include_str!("../testdaten/testdaten_1.xml"));
assert!(actual.is_ok());
let patients = actual.unwrap().patients;
assert_eq!(
patients[0].meldungen()[0].database_id(),
Some("1727528".to_string())
);
assert_eq!(
patients[1].meldungen()[0].database_id(),
Some("1727824".to_string())
);
}
}

View File

@ -28,10 +28,12 @@ use itertools::Itertools;
use crate::cli::{Cli, SubCommand};
use crate::common::{Check, DiffRecord, Icd10GroupSize};
use crate::database::DatabaseSource;
use crate::lkrexport::LkrExportProtocolFile;
mod cli;
mod common;
mod database;
mod lkrexport;
mod opal;
mod resources;