CVRF: 处理 documenttile 和 documenttype

Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
Jia Chao 2024-06-03 15:57:00 +08:00
parent 1d3908c5a8
commit 1d176558bb
2 changed files with 30 additions and 0 deletions

View File

@ -10,6 +10,9 @@ use serde::{Deserialize, Serialize};
use tracing::{debug, error, instrument, trace};
use xml::reader::{EventReader, Events, XmlEvent};
#[cfg(test)]
mod test;
struct XmlReader {
// an iterator for XmlEvent
events: EventReader<BufReader<File>>,
@ -41,6 +44,19 @@ impl XmlReader {
event
}
#[instrument(skip(self))]
pub fn next_characters(&mut self) -> String {
loop {
match self.next() {
Ok(XmlEvent::Characters(data)) => {
trace!(characters = ?data);
return data.into()
}
_ => {}
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -104,6 +120,8 @@ impl CVRF {
// 这里只处理深度为 2 的子 xml 块
match event {
Ok(XmlEvent::StartElement { ref name, .. }) => match name.local_name.as_str() {
"DocumentTitle" => self.documenttitle = xmlreader.next_characters(),
"DocumentType" => self.documenttype = xmlreader.next_characters(),
_ => {}
},
Err(e) => {

12
src/test.rs Normal file
View File

@ -0,0 +1,12 @@
use crate::*;
#[test]
fn cvrf_works() {
let mut cvrf = CVRF::new();
let _ = cvrf.load_xml("test/cvrf-openEuler-SA-2024-1488.xml");
let d_title = "An update for golang is now available for openEuler-20.03-LTS-SP1,openEuler-20.03-LTS-SP4,openEuler-22.03-LTS,openEuler-22.03-LTS-SP1,openEuler-22.03-LTS-SP2 and openEuler-22.03-LTS-SP3";
let d_type = "Security Advisory";
assert_eq!(cvrf.documenttitle, d_title);
assert_eq!(cvrf.documenttype, d_type);
}