AIRA/src/utils.rs

51 lines
1.5 KiB
Rust
Raw Normal View History

2021-04-30 18:54:48 +02:00
use std::{convert::TryInto, time::{SystemTime, UNIX_EPOCH}, path::PathBuf};
2021-03-20 18:03:47 +01:00
use uuid::Bytes;
use crate::print_error;
pub fn to_uuid_bytes(bytes: &[u8]) -> Option<Bytes> {
match bytes.try_into() {
Ok(uuid) => Some(uuid),
Err(e) => {
print_error!(e);
None
}
}
}
pub fn escape_double_quote(origin: String) -> String {
origin.replace("\"", "\\\"")
}
2021-06-16 16:15:21 +02:00
pub fn get_unix_timestamp_ms() -> u128 {
2021-04-28 17:57:49 +02:00
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis()
}
2021-06-16 16:15:21 +02:00
pub fn get_unix_timestamp_sec() -> u64 {
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
}
2021-04-30 18:54:48 +02:00
pub fn get_not_used_path(file_name: &str, parent_directory: &PathBuf) -> String {
let has_extension = file_name.matches('.').count() > 0;
let mut path = parent_directory.join(&file_name);
let mut n = 1;
while path.exists() {
path = if has_extension {
let splits: Vec<&str> = file_name.split('.').collect();
parent_directory.join(format!("{} ({}).{}", splits[..splits.len()-1].join("."), n, splits[splits.len()-1]))
} else {
parent_directory.join(format!("{} ({})", file_name, n))
};
n += 1;
}
path.to_str().unwrap().to_owned()
}
2021-03-20 18:03:47 +01:00
#[macro_export]
macro_rules! print_error {
($arg:tt) => ({
println!("[{}:{}] {}", file!(), line!(), $arg);
});
($($arg:tt)*) => ({
println!("[{}:{}] {}", file!(), line!(), format_args!($($arg)*));
})
}