improve: note 和 reference 使用 hashmap 存储,更方便查询

Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
Jia Chao 2024-06-12 10:59:38 +08:00
parent 524b23e0d8
commit 61a06e5ba6
2 changed files with 25 additions and 17 deletions

View File

@ -111,10 +111,10 @@ pub struct CVRF {
pub documenttracking: DocumentTracking,
// <DocumentNotes>
pub documentnotes: Vec<Note>,
pub documentnotes: HashMap<String, Note>,
// <DocumentReferences>
pub documentreferences: Vec<Reference>,
pub documentreferences: HashMap<String, Reference>,
// <ProductTree xmlns="http://www.icasi.org/CVRF/schema/prod/1.1">
pub producttree: ProductTree,
@ -132,10 +132,10 @@ impl CVRF {
documenttype: String::new(),
documentpublisher: Publisher::new(),
documenttracking: DocumentTracking::new(),
documentnotes: vec![],
documentreferences: vec![],
documentnotes: HashMap::new(),
documentreferences: HashMap::new(),
producttree: ProductTree::new(),
vulnerabilities: vec![],
vulnerabilities: Vec::new(),
}
}
@ -187,7 +187,7 @@ impl CVRF {
if xmlreader.depth < 2 {
break;
}
self.documentnotes.push(note);
self.documentnotes.insert(note.title.clone(), note);
}
}
@ -199,7 +199,7 @@ impl CVRF {
if xmlreader.depth < 2 {
break;
}
self.documentreferences.push(reference);
self.documentreferences.insert(reference.r#type.clone(), reference);
}
}
@ -736,7 +736,7 @@ impl Product {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vulnerability {
// <Notes>...</Notes>
pub notes: Vec<Note>,
pub notes: HashMap<String, Note>,
// <ReleaseDate>
pub releasedate: String,
@ -760,7 +760,7 @@ pub struct Vulnerability {
impl Vulnerability {
pub fn new() -> Self {
Vulnerability {
notes: Vec::new(),
notes: HashMap::new(),
releasedate: String::new(),
cve: String::new(),
productstatuses: Vec::new(),
@ -800,7 +800,7 @@ impl Vulnerability {
if xmlreader.depth < 3 {
break;
}
self.notes.push(note);
self.notes.insert(note.title.clone(), note);
}
}

View File

@ -42,19 +42,27 @@ fn cvrf_works() {
let note_title = "Synopsis";
let note_type = "General";
let note_ordinal = "1";
let note_content = "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 note_content = "golang security update";
assert_eq!(cvrf.documentnotes.len(), 6);
assert_eq!(cvrf.documentnotes[0].title, note_title);
assert_eq!(cvrf.documentnotes[0].r#type, note_type);
assert_eq!(cvrf.documentnotes[0].ordinal, note_ordinal);
assert_eq!(cvrf.documentnotes[1].content, note_content);
if let Some(note) = cvrf.documentnotes.get("Synopsis") {
assert_eq!(note.title, note_title);
assert_eq!(note.r#type, note_type);
assert_eq!(note.ordinal, note_ordinal);
assert_eq!(note.content, note_content);
} else {
assert!(false);
}
// references
let reference_type = "openEuler CVE";
let reference_url = "https://www.openeuler.org/en/security/cve/detail.html?id=CVE-2023-45288";
assert_eq!(cvrf.documentreferences.len(), 3);
assert_eq!(cvrf.documentreferences[1].r#type, reference_type);
assert_eq!(cvrf.documentreferences[1].url[0], reference_url);
if let Some(reference) = cvrf.documentreferences.get("openEuler CVE") {
assert_eq!(reference.r#type, reference_type);
assert_eq!(reference.url[0], reference_url);
} else {
assert!(false);
}
// producttree
let producttree_productid = "openEuler-22.03-LTS";