use chrono::{Datelike, Utc};
use rand::distributions::Alphanumeric;
use rand::Rng;
pub fn generate_share_token(length: usize) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(length)
.map(char::from)
.collect()
}
pub fn slugify(input: &str) -> String {
input
.to_lowercase()
.chars()
.map(|c| {
match c {
'a'..='z' | '0'..='9' => c,
' ' | '_' | '-' => '-',
_ => '-',
}
})
.collect::<String>()
.split('-')
.filter(|s| !s.is_empty())
.collect::<Vec<&str>>()
.join("-")
}
pub fn generate_storage_key() -> String {
let now = chrono::Utc::now();
let uuid = uuid::Uuid::new_v4();
format!(
"{:04}/{:02}/{:02}/{}",
now.year(),
now.month(),
now.day(),
uuid.to_string()
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_share_token() {
let token = generate_share_token(32);
assert_eq!(token.len(), 32);
assert!(token.chars().all(|c| c.is_alphanumeric()));
}
#[test]
fn test_slugify_basic() {
assert_eq!(slugify("My Bucket Name"), "my-bucket-name");
assert_eq!(slugify("Hello World"), "hello-world");
}
#[test]
fn test_slugify_special_chars() {
assert_eq!(slugify("My Bucket!!!"), "my-bucket");
assert_eq!(slugify("Test@#$%Bucket"), "test-bucket");
}
#[test]
fn test_slugify_multiple_spaces() {
assert_eq!(slugify("My Bucket Name"), "my-bucket-name");
assert_eq!(slugify(" leading trailing "), "leading-trailing");
}
#[test]
fn test_generate_storage_key() {
let key = generate_storage_key();
let parts: Vec<&str> = key.split('/').collect();
assert_eq!(parts.len(), 4); assert!(parts[3].len() == 36); }
}