Skip to content

Commit

Permalink
Add: Package trait and impl the trait for package systems
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Nov 3, 2023
1 parent a7e3d77 commit f504e63
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 206 deletions.
28 changes: 5 additions & 23 deletions rust/notus/src/packages/deb.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2023 Greenbone AG
//
// SPDX-License-Identifier: GPL-2.0-or-later
use super::PackageVersion;
use super::{PackageVersion, Package};
use regex::Regex;
use std::{
cmp::Ordering,
Expand All @@ -27,25 +27,7 @@ impl Hash for Deb {
}
}

impl Deb {
fn new(
name: String,
epoch: u64,
upstream_version: String,
debian_revision: String,
full_name: String,
full_version: String,
) -> Self {
Self {
name,
full_name,
full_version,
epoch,
upstream_version,
debian_revision,
}
}

impl Package for Deb {
/// Returns a Some<Ordering> struct or None if not comparable
fn compare(&self, other: &Deb) -> Option<Ordering> {
if self.name != other.name {
Expand Down Expand Up @@ -81,7 +63,7 @@ impl Deb {
s.finish()
}

pub fn from_full_name(full_name: &str) -> Option<Self> {
fn from_full_name(full_name: &str) -> Option<Self> {
if full_name.is_empty() {
return None;
}
Expand Down Expand Up @@ -141,7 +123,7 @@ impl Deb {
})
}

pub fn from_name_and_full_version(name: &str, full_version: &str) -> Option<Self> {
fn from_name_and_full_version(name: &str, full_version: &str) -> Option<Self> {
if name.is_empty() || full_version.is_empty() {
return None;
}
Expand Down Expand Up @@ -194,7 +176,7 @@ impl Deb {
mod deb_tests {
use crate::packages;

use super::Deb;
use super::{Deb,Package};

#[test]
pub fn test_compare_gt() {
Expand Down
21 changes: 11 additions & 10 deletions rust/notus/src/packages/ebuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
hash::{Hash, Hasher},
};

use super::PackageVersion;
use super::{Package, PackageVersion};

// Supported packages types.

Expand All @@ -27,8 +27,8 @@ impl Hash for EBuild {
}

#[allow(dead_code)]
impl EBuild {
fn compare(&self, other: EBuild) -> Option<Ordering> {
impl Package for EBuild {
fn compare(&self, other: &EBuild) -> Option<Ordering> {
if self.name != other.name {
return None;
}
Expand All @@ -45,7 +45,7 @@ impl EBuild {
s.finish()
}

pub fn from_full_name(full_name: &str) -> Option<Self> {
fn from_full_name(full_name: &str) -> Option<Self> {
if full_name.is_empty() {
return None;
}
Expand Down Expand Up @@ -74,7 +74,7 @@ impl EBuild {
})
}

pub fn from_name_and_full_version(name: &str, full_version: &str) -> Option<Self> {
fn from_name_and_full_version(name: &str, full_version: &str) -> Option<Self> {
if name.is_empty() || full_version.is_empty() {
return None;
}
Expand All @@ -101,6 +101,7 @@ mod ebuild_tests {
};

use super::EBuild;
use super::Package;

#[test]
pub fn test_guard() {
Expand Down Expand Up @@ -131,14 +132,14 @@ mod ebuild_tests {
assert!(apache2
.clone()
.unwrap()
.compare(apache1.clone().unwrap())
.compare(&apache1.clone().unwrap())
.unwrap()
.is_gt());

assert!(apache1
.clone()
.unwrap()
.compare(apache2.clone().unwrap())
.compare(&apache2.clone().unwrap())
.unwrap()
.is_le());

Expand All @@ -148,18 +149,18 @@ mod ebuild_tests {
assert!(apache2
.clone()
.unwrap()
.compare(apache3.clone().unwrap())
.compare(&apache3.clone().unwrap())
.unwrap()
.is_le());
assert!(apache2
.clone()
.unwrap()
.compare(apache3.clone().unwrap())
.compare(&apache3.clone().unwrap())
.unwrap()
.is_ge());

let apache4 = EBuild::from_name_and_full_version("apache", "2.4.51-r3");
assert!(apache4.is_some());
assert!(apache4.unwrap().compare(apache3.unwrap()).unwrap().is_ne());
assert!(apache4.unwrap().compare(&apache3.unwrap()).unwrap().is_ne());
}
}
9 changes: 9 additions & 0 deletions rust/notus/src/packages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ impl<'a> PartialOrd for PackageVersion<'a> {
}
}

///
pub trait Package<Rhs=Self> {
fn compare(&self, other: &Rhs) -> Option<Ordering>;
fn from_full_name (a: &str) -> Option<Rhs>;
fn from_name_and_full_version (a: &str, b: &str) -> Option<Rhs>;
fn hash_calc(&self) -> u64;
}


#[cfg(test)]
mod tests {
use crate::packages::PackageVersion;
Expand Down
Loading

0 comments on commit f504e63

Please sign in to comment.