refactor: Move common utility functions to src/modes/common.rs and update module structure
This commit is contained in:
48
src/modes/common.rs
Normal file
48
src/modes/common.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
use std::collections::HashMap;
|
||||
use regex::Regex;
|
||||
use humansize::{FormatSizeOptions, BINARY};
|
||||
use log::debug;
|
||||
|
||||
pub fn get_meta_from_env() -> HashMap<String, String> {
|
||||
debug!("MAIN: Getting meta from KEEP_META_*");
|
||||
let re = Regex::new(r"^KEEP_META_(.+)$").unwrap();
|
||||
let mut meta_env: HashMap<String, String> = HashMap::new();
|
||||
for (key, value) in env::vars() {
|
||||
if let Some(meta_name_caps) = re.captures(key.as_str()) {
|
||||
let name = String::from(meta_name_caps.get(1).unwrap().as_str());
|
||||
debug!("MAIN: Found meta: {}={}", name.clone(), value.clone());
|
||||
meta_env.insert(name, value.clone());
|
||||
}
|
||||
}
|
||||
meta_env
|
||||
}
|
||||
|
||||
pub fn format_size_human_readable(size: u64) -> String {
|
||||
let options = FormatSizeOptions::from(BINARY)
|
||||
.decimal_places(1);
|
||||
humansize::format_size(size, options)
|
||||
}
|
||||
|
||||
pub fn format_size(size: u64, human_readable: bool) -> String {
|
||||
match human_readable {
|
||||
true => format_size_human_readable(size),
|
||||
false => size.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn string_column(s: String, column_width: usize) -> String {
|
||||
if column_width > 0 {
|
||||
match s.char_indices().nth(column_width) {
|
||||
None => s.to_string(),
|
||||
Some((idx, _)) => s[..idx].to_string(),
|
||||
}
|
||||
} else {
|
||||
s.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size_column(size: u64, human_readable: bool, column_width: usize) -> String {
|
||||
string_column(format_size(size, human_readable), column_width)
|
||||
}
|
||||
1
src/modes/mod.rs
Normal file
1
src/modes/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod common;
|
||||
Reference in New Issue
Block a user