mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2025-07-30 16:08:28 -04:00
This is inspired by the work of v0l (https://github.com/v0l/nostr-rs-relay/). A new trait abstracts the storage layer with an async API. Rusqlite is still used with worker threads, but this allows for Postgresql or other backends to be used. There may be bugs, this has not been rigorously tested.
34 lines
804 B
Rust
34 lines
804 B
Rust
//! Common utility functions
|
|
use std::time::SystemTime;
|
|
|
|
/// Seconds since 1970.
|
|
#[must_use] pub fn unix_time() -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
.map(|x| x.as_secs())
|
|
.unwrap_or(0)
|
|
}
|
|
|
|
/// Check if a string contains only hex characters.
|
|
#[must_use] pub fn is_hex(s: &str) -> bool {
|
|
s.chars().all(|x| char::is_ascii_hexdigit(&x))
|
|
}
|
|
|
|
/// Check if a string contains only lower-case hex chars.
|
|
#[must_use] pub fn is_lower_hex(s: &str) -> bool {
|
|
s.chars().all(|x| {
|
|
(char::is_ascii_lowercase(&x) || char::is_ascii_digit(&x)) && char::is_ascii_hexdigit(&x)
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn lower_hex() {
|
|
let hexstr = "abcd0123";
|
|
assert_eq!(is_lower_hex(hexstr), true);
|
|
}
|
|
}
|