From 6e821a4cd970f76c1ead34abd66bac3dad4cef89 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 8 Nov 2020 23:10:33 +0800 Subject: [PATCH] chore: coding style --- Cargo.toml | 2 +- src/util_io.rs | 7 ++----- src/util_size.rs | 38 +++++++++++++++++--------------------- src/util_time.rs | 10 ++++++++++ 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e103f29..2c04a39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.15" +version = "0.6.16" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_io.rs b/src/util_io.rs index 483bb2a..4e63ce6 100644 --- a/src/util_io.rs +++ b/src/util_io.rs @@ -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; diff --git a/src/util_size.rs b/src/util_size.rs index 14bba85..6601e3e 100644 --- a/src/util_size.rs +++ b/src/util_size.rs @@ -9,39 +9,35 @@ pub const SIZE_PB: i64 = SIZE_TB * SIZE_KB; pub fn parse_size(size: &str) -> XResult { 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 { + Ok((mul as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) 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::()?) 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::()?) 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::()?) 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::()?) 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::()?) 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::()?), } - - Ok(no_last_b_size.parse::()?) } 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) } } diff --git a/src/util_time.rs b/src/util_time.rs index a4070e1..558fd47 100644 --- a/src/util_time.rs +++ b/src/util_time.rs @@ -25,6 +25,16 @@ pub fn parse_duration(t: &str) -> Option { } } +#[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(""));