dnspod_lib/
utils.rs

1use chrono::{DateTime, Utc};
2use serde::Serializer;
3
4
5/// Convert [u8] to Hex string
6///
7/// Example:
8///
9/// ```
10/// # use dnspod_lib::utils::encode_hex;
11/// assert_eq!(encode_hex(&[0, 15, 16, 255]), "000f10ff");
12/// ```
13pub fn encode_hex(input: &impl AsRef<[u8]>) -> String {
14    input
15        .as_ref()
16        .iter()
17        .map(|b| format!("{:0>2x}", b))
18        .collect()
19}
20
21/// https://docs.rs/serde/latest/serde/ser/trait.Serializer.html#tymethod.serialize_none
22pub(crate) fn none_to_empty_string<S>(
23    input: &Option<String>,
24    serializer: S,
25) -> std::result::Result<S::Ok, S::Error>
26where
27    S: Serializer,
28{
29    match input {
30        Some(ref s) => serializer.serialize_some(s),
31        None => serializer.serialize_some(""),
32    }
33}
34
35pub(crate) fn datetime_to_timestamp_string<S>(
36    input: &DateTime<Utc>,
37    serializer: S,
38) -> std::result::Result<S::Ok, S::Error>
39where
40    S: Serializer,
41{
42    serializer.serialize_str(input.timestamp().to_string().as_str())
43}