Refacto structures definition
This commit is contained in:
@ -1,6 +1,13 @@
|
||||
use std::ops::{Div, Mul};
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum FilesUtilsError {
|
||||
#[error("UnitConvertError: {0}")]
|
||||
UnitConvert(String),
|
||||
}
|
||||
|
||||
const INVALID_CHARS: [&str; 19] = [
|
||||
"@", "\\", "/", ":", ",", "<", ">", "%", "'", "\"", "?", "{", "}", "$", "*", "|", ";", "=",
|
||||
"\t",
|
||||
@ -28,9 +35,31 @@ pub fn set_file_permission<P: AsRef<Path>>(path: P, mode: u32) -> anyhow::Result
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Convert size unit to MB
|
||||
pub fn convert_size_unit_to_mb(unit: &str, value: usize) -> anyhow::Result<usize> {
|
||||
let fact = match unit {
|
||||
"bytes" | "b" => 1f64,
|
||||
"KB" => 1000f64,
|
||||
"MB" => 1000f64 * 1000f64,
|
||||
"GB" => 1000f64 * 1000f64 * 1000f64,
|
||||
"TB" => 1000f64 * 1000f64 * 1000f64 * 1000f64,
|
||||
|
||||
"k" | "KiB" => 1024f64,
|
||||
"M" | "MiB" => 1024f64 * 1024f64,
|
||||
"G" | "GiB" => 1024f64 * 1024f64 * 1024f64,
|
||||
"T" | "TiB" => 1024f64 * 1024f64 * 1024f64 * 1024f64,
|
||||
|
||||
_ => {
|
||||
return Err(FilesUtilsError::UnitConvert(format!("Unknown size unit: {unit}")).into());
|
||||
}
|
||||
};
|
||||
|
||||
Ok((value as f64).mul(fact.div((1000 * 1000) as f64)).ceil() as usize)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::utils::files_utils::check_file_name;
|
||||
use crate::utils::files_utils::{check_file_name, convert_size_unit_to_mb};
|
||||
|
||||
#[test]
|
||||
fn empty_file_name() {
|
||||
@ -56,4 +85,14 @@ mod test {
|
||||
fn valid_file_name() {
|
||||
assert!(check_file_name("test.iso"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_units_mb() {
|
||||
assert_eq!(convert_size_unit_to_mb("MB", 1).unwrap(), 1);
|
||||
assert_eq!(convert_size_unit_to_mb("MB", 1000).unwrap(), 1000);
|
||||
assert_eq!(convert_size_unit_to_mb("GB", 1000).unwrap(), 1000 * 1000);
|
||||
assert_eq!(convert_size_unit_to_mb("GB", 1).unwrap(), 1000);
|
||||
assert_eq!(convert_size_unit_to_mb("GiB", 3).unwrap(), 3222);
|
||||
assert_eq!(convert_size_unit_to_mb("KiB", 488281).unwrap(), 500);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
pub mod disks_utils;
|
||||
pub mod files_utils;
|
||||
pub mod net_utils;
|
||||
pub mod rand_utils;
|
||||
pub mod time_utils;
|
||||
pub mod url_utils;
|
||||
|
19
virtweb_backend/src/utils/net_utils.rs
Normal file
19
virtweb_backend/src/utils/net_utils.rs
Normal file
@ -0,0 +1,19 @@
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
|
||||
pub fn extract_ipv4(ip: IpAddr) -> Ipv4Addr {
|
||||
match ip {
|
||||
IpAddr::V4(i) => i,
|
||||
IpAddr::V6(_) => {
|
||||
panic!("IPv6 found in IPv4 definition!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_ipv6(ip: IpAddr) -> Ipv6Addr {
|
||||
match ip {
|
||||
IpAddr::V4(_) => {
|
||||
panic!("IPv4 found in IPv6 definition!")
|
||||
}
|
||||
IpAddr::V6(i) => i,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user