feat: add function to resolve database id

This commit is contained in:
Paul-Christian Volkmer 2024-06-12 13:40:15 +02:00
parent ac2f73d0bb
commit c5c0fcf6d4

View File

@ -105,29 +105,32 @@ impl Meldung {
None None
} }
#[allow(unused)]
pub fn database_id(&self) -> Option<String> { pub fn database_id(&self) -> Option<String> {
return match self.id() { match self.id() {
Some(id) => { Some(id) => to_database_id(&id),
_ => None,
}
}
}
pub fn to_database_id(id: &str) -> Option<String> {
let re1 = Regex::new(r"^(?<id>[0-9A-F]+)").unwrap(); let re1 = Regex::new(r"^(?<id>[0-9A-F]+)").unwrap();
let re2 = Regex::new(r"(?<id>[0-9]+)$").unwrap(); let re2 = Regex::new(r"(?<id>[0-9]+)$").unwrap();
if re1.is_match(&id) { if re1.is_match(id) {
match re1.find(&id).map(|m| m.as_str().to_string()) { match re1.find(id).map(|m| m.as_str().to_string()) {
Some(val) => match u64::from_str_radix(&val, 16) { Some(val) => match u64::from_str_radix(&val, 16) {
Ok(val) => Some(val.to_string()), Ok(val) => Some(val.to_string()),
_ => None, _ => None,
}, },
_ => None, _ => None,
} }
} else if re2.is_match(&id) { } else if re2.is_match(id) {
re2.find(&id).map(|m| m.as_str().to_string()) re2.find(id).map(|m| m.as_str().to_string())
} else { } else {
None None
} }
}
_ => None,
};
}
} }
#[cfg(test)] #[cfg(test)]