use chrono::{NaiveDate, NaiveDateTime, Utc};
use num_traits::Num;
use std::fmt::Display;
pub fn generate_timestamp() -> String {
let millis = Utc::now().timestamp_millis();
format!("{:016}", millis)
}
pub fn format_string(data: &str) -> String {
data.to_string()
}
pub fn format_number<T>(data: T) -> String
where
T: Num + Display + Copy + 'static,
{
data.to_string()
}
pub fn format_date(data: NaiveDate) -> String {
data.format("%Y-%m-%d").to_string()
}
pub fn format_datetime(data: NaiveDateTime) -> String {
data.format("%Y-%m-%d %H:%M:%S").to_string()
}
pub fn format_fixed_length(s: &str, length: usize) -> String {
let s_len = s.len();
if s_len == length {
s.to_string()
} else if s_len < length {
format!("{:<width$}", s, width = length)
} else {
s[..length].to_string()
}
}
pub fn format_log(
collect_id: &str,
collect_id_length: usize,
id: &str,
id_length: usize,
timestamp: &str,
log_data: &str,
) -> String {
let formatted_collect_id = format_fixed_length(collect_id, collect_id_length);
let formatted_id = format_fixed_length(id, id_length);
format!(
"{}{}{}{}",
formatted_collect_id, formatted_id, timestamp, log_data
)
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::NaiveDate;
#[test]
fn test_generate_timestamp() {
let timestamp = generate_timestamp();
assert_eq!(timestamp.len(), 16);
assert!(timestamp.chars().all(|c| c.is_ascii_digit()));
}
#[test]
fn test_format_string() {
let result = format_string("hello world");
assert_eq!(result, "hello world");
}
#[test]
fn test_format_number_integer() {
let result = format_number(123i32);
assert_eq!(result, "123");
}
#[test]
fn test_format_number_float() {
let result = format_number(75.5f64);
assert_eq!(result, "75.5");
}
#[test]
fn test_format_number_unsigned() {
let result = format_number(42u8);
assert_eq!(result, "42");
}
#[test]
fn test_format_date() {
let date = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
let result = format_date(date);
assert_eq!(result, "2024-01-01");
}
#[test]
fn test_format_datetime() {
let datetime = NaiveDate::from_ymd_opt(2024, 1, 1)
.unwrap()
.and_hms_opt(10, 30, 0)
.unwrap();
let result = format_datetime(datetime);
assert_eq!(result, "2024-01-01 10:30:00");
}
#[test]
fn test_format_fixed_length_exact() {
let result = format_fixed_length("hello", 5);
assert_eq!(result, "hello");
}
#[test]
fn test_format_fixed_length_shorter() {
let result = format_fixed_length("hi", 5);
assert_eq!(result, "hi ");
}
#[test]
fn test_format_fixed_length_longer() {
let result = format_fixed_length("hello world", 5);
assert_eq!(result, "hello");
}
#[test]
fn test_format_log() {
let collect_id = "RESUME-AGENT";
let collect_id_length = 12;
let id = "user_login";
let id_length = 10;
let timestamp = "0001704067200001";
let log_data = r#"{"user_id":123,"status":"success"}"#;
let result = format_log(
collect_id,
collect_id_length,
id,
id_length,
timestamp,
log_data,
);
assert_eq!(
result,
"RESUME-AGENTuser_login0001704067200001{\"user_id\":123,\"status\":\"success\"}"
);
}
#[test]
fn test_format_log_with_padding() {
let collect_id = "SHORT";
let collect_id_length = 12;
let id = "login";
let id_length = 10;
let timestamp = "0001704067200001";
let log_data = r#"{"user_id":123}"#;
let result = format_log(
collect_id,
collect_id_length,
id,
id_length,
timestamp,
log_data,
);
assert_eq!(
result,
"SHORT login 0001704067200001{\"user_id\":123}"
);
}
#[test]
fn test_format_log_with_truncation() {
let collect_id = "VERY-LONG-COLLECT-ID";
let collect_id_length = 12;
let id = "very-long-id-name";
let id_length = 10;
let timestamp = "0001704067200001";
let log_data = r#"{"user_id":123}"#;
let result = format_log(
collect_id,
collect_id_length,
id,
id_length,
timestamp,
log_data,
);
assert_eq!(
result,
"VERY-LONG-COvery-long-0001704067200001{\"user_id\":123}"
);
}
}