hydrus_api/
utils.rs

1use crate::api_core::common::FileIdentifier;
2use crate::wrapper::tag::Tag;
3use chrono::{Datelike, Duration};
4
5/// Converts a list of tags into a list of string tags
6pub fn tag_list_to_string_list(tags: Vec<Tag>) -> Vec<String> {
7    tags.into_iter().map(|t| t.to_string()).collect()
8}
9
10pub(crate) fn format_datetime<D: Datelike>(datetime: D) -> String {
11    format!(
12        "{:04}-{:02}-{:02}",
13        datetime.year(),
14        datetime.month(),
15        datetime.day()
16    )
17}
18
19pub(crate) fn format_duration(duration: Duration) -> String {
20    let mut expression = String::new();
21    let days = duration.num_days();
22    let hours = duration.num_hours() % 24;
23    let minutes = duration.num_minutes() % 60;
24    let seconds = duration.num_seconds() % 60;
25
26    if days > 0 {
27        expression.push_str(&days.to_string());
28        expression.push_str(" days ");
29    }
30    if hours > 0 {
31        expression.push_str(&hours.to_string());
32        expression.push_str(" hours ")
33    }
34    if minutes > 0 {
35        expression.push_str(&minutes.to_string());
36        expression.push_str(" minutes ");
37    }
38    expression.push_str(&seconds.to_string());
39    expression.push_str(" seconds");
40
41    expression
42}
43
44pub(crate) fn split_file_identifiers_into_hashes_and_ids(
45    files: Vec<FileIdentifier>,
46) -> (Vec<u64>, Vec<String>) {
47    let mut ids = Vec::new();
48    let mut hashes = Vec::new();
49
50    for file in files {
51        match file {
52            FileIdentifier::ID(id) => ids.push(id),
53            FileIdentifier::Hash(hash) => hashes.push(hash),
54        }
55    }
56    (ids, hashes)
57}