Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Rust 2024 #1835

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust/src/feed/oid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
pub fn init(
loader: L,
verifier: V,
) -> impl Iterator<Item = Result<(String, String), update::Error>> {
) -> impl Iterator<Item = Result<(String, String), update::Error>> + use<L, V> {
Self { loader, verifier }
}

Expand Down
8 changes: 4 additions & 4 deletions rust/src/feed/transpile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl FunctionNameMatcher<'_> {
StatementKind::Exit(..) => self.name.map(|x| x == "exit").unwrap_or(true),
StatementKind::Include(..) => self.name.map(|x| x == "include").unwrap_or(true),
StatementKind::Call(..) => {
if let crate::nasl::syntax::TokenCategory::Identifier(
if let &crate::nasl::syntax::TokenCategory::Identifier(
crate::nasl::syntax::IdentifierType::Undefined(ref x),
) = s.start().category()
{
Expand All @@ -176,7 +176,7 @@ impl FunctionNameMatcher<'_> {
}
}
StatementKind::FunctionDeclaration(id, ..) => {
if let crate::nasl::syntax::TokenCategory::Identifier(
if let &crate::nasl::syntax::TokenCategory::Identifier(
crate::nasl::syntax::IdentifierType::Undefined(ref x),
) = id.category()
{
Expand Down Expand Up @@ -764,7 +764,7 @@ mod functions {
use super::*;

macro_rules! parameter_check {
($name:expr, $code:expr, $params:expr, $expected:expr) => {{
($name:expr_2021, $code:expr_2021, $params:expr_2021, $expected:expr_2021) => {{
let name = $name.to_string();
let replaces = [ReplaceCommand {
find: Find::FunctionByName(name),
Expand All @@ -774,7 +774,7 @@ mod functions {

assert_eq!(&result, $expected);
}};
($code:expr, $params:expr, $expected:expr) => {{
($code:expr_2021, $params:expr_2021, $expected:expr_2021) => {{
if let Some((name, _)) = $code.rsplit_once("(") {
let name = name.replace("function ", "");
parameter_check!(name, $code, $params, $expected)
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod tests {
use crate::nasl::test_prelude::*;

macro_rules! make_dict {
($($key:expr => $val:expr),*) => {
($($key:expr_2021 => $val:expr_2021),*) => {
{
#[allow(unused_mut)]
let mut result: HashMap<String, NaslValue> = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/cryptographic/aes_ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn aes256_ccm_decrypt_auth(register: &Register) -> Result<NaslValue, FnError> {
}

macro_rules! ccm_call_typed {
($(($t1s: expr, $t1: ty) => $(($t2s: expr, $t2: ty)),*);*) => {
($(($t1s: expr_2021, $t1: ty) => $(($t2s: expr_2021, $t2: ty)),*);*) => {
fn ccm_typed<D>(tag_size: usize, iv_size: usize, crypt: Crypt, key: &[u8], nonce: &[u8], data: &[u8], aad: &[u8]) -> Result<Result<Vec<u8>, aError>, FnError>
where D: BlockCipher + BlockSizeUser<BlockSize = U16> + BlockEncrypt + BlockDecrypt + KeyInit
{
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/description/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::nasl::utils::get_named_parameter;
/// Followed by required named parameter separated by `:` `(field1: field2)`.
/// The third group indicated by `(?field1: field2)` are optional named parameter.
macro_rules! make_storage_function {
($($name:ident $transform:expr => $([$len:expr])? $(($($value:ident):+))? $(?($($optional_value:ident):+))?),+) => {
($($name:ident $transform:expr_2021 => $([$len:expr_2021])? $(($($value:ident):+))? $(?($($optional_value:ident):+))?),+) => {
$(
$(
/// Stores
Expand Down
12 changes: 6 additions & 6 deletions rust/src/nasl/builtin/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,16 +516,16 @@ impl NaslSockets {
// TODO: set timeout to global recv timeout when available
let timeout = Duration::from_secs(10);
self.wait_before_next_probe();
if let Ok(tcp) = TcpConnection::connect_priv(addr, sport, dport.0, timeout) {
match TcpConnection::connect_priv(addr, sport, dport.0, timeout) { Ok(tcp) => {
self.add(NaslSocket::Tcp(Box::new(tcp)))
} else {
} _ => {
continue;
}
} else if let Ok(udp) = UdpConnection::new_priv(addr, sport, dport.0) {
}}
} else { match UdpConnection::new_priv(addr, sport, dport.0) { Ok(udp) => {
self.add(NaslSocket::Udp(udp))
} else {
} _ => {
continue;
};
}}};
return Ok(NaslValue::Number(fd as i64));
}
Err(SocketError::UnableToOpenPrivSocket(addr).into())
Expand Down
18 changes: 9 additions & 9 deletions rust/src/nasl/builtin/network/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ struct TcpDataStream {

impl Read for TcpDataStream {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if let Some(tls) = &mut self.tls {
match &mut self.tls { Some(tls) => {
let mut stream = Stream::new(tls, &mut self.sock);
stream.read(buf)
} else {
} _ => {
self.sock.read(buf)
}
}}
}
}

Expand All @@ -53,22 +53,22 @@ impl BufRead for TcpConnection {
impl Write for TcpConnection {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let stream = self.stream.get_mut();
if let Some(tls) = &mut stream.tls {
match &mut stream.tls { Some(tls) => {
let mut stream = Stream::new(tls, &mut stream.sock);
stream.write(buf)
} else {
} _ => {
stream.sock.write(buf)
}
}}
}

fn flush(&mut self) -> std::io::Result<()> {
let stream = self.stream.get_mut();
if let Some(tls) = &mut stream.tls {
match &mut stream.tls { Some(tls) => {
let mut stream = Stream::new(tls, &mut stream.sock);
stream.flush()
} else {
} _ => {
stream.sock.flush()
}
}}
}
}

Expand Down
6 changes: 3 additions & 3 deletions rust/src/nasl/interpreter/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn as_i64(left: NaslValue, right: Option<NaslValue>) -> (i64, i64) {
}

macro_rules! expr {
($e:expr) => {
($e:expr_2021) => {
$e
};
}
Expand All @@ -50,7 +50,7 @@ macro_rules! num_expr {
Ok(NaslValue::Number(result as i64))
}
};
($op:expr => $left:ident $right:ident) => {
($op:expr_2021 => $left:ident $right:ident) => {
{
let (left, right) = as_i64($left, $right);
let result = $op(left, right);
Expand Down Expand Up @@ -288,7 +288,7 @@ impl Interpreter<'_> {
mod tests {
use crate::nasl::test_prelude::*;
macro_rules! create_test {
($($name:tt: $code:expr => $result:expr),*) => {
($($name:tt: $code:expr_2021 => $result:expr_2021),*) => {

$(
#[test]
Expand Down
14 changes: 7 additions & 7 deletions rust/src/nasl/syntax/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl SyntaxError {
/// ```
#[macro_export]
macro_rules! syntax_error {
($kind:expr) => {{
($kind:expr_2021) => {{
use $crate::nasl::syntax::SyntaxError;
SyntaxError::new($kind, line!(), file!().to_string())
}};
Expand All @@ -107,7 +107,7 @@ macro_rules! syntax_error {
/// ```
#[macro_export]
macro_rules! unexpected_token {
($token:expr) => {{
($token:expr_2021) => {{
use $crate::nasl::syntax::ErrorKind;
use $crate::syntax_error;
syntax_error!(ErrorKind::UnexpectedToken($token))
Expand All @@ -126,7 +126,7 @@ macro_rules! unexpected_token {
/// ```
#[macro_export]
macro_rules! unexpected_statement {
($statement:expr) => {{
($statement:expr_2021) => {{
use $crate::nasl::syntax::ErrorKind;
use $crate::syntax_error;
syntax_error!(ErrorKind::MissingSemicolon($statement))
Expand All @@ -145,7 +145,7 @@ macro_rules! unexpected_statement {
/// ```
#[macro_export]
macro_rules! unclosed_statement {
($statement:expr) => {{
($statement:expr_2021) => {{
use $crate::nasl::syntax::ErrorKind;
use $crate::syntax_error;

Expand All @@ -169,7 +169,7 @@ macro_rules! unclosed_statement {
/// ```
#[macro_export]
macro_rules! unclosed_token {
($token:expr) => {{
($token:expr_2021) => {{
use $crate::nasl::syntax::ErrorKind;
use $crate::syntax_error;

Expand All @@ -188,7 +188,7 @@ macro_rules! unclosed_token {
/// ```
#[macro_export]
macro_rules! unexpected_end {
($reason:expr) => {{
($reason:expr_2021) => {{
use $crate::nasl::syntax::ErrorKind;
use $crate::syntax_error;
syntax_error!(ErrorKind::EoF)
Expand All @@ -208,7 +208,7 @@ macro_rules! unexpected_end {
/// ```
#[macro_export]
macro_rules! max_recursion {
($reason:expr) => {{
($reason:expr_2021) => {{
use $crate::nasl::syntax::ErrorKind;
use $crate::syntax_error;
syntax_error!(ErrorKind::MaxRecursionDepth($reason))
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/syntax/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ mod infix {
}

macro_rules! calculated_test {
($code:expr, $expected:expr) => {
($code:expr_2021, $expected:expr_2021) => {
let expr = parse($code).next().unwrap().unwrap();
assert_eq!(resolve(&expr), $expected);
};
Expand Down
6 changes: 3 additions & 3 deletions rust/src/nasl/syntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub enum UnclosedCategory {
}

macro_rules! make_keyword_matcher {
($($matcher:ident : $define:expr),+) => {
($($matcher:ident : $define:expr_2021),+) => {

impl IdentifierType {
/// Creates a new keyword based on a string identifier
Expand Down Expand Up @@ -737,7 +737,7 @@ impl<'a> Tokenizer<'a> {
//'+' => double_token!(self.cursor, start, '+', '+', PlusPlus, '=', PlusEqual),
// within the Iterator implementation of Tokenizer
macro_rules! two_symbol_token {
($cursor:expr, $start:tt, $single_symbol:tt, $($matching_char:tt, $two_symbol_token:expr ), *) => {
($cursor:expr_2021, $start:tt, $single_symbol:tt, $($matching_char:tt, $two_symbol_token:expr_2021 ), *) => {
{
let next = $cursor.peek(0);
match next {
Expand Down Expand Up @@ -809,7 +809,7 @@ mod tests {

// use macro instead of a method to have correct line numbers on failure
macro_rules! verify_tokens {
($code:expr, $expected:expr) => {{
($code:expr_2021, $expected:expr_2021) => {{
use std::string::String;
let tokenizer = Tokenizer::new($code);
let actual: Vec<String> = tokenizer.map(|t| t.category().to_string()).collect();
Expand Down
16 changes: 8 additions & 8 deletions rust/src/nasl/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ where
}

pub async fn async_verify(mut self) {
if let Err(err) = self.verify().await {
match self.verify().await { Err(err) => {
// Drop first so we don't call the destructor, which would panic.
std::mem::forget(self);
panic!("{}", err)
} else {
} _ => {
std::mem::forget(self)
}
}}
}

fn check_result(
Expand Down Expand Up @@ -379,9 +379,9 @@ impl<L: Loader, S: Storage> Drop for TestBuilder<L, S> {
fn drop(&mut self) {
if tokio::runtime::Handle::try_current().is_ok() {
panic!("To use TestBuilder in an asynchronous context, explicitly call async_verify()");
} else if let Err(err) = futures::executor::block_on(self.verify()) {
} else { match futures::executor::block_on(self.verify()) { Err(err) => {
panic!("{}", err)
}
} _ => {}}}
}
}

Expand All @@ -403,7 +403,7 @@ pub fn check_code_result(code: &str, expected: impl ToNaslResult) {
/// perform a check on the line of code using a new `TestBuilder`.
#[macro_export]
macro_rules! check_err_matches {
($t: ident, $code: expr, $pat: pat $(,)?) => {
($t: ident, $code: expr_2021, $pat: pat $(,)?) => {
$t.check(
$code,
|e| {
Expand All @@ -427,7 +427,7 @@ macro_rules! check_err_matches {
Some(stringify!($pat)),
);
};
($code: expr, $pat: pat $(,)?) => {
($code: expr_2021, $pat: pat $(,)?) => {
let mut t = $crate::nasl::test_utils::TestBuilder::default();
check_err_matches!(t, $code, $pat);
};
Expand All @@ -437,7 +437,7 @@ macro_rules! check_err_matches {
/// and that the inner value matches a pattern.
#[macro_export]
macro_rules! check_code_result_matches {
($code: expr, $pat: pat) => {
($code: expr_2021, $pat: pat) => {
let mut t = $crate::nasl::test_utils::TestBuilder::default();
t.check($code, |val| matches!(val, Ok($pat)), Some(stringify!($pat)));
};
Expand Down
4 changes: 2 additions & 2 deletions rust/src/nasl/utils/function/positionals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a, T: FromNaslValue<'a>> Positionals<'a, T> {
/// Returns an iterator over the positional arguments.
/// The item type is Result<T, FnError>, since
/// the conversion to T can still fail.
pub fn iter(&self) -> impl Iterator<Item = Result<T, FnError>> + 'a {
pub fn iter(&self) -> impl Iterator<Item = Result<T, FnError>> + 'a + use<'a, T> {
self.register
.positional()
.iter()
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<'a, T: FromNaslValue<'a>> CheckedPositionals<T> {

/// Returns an iterator over the references to the positional arguments
/// in the target type `T`.
pub fn iter(&self) -> impl Iterator<Item = &T> + '_ {
pub fn iter(&self) -> impl Iterator<Item = &T> + '_ + use<'_, T> {
self.data.iter()
}

Expand Down
12 changes: 6 additions & 6 deletions rust/src/nasl/utils/function/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ pub fn get_optional_maybe_named_arg<'a, T: FromNaslValue<'a>>(
position: usize,
) -> Result<Option<T>, FnError> {
let via_position = get_optional_positional_arg::<T>(register, position)?;
if let Some(via_position) = via_position {
match via_position { Some(via_position) => {
Ok(Some(via_position))
} else {
} _ => {
get_optional_named_arg(register, name)
}
}}
}

/// A convenience function to obtain an argument
Expand All @@ -101,11 +101,11 @@ pub fn get_maybe_named_arg<'a, T: FromNaslValue<'a>>(
position: usize,
) -> Result<T, FnError> {
let via_position = get_optional_positional_arg(register, position)?;
if let Some(via_position) = via_position {
match via_position { Some(via_position) => {
Ok(via_position)
} else {
} _ => {
get_named_arg(register, name)
}
}}
}

/// Check that named and maybe_named arguments account for
Expand Down
6 changes: 3 additions & 3 deletions rust/src/openvasd/controller/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ impl<S, DB> ContextBuilder<S, DB, NoScanner> {

impl<S, DB> ContextBuilder<S, DB, Scanner<S>> {
fn configure_authentication_methods(&mut self) {
let tls_config = if let Some(tls_config) = self.tls_config.take() {
let tls_config = match self.tls_config.take() { Some(tls_config) => {
if tls_config.has_clients && self.api_key.is_some() {
tracing::warn!("Client certificates and api key are configured. To disable the possibility to bypass client verification the API key is ignored.");
self.api_key = None;
}
Some(tls_config)
} else {
} _ => {
None
};
}};
self.tls_config = tls_config;
match (self.tls_config.is_some(), self.api_key.is_some()) {
(true, true) => unreachable!(),
Expand Down
Loading
Loading