1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
pub fn current_timestamp() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("time went backwards")
.as_nanos()
}
pub fn checksum(data: &[u8]) -> u32 {
crc::crc32::checksum_ieee(data)
}
pub fn parse_file_id(path: &Path) -> Option<u64> {
path.file_name()?
.to_str()?
.split('.')
.next()?
.parse::<u64>()
.ok()
}
pub fn to_utf8_string(value: &[u8]) -> String {
String::from_utf8_lossy(value).to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_file_id() {
let r = parse_file_id(Path::new("path/to/12345.tinkv.data"));
assert_eq!(r, Some(12345 as u64));
let r = parse_file_id(Path::new("path/to/.tinkv.data"));
assert_eq!(r, None);
let r = parse_file_id(Path::new("path/to"));
assert_eq!(r, None);
}
#[test]
fn test_to_utf8_str() {
assert_eq!(to_utf8_string(b"hello, world"), "hello, world".to_owned());
}
}