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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
pub use self::times::*;
use crate::{ConfigFile, ConfigFileVec};
use glob::glob;
use std::{
fs::File,
io::{self, BufRead, BufReader},
str::FromStr,
string::ToString,
};
pub fn collect_config_files(pattern: &str, required: bool) -> ConfigFileVec {
glob(pattern)
.expect("")
.map(|p| ConfigFile::from(p.expect("Failed to read the pathbuf")).required(required))
.collect::<Vec<_>>()
}
pub fn file_to_vec(fp: String) -> io::Result<Vec<String>> {
let file_in = File::open(fp)?;
let file_reader = BufReader::new(file_in);
Ok(file_reader.lines().filter_map(io::Result::ok).collect())
}
pub fn is_float<T: ToString>(data: &T) -> bool {
f64::from_str(&data.to_string()).is_ok()
}
pub(crate) mod times {
use chrono::{DateTime, TimeZone, Utc};
pub fn chrono_datetime_now() -> DateTime<Utc> {
Utc::now()
}
pub fn chrono_into_bson<T: TimeZone>(data: DateTime<T>) -> bson::DateTime {
bson::DateTime::from_chrono(data)
}
pub fn timestamp() -> i64 {
Utc::now().timestamp()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_file_to_vec() {
let fp = "README.md".to_string();
let a = file_to_vec(fp);
assert!(a.is_ok());
assert!(!a.expect("").is_empty())
}
#[test]
fn test_is_float() {
let data = vec!["1", "-10", "ifjuka87"];
assert!(is_float(&data[0]));
assert!(is_float(&data[1]));
assert!(!is_float(&data[2]))
}
}