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
//! Various utility functions
//!
pub mod fs;
pub mod hasher;
pub mod logging;
pub mod paginate;
pub mod progress_bar;
pub mod read_progress;
pub mod str;
pub use crate::util::read_progress::ReadProgress;
pub use paginate::{paginate, paginate_with_total};
pub mod oxen_date_format {
use chrono::{DateTime, Local};
use serde::{self, Deserialize, Deserializer, Serializer};
pub const FORMAT: &str = "%a, %d %b %Y %H:%M:%S %z";
// The signature of a serialize_with function must follow the pattern:
//
// fn serialize<S>(&T, S) -> Result<S::Ok, S::Error>
// where
// S: Serializer
//
// although it may also be generic over the input types T.
pub fn serialize<S>(date: &DateTime<Local>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = format!("{}", date.format(FORMAT));
serializer.serialize_str(&s)
}
// The signature of a deserialize_with function must follow the pattern:
//
// fn deserialize<'de, D>(D) -> Result<T, D::Error>
// where
// D: Deserializer<'de>
//
// although it may also be generic over the output types T.
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Local>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
DateTime::parse_from_str(&s, FORMAT)
.map(Into::into)
.map_err(serde::de::Error::custom)
}
}