mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 07:30:05 +08:00
chore: coding style
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rust_util"
|
||||
version = "0.6.15"
|
||||
version = "0.6.16"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Hatter's Rust Util"
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{ self, ErrorKind, prelude::* },
|
||||
time::{ SystemTime, Duration },
|
||||
};
|
||||
use std::{ fs::File, io::{ self, ErrorKind, prelude::* } };
|
||||
use std::time::{ SystemTime, Duration };
|
||||
|
||||
use crate::{ XResult, new_box_ioerror };
|
||||
use crate::util_size;
|
||||
|
||||
@@ -9,39 +9,35 @@ pub const SIZE_PB: i64 = SIZE_TB * SIZE_KB;
|
||||
|
||||
pub fn parse_size(size: &str) -> XResult<i64> {
|
||||
let lower_size = size.to_lowercase();
|
||||
let no_last_b_size = if lower_size.ends_with('b') {
|
||||
&lower_size[0..lower_size.len()-1]
|
||||
} else {
|
||||
&lower_size
|
||||
let no_last_b_size = crate::iff!(
|
||||
lower_size.ends_with('b'), &lower_size[0..lower_size.len()-1], &lower_size
|
||||
);
|
||||
let parse_and_process_size = |mul: i64| -> XResult<i64> {
|
||||
Ok((mul as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64)
|
||||
};
|
||||
if no_last_b_size.ends_with('k') {
|
||||
return Ok((SIZE_KB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with('m') {
|
||||
return Ok((SIZE_MB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with('g') {
|
||||
return Ok((SIZE_GB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with('t') {
|
||||
return Ok((SIZE_TB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with('p') {
|
||||
return Ok((SIZE_PB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
match no_last_b_size.chars().last() {
|
||||
Some('k') => parse_and_process_size(SIZE_KB),
|
||||
Some('m') => parse_and_process_size(SIZE_MB),
|
||||
Some('g') => parse_and_process_size(SIZE_GB),
|
||||
Some('t') => parse_and_process_size(SIZE_TB),
|
||||
Some('p') => parse_and_process_size(SIZE_PB),
|
||||
_ => Ok(no_last_b_size.parse::<i64>()?),
|
||||
}
|
||||
|
||||
Ok(no_last_b_size.parse::<i64>()?)
|
||||
}
|
||||
|
||||
pub fn get_display_size(size: i64) -> String {
|
||||
if size < SIZE_KB {
|
||||
size.to_string()
|
||||
} else if size < SIZE_MB {
|
||||
format!("{:.*}KB", 2, (size as f64) / 1024.)
|
||||
format!("{:.*}KB", 2, (size as f64) / SIZE_KB as f64)
|
||||
} else if size < SIZE_GB {
|
||||
format!("{:.*}MB", 2, (size as f64) / 1024. / 1024.)
|
||||
format!("{:.*}MB", 2, (size as f64) / SIZE_MB as f64)
|
||||
} else if size < SIZE_TB {
|
||||
format!("{:.*}GB", 2, (size as f64) / 1024. / 1024. / 1024.)
|
||||
format!("{:.*}GB", 2, (size as f64) / SIZE_GB as f64)
|
||||
} else if size < SIZE_PB {
|
||||
format!("{:.*}TB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024.)
|
||||
format!("{:.*}TB", 2, (size as f64) / SIZE_TB as f64)
|
||||
} else {
|
||||
format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.)
|
||||
format!("{:.*}PB", 2, (size as f64) / SIZE_PB as f64)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,16 @@ pub fn parse_duration(t: &str) -> Option<Duration> {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_current_secs() {
|
||||
assert!(get_current_secs() != 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_current_millis() {
|
||||
assert!(get_current_millis() != 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_duration() {
|
||||
assert_eq!(None, parse_duration(""));
|
||||
|
||||
Reference in New Issue
Block a user