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
//! Extensions for things and generic utilities

use chrono::{DateTime, SecondsFormat, Utc};

/// Extensions for [chrono::DateTime] for nicer functionality
pub trait DateTimeUtils {
    /// return a DateTime object as a string
    fn to_saml_datetime_string(&self) -> String;
}

impl DateTimeUtils for DateTime<Utc> {
    /// return a DateTime object as a string
    fn to_saml_datetime_string(&self) -> String {
        self.to_rfc3339_opts(SecondsFormat::Secs, true)
    }
}

/// Takes a [Vec<u8>] and turns it into a [String]
///
/// With an optional "join" string to allow you to space it out etc.
///
/// From <https://illegalargumentexception.blogspot.com/2015/05/rust-byte-array-to-hex-string.html>
pub fn to_hex_string(bytes: Vec<u8>, join: Option<&str>) -> String {
    let strs: Vec<String> = bytes
        .iter()
        .map(|b| format!("{:02X}", b).to_lowercase())
        .collect();
    match join {
        Some(joinval) => strs.join(joinval),
        None => strs.join(""),
    }
}