srad_types/
utils.rs

1use std::time::{SystemTime, UNIX_EPOCH};
2
3/// Get the current unix timestamp
4pub fn timestamp() -> u64 {
5    SystemTime::now()
6        .duration_since(UNIX_EPOCH)
7        .unwrap()
8        .as_millis() as u64
9}
10
11/// Validate a provided name value
12pub fn validate_name(name: &str) -> Result<(), String> {
13    if name.is_empty() {
14        return Err("name string must not be empty".into());
15    }
16    for c in name.chars() {
17        if matches!(c, '+' | '/' | '#') {
18            return Err(format!(
19                "name string {name} cannot contain '+', '/' or '#' characters"
20            ));
21        }
22    }
23    Ok(())
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn test_valiate_name_valid_strings() {
32        assert!(validate_name("hello").is_ok());
33        assert!(validate_name("hello123").is_ok());
34        assert!(validate_name("hello_world").is_ok());
35    }
36
37    #[test]
38    fn test_validate_name_invalid_strings() {
39        assert!(validate_name("").is_err());
40        assert!(validate_name("hello+world").is_err());
41        assert!(validate_name("hello/world").is_err());
42        assert!(validate_name("hello#world").is_err());
43        assert!(validate_name("hello+/world").is_err());
44        assert!(validate_name("hello+/#world").is_err());
45    }
46}