Skip to main content

iris_hub/
utils.rs

1//! # Utils Module
2//! 
3//! Funções utilitárias usadas em toda a aplicação.
4
5/// Gera um ID simples baseado em timestamp.
6/// 
7/// Combina segundos desde UNIX epoch com nanosegundos
8/// para criar um identificador único.
9/// 
10/// # Exemplo
11/// ```rust
12/// let id = uuid_simple();
13/// assert!(!id.is_empty());
14/// ```
15pub fn uuid_simple() -> String {
16    use std::time::{SystemTime, UNIX_EPOCH};
17    let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
18    format!("{}{}", duration.as_secs(), duration.subsec_nanos())
19}
20
21/// Trunca um caminho para exibição.
22/// 
23/// Se o caminho for maior que o tamanho máximo,
24/// adiciona "..." no início e mostra apenas o final.
25/// 
26/// # Argumentos
27/// * `path` - Caminho a ser truncado
28/// * `max_len` - Tamanho máximo em caracteres
29/// 
30/// # Exemplo
31/// ```rust
32/// let truncated = truncate_path("C:\\Users\\Name\\Projects\\MyApp", 20);
33/// assert!(truncated.len() <= 20);
34/// ```
35pub fn truncate_path(path: &str, max_len: usize) -> String {
36    if path.len() <= max_len {
37        path.to_string()
38    } else {
39        format!("...{}", &path[path.len() - max_len + 3..])
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    
47    #[test]
48    fn test_uuid_simple_not_empty() {
49        let id = uuid_simple();
50        assert!(!id.is_empty());
51    }
52    
53    #[test]
54    fn test_uuid_simple_unique() {
55        let id1 = uuid_simple();
56        std::thread::sleep(std::time::Duration::from_nanos(1));
57        let id2 = uuid_simple();
58        assert_ne!(id1, id2);
59    }
60    
61    #[test]
62    fn test_truncate_path_short() {
63        let path = "C:\\short";
64        let result = truncate_path(path, 20);
65        assert_eq!(result, path);
66    }
67    
68    #[test]
69    fn test_truncate_path_long() {
70        let path = "C:\\Users\\Name\\Very\\Long\\Path\\To\\Project";
71        let result = truncate_path(path, 20);
72        assert!(result.starts_with("..."));
73        assert!(result.len() <= 20);
74    }
75}