scaffolding_core/defaults.rs
1//! The defaults module provides the methods for creating deafult values
2//! for the Scaffolding common attributes
3
4use chrono::{DateTime, Duration, Months, Utc};
5use uuid::Uuid;
6
7/// generate the default value for access management
8///
9/// ```rust
10/// use scaffolding_core::defaults::*;
11///
12/// assert_eq!(access(), "public".to_string());
13/// ```
14pub fn access() -> String {
15 "public".to_string()
16}
17
18/// adds x days to the timestamp
19///
20/// ```rust
21/// use scaffolding_core::defaults::*;
22///
23/// assert_eq!(add_days(1711295319, 1), 1711381719);
24/// ```
25pub fn add_days(dtm: i64, days: i64) -> i64 {
26 let dt = DateTime::from_timestamp(dtm, 0).unwrap() + Duration::try_days(days).unwrap();
27 dt.timestamp()
28}
29
30/// adds x months to the timestamp
31///
32/// ```rust
33/// use scaffolding_core::defaults::*;
34///
35/// assert_eq!(add_months(1711295319, 1), 1713973719);
36/// ```
37pub fn add_months(dtm: i64, months: u32) -> i64 {
38 let dt = DateTime::from_timestamp(dtm, 0).unwrap() + Months::new(months);
39 dt.timestamp()
40}
41
42/// adds x years to the timestamp
43///
44/// ```rust
45/// use scaffolding_core::defaults::*;
46///
47/// assert_eq!(add_years(1711295319, 1), 1742831319);
48/// ```
49pub fn add_years(dtm: i64, years: u32) -> i64 {
50 let dt = DateTime::from_timestamp(dtm, 0).unwrap() + Months::new(years * 12);
51 dt.timestamp()
52}
53
54/// generates a uuid v4 value
55///
56/// ```rust
57/// use scaffolding_core::defaults::*;
58///
59/// assert_eq!(id().len(), "54324f57-9e6b-4142-b68d-1d4c86572d0a".len());
60/// ```
61pub fn id() -> String {
62 Uuid::new_v4().to_string()
63}
64
65/// provided the default unix epoch time (UTC) as seconds
66/// for the timestamp: 9999-12-31 23:59:59
67///
68/// ```rust
69/// use scaffolding_core::defaults::*;
70///
71/// assert_eq!(never(), 253402261199);
72/// ```
73pub fn never() -> i64 {
74 253402261199
75}
76
77/// generate the current unix epoch time (UTC) as seconds
78///
79/// ```rust
80/// use chrono::Utc;
81/// use scaffolding_core::defaults::*;
82///
83/// assert_eq!(now(), Utc::now().timestamp());
84/// ```
85pub fn now() -> i64 {
86 Utc::now().timestamp()
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn test_id() {
95 assert_eq!(id().len(), "54324f57-9e6b-4142-b68d-1d4c86572d0a".len());
96 }
97
98 #[test]
99 fn test_add_days() {
100 assert_eq!(add_days(1711295319, 1), 1711381719);
101 }
102
103 #[test]
104 fn test_add_months() {
105 assert_eq!(add_months(1711295319, 1), 1713973719);
106 // test for a leap year
107 // 2023-1-29 +1 = 2023-2-28
108 assert_eq!(add_months(1674993600, 1), 1677585600);
109 }
110
111 #[test]
112 fn test_add_years() {
113 assert_eq!(add_years(1711295319, 1), 1742831319);
114 // test for a leap year
115 // 2024-2-29 +1 = 2025-2-28
116 assert_eq!(add_years(1709208000, 1), 1740744000);
117 }
118
119 #[test]
120 fn test_never() {
121 assert_eq!(never(), 253402261199);
122 }
123
124 #[test]
125 fn test_now() {
126 assert_eq!(now(), Utc::now().timestamp());
127 }
128}