1use crate::Field;
2use chrono::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime};
3use json::{object, JsonValue};
4use std::time::{Duration, UNIX_EPOCH};
5
6pub struct Year {
7 pub require: bool,
8 pub field: String,
9 pub mode: String,
10 pub title: String,
11 pub def: String,
12 pub show: bool,
13 pub describe: String,
14 pub example: JsonValue,
15}
16
17impl Year {
18 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
19 Self {
20 field: field.to_string(),
21 mode: "year".to_string(),
22 title: title.to_string(),
23 def: default.to_string(),
24 require,
25 show: true,
26 describe: String::new(),
27 example: JsonValue::Null,
28 }
29 }
30 pub fn year() -> String {
31 let now: DateTime<Local> = Local::now();
32 let dft = now.format("%Y");
33 dft.to_string()
34 }
35 pub fn timestamp_to_year(timestamp: i64) -> String {
36 let d = UNIX_EPOCH + Duration::from_secs(timestamp as u64);
37 let datetime = DateTime::<Local>::from(d);
38 let timestamp_str = datetime.format("%Y").to_string();
39 timestamp_str
40 }
41}
42
43impl Field for Year {
44 fn sql(&mut self, model: &str) -> String {
45 let mut sql = format!("`{}`", self.field);
46 sql = format!("{} year", sql.clone());
47 if self.require {
48 sql = format!("{} ", sql.clone())
49 } else {
50 sql = format!("{} default '{}'", sql.clone(), self.def);
51 }
52 match model {
53 "sqlite" => sql,
54 "pgsql" => {
55 let mut sql = format!("{} year", self.field);
56 if self.require {
57 sql = format!("{} ", sql.clone())
58 } else {
59 sql = format!("{} default '{}'", sql.clone(), self.def);
60 }
61 format!(
62 "{} comment '{}|{}|{}|{}'",
63 sql.clone(),
64 self.title,
65 self.mode,
66 self.require,
67 self.def
68 )
69 }
70 _ => format!(
71 "{} comment '{}|{}|{}|{}'",
72 sql.clone(),
73 self.title,
74 self.mode,
75 self.require,
76 self.def
77 ),
78 }
79 }
80 fn hide(&mut self) -> &mut Self {
81 self.show = false;
82 self
83 }
84
85 fn describe(&mut self, text: &str) -> &mut Self {
86 self.describe = text.to_string();
87 self
88 }
89
90 fn field(&mut self) -> JsonValue {
91 let mut field = object! {};
92 field.insert("require", JsonValue::from(self.require)).unwrap();
93 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
94 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
95 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
96 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
97 field.insert("show", JsonValue::from(self.show)).unwrap();
98 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
99 field.insert("example", self.example.clone()).unwrap();
100 field
101 }
102
103 fn swagger(&mut self) -> JsonValue {
104 object! {
105 "type": self.mode.clone(),
106 "example": self.example.clone(),
107 }
108 }
109 fn example(&mut self, data: JsonValue) -> &mut Self {
110 self.example = data.clone();
111 self
112 }
113}
114
115pub struct Datetime {
116 pub require: bool,
117 pub field: String,
118 pub mode: String,
119 pub title: String,
120 pub def: String,
121 pub show: bool,
122 pub describe: String,
123 pub example: JsonValue,
124}
125
126impl Datetime {
127 pub fn new(require: bool, field: &str, title: &str, mut default: &str) -> Self {
128 if default.is_empty() {
129 default = "0001-01-01 00:00:00";
130 }
131 Self {
132 field: field.to_string(),
133 mode: "datetime".to_string(),
134 title: title.to_string(),
135 def: default.to_string(),
136 require,
137 show: true,
138 describe: String::new(),
139 example: JsonValue::Null,
140 }
141 }
142 pub fn datetime() -> String {
143 let now: DateTime<Local> = Local::now();
144 let dft = now.format("%Y-%m-%d %H:%M:%S");
145 dft.to_string()
146 }
147 pub fn timestamp_to_datetime(timestamp: i64) -> String {
148 let d = UNIX_EPOCH + Duration::from_secs(timestamp as u64);
149 let datetime = DateTime::<Local>::from(d);
150 let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string();
151 timestamp_str
152 }
153 pub fn datetime_format(format: &str) -> String {
154 let now: DateTime<Local> = Local::now();
155 let dft = now.format(format);
156 dft.to_string()
157 }
158}
159
160impl Field for Datetime {
161 fn sql(&mut self, model: &str) -> String {
162 let sql = format!("`{}` datetime default '{}'", self.field, self.def.clone());
163 match model {
164 "sqlite" => sql,
165 "pgsql" => {
166 let sql = format!(r#""{}" timestamp default '{}'"#, self.field, self.def.clone());
167 format!(
168 "{} comment '{}|{}|{}|{}'",
169 sql.clone(),
170 self.title,
171 self.mode,
172 self.require,
173 self.def
174 )
175 }
176 _ => format!(
177 "{} comment '{}|{}|{}|{}'",
178 sql.clone(),
179 self.title,
180 self.mode,
181 self.require,
182 self.def
183 ),
184 }
185 }
186 fn hide(&mut self) -> &mut Self {
187 self.show = false;
188 self
189 }
190 fn describe(&mut self, text: &str) -> &mut Self {
191 self.describe = text.to_string();
192 self
193 }
194
195 fn field(&mut self) -> JsonValue {
196 let mut field = object! {};
197 field.insert("require", JsonValue::from(self.require)).unwrap();
198 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
199 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
200 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
201 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
202
203 field.insert("show", JsonValue::from(self.show)).unwrap();
204 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
205 field.insert("example", self.example.clone()).unwrap();
206 field
207 }
208
209 fn swagger(&mut self) -> JsonValue {
210 object! {
211 "type": self.mode.clone(),
212 "example": self.example.clone(),
213 }
214 }
215 fn example(&mut self, data: JsonValue) -> &mut Self {
216 self.example = data.clone();
217 self
218 }
219}
220
221pub struct Time {
222 pub require: bool,
223 pub field: String,
224 pub mode: String,
225 pub title: String,
226 pub def: String,
227 pub show: bool,
228 pub describe: String,
229 pub example: JsonValue,
230}
231
232impl Time {
233 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
234 Self {
235 field: field.to_string(),
236 mode: "time".to_string(),
237 title: title.to_string(),
238 def: default.to_string(),
239 require,
240 show: true,
241 describe: String::new(),
242 example: JsonValue::Null,
243 }
244 }
245 pub fn time() -> String {
246 let now: DateTime<Local> = Local::now();
247 let dft = now.format("%H:%M:%S");
248 dft.to_string()
249 }
250}
251
252impl Field for Time {
253 fn sql(&mut self, model: &str) -> String {
254 let sql = format!("`{}` time default '{}'", self.field, self.def);
255 match model {
256 "sqlite" => sql,
257 "pgsql" => {
258 let sql = format!(r#""{}" time default '{}'"#, self.field, self.def.clone());
259 format!(
260 "{} comment '{}|{}|{}|{}'",
261 sql.clone(),
262 self.title,
263 self.mode,
264 self.require,
265 self.def
266 )
267 }
268 _ => format!(
269 "{} comment '{}|{}|{}|{}'",
270 sql.clone(),
271 self.title,
272 self.mode,
273 self.require,
274 self.def
275 ),
276 }
277 }
278 fn hide(&mut self) -> &mut Self {
279 self.show = false;
280 self
281 }
282
283 fn describe(&mut self, text: &str) -> &mut Self {
284 self.describe = text.to_string();
285 self
286 }
287
288 fn field(&mut self) -> JsonValue {
289 let mut field = object! {};
290 field.insert("require", JsonValue::from(self.require)).unwrap();
291 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
292 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
293 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
294 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
295
296 field.insert("show", JsonValue::from(self.show)).unwrap();
297 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
298 field.insert("example", self.example.clone()).unwrap();
299 field
300 }
301
302 fn swagger(&mut self) -> JsonValue {
303 object! {
304 "type": self.mode.clone(),
305 "example": self.example.clone(),
306 }
307 }
308 fn example(&mut self, data: JsonValue) -> &mut Self {
309 self.example = data.clone();
310 self
311 }
312}
313
314pub struct Date {
315 pub require: bool,
316 pub field: String,
317 pub mode: String,
318 pub title: String,
319 pub def: String,
320 pub show: bool,
321 pub describe: String,
322 pub example: JsonValue,
323}
324
325impl Date {
326 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
327 let def = {
328 if default.is_empty() {
329 "0001-01-01"
330 } else {
331 default
332 }
333 };
334 Self {
335 field: field.to_string(),
336 mode: "date".to_string(),
337 title: title.to_string(),
338 def: def.parse().unwrap(),
339 require,
340 show: true,
341 describe: "".to_string(),
342 example: JsonValue::Null,
343 }
344 }
345 pub fn date() -> String {
347 let now: DateTime<Local> = Local::now();
348 let dft = now.format("%Y-%m-%d");
349 dft.to_string()
350 }
351 pub fn timestamp_to_date(timestamp: i64) -> String {
352 let d = UNIX_EPOCH + Duration::from_secs(timestamp as u64);
353 let datetime = DateTime::<Local>::from(d);
354 let timestamp_str = datetime.format("%Y-%m-%d").to_string();
355 timestamp_str
356 }
357}
358
359impl Field for Date {
360 fn sql(&mut self, model: &str) -> String {
361 let sql = format!("`{}` date default '{}'", self.field, self.def);
362 match model {
363 "sqlite" => sql,
364 "pgsql" => {
365 let sql = format!(r#""{}" date default '{}'"#, self.field, self.def.clone());
366 format!(
367 "{} comment '{}|{}|{}|{}'",
368 sql.clone(),
369 self.title,
370 self.mode,
371 self.require,
372 self.def
373 )
374 }
375 _ => format!(
376 "{} comment '{}|{}|{}|{}'",
377 sql.clone(),
378 self.title,
379 self.mode,
380 self.require,
381 self.def
382 ),
383 }
384 }
385 fn hide(&mut self) -> &mut Self {
386 self.show = false;
387 self
388 }
389
390 fn describe(&mut self, text: &str) -> &mut Self {
391 self.describe = text.to_string();
392 self
393 }
394
395 fn field(&mut self) -> JsonValue {
396 let mut field = object! {};
397 field.insert("require", JsonValue::from(self.require)).unwrap();
398 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
399 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
400 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
401 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
402
403 field.insert("show", JsonValue::from(self.show)).unwrap();
404 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
405 field.insert("example", self.example.clone()).unwrap();
406 field
407 }
408 fn swagger(&mut self) -> JsonValue {
409 object! {
410 "type": self.mode.clone(),
411 "example": self.example.clone(),
412 }
413 }
414 fn example(&mut self, data: JsonValue) -> &mut Self {
415 self.example = data.clone();
416 self
417 }
418}
419
420pub struct Timestamp {
421 pub require: bool,
422 pub field: String,
423 pub mode: String,
424 pub title: String,
425 pub def: f64,
426 pub dec: i32,
427 pub show: bool,
428 pub describe: String,
429 pub example: JsonValue,
430}
431
432impl Timestamp {
433 pub fn new(require: bool, field: &str, title: &str, dec: i32, default: f64) -> Self {
434 Self {
435 require,
436 field: field.to_string(),
437 mode: "timestamp".to_string(),
438 title: title.to_string(),
439 def: default,
440 dec,
441 show: true,
442 describe: "".to_string(),
443 example: JsonValue::Null,
444 }
445 }
446 pub fn timestamp() -> i64 {
448 Local::now().timestamp()
449 }
450 pub fn timestamp_ms() -> i64 {
452 Local::now().timestamp_millis()
453 }
454 pub fn timestamp_ms_f64() -> f64 {
456 Local::now().timestamp_millis() as f64 / 1000.0
457 }
458 pub fn timestamp_μs() -> i64 {
460 Local::now().timestamp_micros()
461 }
462 pub fn timestamp_μs_f64() -> f64 {
464 Local::now().timestamp_micros() as f64 / 1000.0 / 1000.0
465 }
466 pub fn timestamp_ns() -> i64 {
468 Local::now().timestamp_nanos_opt().unwrap()
469 }
470
471 pub fn date_to_timestamp(date: &str) -> i64 {
473 let t = NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap();
474 let tz = FixedOffset::east_opt(Local::now().offset().local_minus_utc()).unwrap();
475 t.and_hms_opt(0, 0, 0).unwrap().and_local_timezone(tz).unwrap().timestamp()
476 }
477 pub fn datetime_to_timestamp(datetime: &str) -> i64 {
479 let t = NaiveDateTime::parse_from_str(datetime, "%Y-%m-%d %H:%M:%S").unwrap();
480 let tz = FixedOffset::east_opt(Local::now().offset().local_minus_utc()).unwrap();
481 t.and_local_timezone(tz).unwrap().timestamp()
482 }
483 pub fn datetime_to_rfc2822(datetime: &str) -> String {
485 let t = NaiveDateTime::parse_from_str(datetime, "%Y-%m-%d %H:%M:%S").unwrap();
486 let tz = FixedOffset::east_opt(Local::now().offset().local_minus_utc()).unwrap();
487 t.and_local_timezone(tz).unwrap().to_rfc2822()
488 }
489 pub fn datetime_utc_rfc2822(datetime: &str) -> String {
490 let t = NaiveDateTime::parse_from_str(datetime, "%Y-%m-%d %H:%M:%S").unwrap();
491 t.and_utc().to_rfc2822()
492 }
493 pub fn datetime_to_fmt(datetime: &str, fmt: &str) -> String {
494 let t = NaiveDateTime::parse_from_str(datetime, "%Y-%m-%d %H:%M:%S").unwrap();
495 let tz = FixedOffset::east_opt(Local::now().offset().local_minus_utc()).unwrap();
496 t.and_local_timezone(tz).unwrap().format(fmt).to_string()
497 }
498}
499
500impl Field for Timestamp {
501 fn sql(&mut self, model: &str) -> String {
502 let max = 10 + self.dec;
503 match model {
504 "sqlite" => {
505 let mut sql = format!("`{}` float({},{}) ", self.field, max, self.dec);
506 self.def = format!("{0:.width$}", self.def, width = self.dec as usize).parse::<f64>().unwrap();
507 sql = format!("{} default {}", sql.clone(), self.def);
508 sql
509 }
510 "pgsql" => {
511 let mut sql = format!(r#""{}" decimal({},{})"#, self.field, max, self.dec);
512 let def = format!("{0:.width$}", self.def, width = self.dec as usize);
513 self.def = def.parse::<f64>().unwrap();
514 sql = format!("{} default {}", sql.clone(), self.def);
515 format!(
516 "{} comment '{}|{}|{}|{}|{}'",
517 sql.clone(),
518 self.title,
519 self.mode,
520 self.require,
521 self.dec,
522 self.def
523 )
524 }
525 _ => {
526 let mut sql = format!("`{}` decimal({},{}) ", self.field, max, self.dec);
527 let def = format!("{0:.width$}", self.def, width = self.dec as usize);
528 self.def = def.parse::<f64>().unwrap();
529 sql = format!("{} default {}", sql.clone(), self.def);
530 format!(
531 "{} comment '{}|{}|{}|{}|{}'",
532 sql.clone(),
533 self.title,
534 self.mode,
535 self.require,
536 self.dec,
537 self.def
538 )
539 }
540 }
541 }
542 fn hide(&mut self) -> &mut Self {
543 self.show = false;
544 self
545 }
546
547 fn describe(&mut self, text: &str) -> &mut Self {
548 self.describe = text.to_string();
549 self
550 }
551
552 fn field(&mut self) -> JsonValue {
553 let mut field = object! {};
554 field.insert("require", JsonValue::from(self.require)).unwrap();
555 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
556 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
557 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
558 field.insert("def", JsonValue::from(self.def)).unwrap();
559 field.insert("dec", JsonValue::from(self.dec)).unwrap();
560 field.insert("show", JsonValue::from(self.show)).unwrap();
561 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
562 field.insert("example", self.example.clone()).unwrap();
563 field
564 }
565
566 fn swagger(&mut self) -> JsonValue {
567 object! {
568 "type": self.mode.clone(),
569 "example": self.example.clone(),
570 }
571 }
572
573 fn example(&mut self, data: JsonValue) -> &mut Self {
574 self.example = data.clone();
575 self
576 }
577}