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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! The defaults module provides the methods for creating deafult values
//! for the Scaffolding common attributes

use chrono::{DateTime, Duration, Months, Utc};
use uuid::Uuid;

/// generate the default value for access management
///
/// ```rust
/// use scaffolding_core::defaults::*;
///
/// assert_eq!(access(), "public".to_string());
/// ```
pub fn access() -> String {
    "public".to_string()
}

/// adds x days to the timestamp
///
/// ```rust
/// use scaffolding_core::defaults::*;
///
/// assert_eq!(add_days(1711295319, 1), 1711381719);
/// ```
pub fn add_days(dtm: i64, days: i64) -> i64 {
    let dt = DateTime::from_timestamp(dtm, 0).unwrap() + Duration::try_days(days).unwrap();
    dt.timestamp()
}

/// adds x months to the timestamp
///
/// ```rust
/// use scaffolding_core::defaults::*;
///
/// assert_eq!(add_months(1711295319, 1), 1713973719);
/// ```
pub fn add_months(dtm: i64, months: u32) -> i64 {
    let dt = DateTime::from_timestamp(dtm, 0).unwrap() + Months::new(months);
    dt.timestamp()
}

/// adds x years to the timestamp
///
/// ```rust
/// use scaffolding_core::defaults::*;
///
/// assert_eq!(add_years(1711295319, 1), 1742831319);
/// ```
pub fn add_years(dtm: i64, years: u32) -> i64 {
    let dt = DateTime::from_timestamp(dtm, 0).unwrap() + Months::new(years * 12);
    dt.timestamp()
}

/// generates a uuid v4 value
///
/// ```rust
/// use scaffolding_core::defaults::*;
///
/// assert_eq!(id().len(), "54324f57-9e6b-4142-b68d-1d4c86572d0a".len());
/// ```
pub fn id() -> String {
    Uuid::new_v4().to_string()
}

/// provided the default unix epoch time (UTC) as seconds
/// for the timestamp: 9999-12-31 23:59:59
///
/// ```rust
/// use scaffolding_core::defaults::*;
///
/// assert_eq!(never(), 253402261199);
/// ```
pub fn never() -> i64 {
    253402261199
}

/// generate the current unix epoch time (UTC) as seconds
///
/// ```rust
/// use chrono::Utc;
/// use scaffolding_core::defaults::*;
///
/// assert_eq!(now(), Utc::now().timestamp());
/// ```
pub fn now() -> i64 {
    Utc::now().timestamp()
}

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

    #[test]
    fn test_id() {
        assert_eq!(id().len(), "54324f57-9e6b-4142-b68d-1d4c86572d0a".len());
    }

    #[test]
    fn test_add_days() {
        assert_eq!(add_days(1711295319, 1), 1711381719);
    }

    #[test]
    fn test_add_months() {
        assert_eq!(add_months(1711295319, 1), 1713973719);
        // test for a leap year
        // 2023-1-29 +1 = 2023-2-28
        assert_eq!(add_months(1674993600, 1), 1677585600);
    }

    #[test]
    fn test_add_years() {
        assert_eq!(add_years(1711295319, 1), 1742831319);
        // test for a leap year
        // 2024-2-29 +1 = 2025-2-28
        assert_eq!(add_years(1709208000, 1), 1740744000);
    }

    #[test]
    fn test_never() {
        assert_eq!(never(), 253402261199);
    }

    #[test]
    fn test_now() {
        assert_eq!(now(), Utc::now().timestamp());
    }
}