1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 15:40:03 +08:00

chore: coding style

This commit is contained in:
2020-11-08 23:10:33 +08:00
parent 755dc5619d
commit 6e821a4cd9
4 changed files with 30 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "rust_util" name = "rust_util"
version = "0.6.15" version = "0.6.16"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Hatter's Rust Util" description = "Hatter's Rust Util"

View File

@@ -1,8 +1,5 @@
use std::{ use std::{ fs::File, io::{ self, ErrorKind, prelude::* } };
fs::File, use std::time::{ SystemTime, Duration };
io::{ self, ErrorKind, prelude::* },
time::{ SystemTime, Duration },
};
use crate::{ XResult, new_box_ioerror }; use crate::{ XResult, new_box_ioerror };
use crate::util_size; use crate::util_size;

View File

@@ -9,39 +9,35 @@ pub const SIZE_PB: i64 = SIZE_TB * SIZE_KB;
pub fn parse_size(size: &str) -> XResult<i64> { pub fn parse_size(size: &str) -> XResult<i64> {
let lower_size = size.to_lowercase(); let lower_size = size.to_lowercase();
let no_last_b_size = if lower_size.ends_with('b') { let no_last_b_size = crate::iff!(
&lower_size[0..lower_size.len()-1] lower_size.ends_with('b'), &lower_size[0..lower_size.len()-1], &lower_size
} else { );
&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') { match no_last_b_size.chars().last() {
return Ok((SIZE_KB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64); Some('k') => parse_and_process_size(SIZE_KB),
} else if no_last_b_size.ends_with('m') { Some('m') => parse_and_process_size(SIZE_MB),
return Ok((SIZE_MB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64); Some('g') => parse_and_process_size(SIZE_GB),
} else if no_last_b_size.ends_with('g') { Some('t') => parse_and_process_size(SIZE_TB),
return Ok((SIZE_GB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64); Some('p') => parse_and_process_size(SIZE_PB),
} else if no_last_b_size.ends_with('t') { _ => Ok(no_last_b_size.parse::<i64>()?),
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);
} }
Ok(no_last_b_size.parse::<i64>()?)
} }
pub fn get_display_size(size: i64) -> String { pub fn get_display_size(size: i64) -> String {
if size < SIZE_KB { if size < SIZE_KB {
size.to_string() size.to_string()
} else if size < SIZE_MB { } 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 { } 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 { } 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 { } 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 { } else {
format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.) format!("{:.*}PB", 2, (size as f64) / SIZE_PB as f64)
} }
} }

View File

@@ -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] #[test]
fn test_parse_duration() { fn test_parse_duration() {
assert_eq!(None, parse_duration("")); assert_eq!(None, parse_duration(""));