rust/conf: rust format

pull/12907/head
Jason Ish 1 year ago committed by Victor Julien
parent 813eb27b95
commit 640dd7c582

@ -17,18 +17,18 @@
//! Module for retrieving configuration details.
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::ffi::{CString, CStr};
use std::ptr;
use std::str;
use nom7::{
character::complete::{multispace0, not_line_ending},
sequence::{preceded, tuple},
number::complete::double,
combinator::verify,
number::complete::double,
sequence::{preceded, tuple},
IResult,
};
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::ptr;
use std::str;
use suricata_sys::sys::SCConfGet;
use suricata_sys::sys::SCConfGetChildValue;
use suricata_sys::sys::SCConfGetChildValueBool;
@ -66,9 +66,7 @@ pub fn conf_get(key: &str) -> Option<&str> {
return None;
}
let value = str::from_utf8(unsafe{
CStr::from_ptr(vptr).to_bytes()
}).unwrap();
let value = str::from_utf8(unsafe { CStr::from_ptr(vptr).to_bytes() }).unwrap();
return Some(value);
}
@ -90,9 +88,8 @@ pub struct ConfNode {
}
impl ConfNode {
pub fn wrap(conf: *const SCConfNode) -> Self {
return Self { conf }
return Self { conf };
}
pub fn get_child_value(&self, key: &str) -> Option<&str> {
@ -100,9 +97,7 @@ impl ConfNode {
unsafe {
let s = CString::new(key).unwrap();
if SCConfGetChildValue(self.conf,
s.as_ptr(),
&mut vptr) != 1 {
if SCConfGetChildValue(self.conf, s.as_ptr(), &mut vptr) != 1 {
return None;
}
}
@ -111,9 +106,7 @@ impl ConfNode {
return None;
}
let value = str::from_utf8(unsafe{
CStr::from_ptr(vptr).to_bytes()
}).unwrap();
let value = str::from_utf8(unsafe { CStr::from_ptr(vptr).to_bytes() }).unwrap();
return Some(value);
}
@ -123,9 +116,7 @@ impl ConfNode {
unsafe {
let s = CString::new(key).unwrap();
if SCConfGetChildValueBool(self.conf,
s.as_ptr(),
&mut vptr) != 1 {
if SCConfGetChildValueBool(self.conf, s.as_ptr(), &mut vptr) != 1 {
return false;
}
}
@ -135,13 +126,12 @@ impl ConfNode {
}
return false;
}
}
const BYTE: u64 = 1;
const KILOBYTE: u64 = 1024;
const MEGABYTE: u64 = 1_048_576;
const GIGABYTE: u64 = 1_073_741_824;
const BYTE: u64 = 1;
const KILOBYTE: u64 = 1024;
const MEGABYTE: u64 = 1_048_576;
const GIGABYTE: u64 = 1_073_741_824;
/// Helper function to retrieve memory unit from a string slice
///
@ -153,11 +143,11 @@ const GIGABYTE: u64 = 1_073_741_824;
fn get_memunit(unit: &str) -> u64 {
let unit = &unit.to_lowercase()[..];
match unit {
"b" => { BYTE }
"kb" => { KILOBYTE }
"mb" => { MEGABYTE }
"gb" => { GIGABYTE }
_ => { 0 }
"b" => BYTE,
"kb" => KILOBYTE,
"mb" => MEGABYTE,
"gb" => GIGABYTE,
_ => 0,
}
}
@ -174,8 +164,10 @@ pub fn get_memval(arg: &str) -> Result<u64, &'static str> {
let arg = arg.trim();
let val: f64;
let mut unit: &str;
let mut parser = tuple((preceded(multispace0, double),
preceded(multispace0, verify(not_line_ending, |c: &str| c.len() < 3))));
let mut parser = tuple((
preceded(multispace0, double),
preceded(multispace0, verify(not_line_ending, |c: &str| c.len() < 3)),
));
let r: IResult<&str, (f64, &str)> = parser(arg);
if let Ok(r) = r {
val = (r.1).0;
@ -201,7 +193,7 @@ mod tests {
#[test]
fn test_memval_nospace() {
let s = "10";
let res = 10 ;
let res = 10;
assert_eq!(Ok(10), get_memval(s));
let s = "10kb";
@ -223,7 +215,7 @@ mod tests {
#[test]
fn test_memval_space_start() {
let s = " 10";
let res = 10 ;
let res = 10;
assert_eq!(Ok(res), get_memval(s));
let s = " 10Kb";
@ -242,7 +234,7 @@ mod tests {
#[test]
fn test_memval_space_end() {
let s = " 10 ";
let res = 10 ;
let res = 10;
assert_eq!(Ok(res), get_memval(s));
let s = "10Kb ";
@ -261,7 +253,7 @@ mod tests {
#[test]
fn test_memval_space_in_bw() {
let s = " 10 ";
let res = 10 ;
let res = 10;
assert_eq!(Ok(res), get_memval(s));
let s = "10 Kb ";

Loading…
Cancel
Save