summary refs log tree commit diff stats
path: root/src/new/replacement.rs
blob: 0e9f27ffb4ab229446fa1222b37ce3f085a89521 (plain) (blame)
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
use std::time::{SystemTime, UNIX_EPOCH};

use chrono::{Local, TimeDelta, TimeZone};
use log::debug;

use crate::constants::{
    DATE, REPLACEMENT_CHAPTER, REPLACEMENT_CHAPTER_SECTION, REPLACEMENT_FIGURE, REPLACMENT_SECTION,
};

fn get_current_date() -> String {
    let start = SystemTime::now();
    let seconds_since_epoch: TimeDelta = TimeDelta::from_std(
        start
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards"),
    )
    .expect("Time does not go backwards");

    debug!(
        "Adding a date with timestamp: {}",
        seconds_since_epoch.num_seconds()
    );

    let our_date = format!(
        "{}",
        Local
            .timestamp_opt(seconds_since_epoch.num_seconds(), 0)
            // only has unwrap, no expect. But should always work
            .unwrap()
            .format("%Y-%m-%d %H:%M:%S%.f %:z")
    );

    our_date
}

pub fn untemplatize_section(input: &str, new_section_name: &str, new_chapter_name: &str) -> String {
    input
        .replace(REPLACEMENT_CHAPTER_SECTION, &new_chapter_name)
        .replace(REPLACMENT_SECTION, &new_section_name)
        .replace(DATE, &get_current_date())
}

pub fn untemplatize_chapter(input: &str, new_chapter_name: &str) -> String {
    input
        .replace(REPLACEMENT_CHAPTER, &new_chapter_name)
        .replace(DATE, &get_current_date())
}
pub fn untemplatize_figure(input: &str, new_figure_name: &str) -> String {
    input
        .replace(REPLACEMENT_FIGURE, &new_figure_name)
        .replace(DATE, &get_current_date())
}