nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

utils.rs (768B)


      1 //! Common utility functions
      2 use std::time::SystemTime;
      3 
      4 /// Seconds since 1970.
      5 pub fn unix_time() -> u64 {
      6     SystemTime::now()
      7         .duration_since(SystemTime::UNIX_EPOCH)
      8         .map(|x| x.as_secs())
      9         .unwrap_or(0)
     10 }
     11 
     12 /// Check if a string contains only hex characters.
     13 pub fn is_hex(s: &str) -> bool {
     14     s.chars().all(|x| char::is_ascii_hexdigit(&x))
     15 }
     16 
     17 /// Check if a string contains only lower-case hex chars.
     18 pub fn is_lower_hex(s: &str) -> bool {
     19     s.chars().all(|x| {
     20         (char::is_ascii_lowercase(&x) || char::is_ascii_digit(&x)) && char::is_ascii_hexdigit(&x)
     21     })
     22 }
     23 
     24 #[cfg(test)]
     25 mod tests {
     26     use super::*;
     27 
     28     #[test]
     29     fn lower_hex() {
     30         let hexstr = "abcd0123";
     31         assert_eq!(is_lower_hex(hexstr), true);
     32     }
     33 }