Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Feb 14, 2024
1 parent 2a47a61 commit 753e1fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
1 change: 0 additions & 1 deletion rust/models/src/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub struct Credential {
#[cfg_attr(feature = "serde_support", serde(flatten))]
/// Type of the credential to get access. Different services support different types.
pub credential_type: CredentialType,

}

impl Credential {
Expand Down
83 changes: 44 additions & 39 deletions rust/models/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,92 +79,97 @@ impl TryFrom<&str> for Protocol {
}

pub fn ports_to_openvas_port_list(ports: Vec<Port>) -> Option<String> {

fn add_range_to_list (list: &mut String, start: usize, end: Option<usize>) {
fn add_range_to_list(list: &mut String, start: usize, end: Option<usize>) {
// Add range
if let Some(end) = end {
list.push_str(start.to_string().as_str());
list.push('-');
list.push_str(end.to_string().as_str());
list.push(',');
// Add single port
// Add single port
} else {
list.push_str(start.to_string().as_str());
list.push(',');
}

}
if ports.is_empty() {
return None;
}

let mut udp = String::from("udp:");
let mut tcp = String::from("tcp:");

ports.iter().for_each(
|p| match p.protocol {
Some(Protocol::TCP) => {p.range.iter().for_each(|r| add_range_to_list(&mut tcp, r.start, r.end));},
Some(Protocol::UDP) => {p.range.iter().for_each(|r| add_range_to_list(&mut udp, r.start, r.end));},
None => {
p.range.iter().for_each(|r| add_range_to_list(&mut tcp, r.start, r.end));
p.range.iter().for_each(|r| add_range_to_list(&mut udp, r.start, r.end));
}
ports.iter().for_each(|p| match p.protocol {
Some(Protocol::TCP) => {
p.range
.iter()
.for_each(|r| add_range_to_list(&mut tcp, r.start, r.end));
}
Some(Protocol::UDP) => {
p.range
.iter()
.for_each(|r| add_range_to_list(&mut udp, r.start, r.end));
}
None => {
p.range
.iter()
.for_each(|r| add_range_to_list(&mut tcp, r.start, r.end));
p.range
.iter()
.for_each(|r| add_range_to_list(&mut udp, r.start, r.end));
}
);
});
if udp != *"udp:" {
tcp.push_str(&udp);
}
Some(tcp)

}

#[cfg(test)]
mod tests {

use crate::{Protocol, Port,PortRange, ports_to_openvas_port_list};
use crate::{ports_to_openvas_port_list, Port, PortRange, Protocol};

#[test]
fn test_port_conversion_to_string() {

let ports = vec![
Port{
Port {
protocol: Some(Protocol::TCP),
range: vec![
PortRange{
PortRange {
start: 22,
end: Some(25),
end: Some(25),
},
PortRange{
PortRange {
start: 80,
end: None,
end: None,
},
]
],
},
Port{
Port {
protocol: Some(Protocol::UDP),
range: vec![
PortRange{
PortRange {
start: 30,
end: Some(40),
end: Some(40),
},
PortRange{
PortRange {
start: 5060,
end: None,
end: None,
},
]
],
},
Port{
Port {
protocol: None,
range: vec![
PortRange{
start: 1000,
end: None,
},
]
range: vec![PortRange {
start: 1000,
end: None,
}],
},
];
assert_eq!(ports_to_openvas_port_list(ports), Some("tcp:22-25,80,1000,udp:30-40,5060,1000,".to_string()));

assert_eq!(
ports_to_openvas_port_list(ports),
Some("tcp:22-25,80,1000,udp:30-40,5060,1000,".to_string())
);
}
}

3 changes: 1 addition & 2 deletions rust/storage/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,8 @@ impl From<(&str, &str, &str, &str)> for NvtPreference {
}
}
}
impl From<&NvtPreference> for (String,String,String,String) {
impl From<&NvtPreference> for (String, String, String, String) {
fn from(pref: &NvtPreference) -> Self {

let id = pref.id().unwrap().to_string();
let class = match pref.class {
PreferenceType::CheckBox => "checkbox",
Expand Down

0 comments on commit 753e1fc

Please sign in to comment.