summaryrefslogtreecommitdiffstats
path: root/src/utils/misc.rs
blob: accf7315af9773549c87eb84d3dbff340e13739f (plain)
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
use chrono::*;
use std::fmt::Display;
use std::time;

pub fn joinlines(first: &str, second: &str) -> String {
  use itertools::Itertools;

  let first = first.split(|x| x == '\n');
  let second = second.split(|x| x == '\n');
  let maxlen = first.clone().map(|x| x.len()).max().unwrap();

  first
    .zip(second)
    .map(|(fst, snd)| format!("{:width$} {}", fst, snd, width = maxlen))
    .join("\n")
}

pub fn format_duration(duration: &time::Duration) -> impl Display {
  //TODO replace this with duration.as_millis() when it becomes stable
  duration.as_secs() * 1000 + u64::from(duration.subsec_millis())
}

pub fn get_bucket_for_date(date: Date<Local>) -> String {
  date.format("%G-W%V").to_string()
}

#[cfg(not(test))]
pub fn make_new_uid() -> String {
  use uuid::Uuid;

  let suffix = "@khaleesi";
  format!("{}{}", Uuid::new_v4().to_hyphenated_ref(), suffix)
}

#[cfg(test)]
pub fn make_new_uid() -> String {
  "11111111-2222-3333-4444-444444444444@khaleesi".to_string()
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn format_duration_test() {
    let millis: u64 = 12345678;
    let duration = time::Duration::from_millis(millis);
    let string_duration = format!("{}", format_duration(&duration));
    let string_from_secs = format!("{}", millis);
    assert_eq!(string_from_secs, string_duration);
  }

  #[test]
  fn joinlines_test() {
    let first = ["123", "ß", "1234"].join("\n");
    let second = ["abc", "1", "Otto"].join("\n");
    let expected = indoc!("
      123  abc
      ß    1
      1234 Otto");
    assert_eq!(expected, joinlines(first.as_str(), second.as_str()));
  }
}