crcnt_ddd 0.2.11

CRCNT DDD Basic Objects
Documentation
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use {chrono::{DateTime,
              Duration,
              NaiveDateTime,
              Utc},
     crcnt_ddd_macros::Domain,
     mysql_common::value::{convert::{ConvIr,
                                     FromValue,
                                     FromValueError},
                           Value},
     std::{cmp::Ordering,
           ops},
     ulid::Ulid};

//<editor-fold desc="CreateAt Def">
/// CreateAt is a timestamp in milliseconds like it in Java
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct CreateAt(UtcDateTime);

impl CreateAt {
  pub fn now() -> Self { Self(UtcDateTime::now()) }

  pub fn timestamp(&self) -> i64 { *&self.0.0.timestamp() }

  pub fn timestamp_millis(&self) -> i64 { *&self.0.0.timestamp_millis() }

  pub fn naive_date_time(&self) -> NaiveDateTime { self.0.0.naive_utc() }
}
//</editor-fold>

//<editor-fold desc="UpdateAt Def">
/// UpdateAt is a timestamp in milliseconds like it in Java
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct UpdateAt(UtcDateTime);

impl UpdateAt {
  pub fn now() -> Self { Self(UtcDateTime::now()) }

  pub fn timestamp(&self) -> i64 { *&self.0.0.timestamp() }

  pub fn timestamp_millis(&self) -> i64 { *&self.0.0.timestamp_millis() }

  pub fn naive_date_time(&self) -> NaiveDateTime { self.0.0.naive_utc() }
}
//</editor-fold>

//<editor-fold desc="AvailableSince Def">
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct AvailableSince(UtcDateTime);

impl AvailableSince {
  pub fn now() -> Self { Self(UtcDateTime::now()) }

  pub fn timestamp(&self) -> i64 { *&self.0.0.timestamp() }

  pub fn timestamp_millis(&self) -> i64 { *&self.0.0.timestamp_millis() }

  pub fn from_rfc3399<S: AsRef<str>>(s: S) -> Result<Self, String> { Ok(AvailableSince(UtcDateTime::from_rfc3339(s)?)) }

  pub fn is_available_now(&self) -> bool {
    let now = Utc::now();
    &self.0.0 <= &now
  }

  pub fn naive_date_time(&self) -> NaiveDateTime { self.0.0.naive_utc() }
}
//</editor-fold>

//<editor-fold desc="ExpiredSince Def">
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct ExpiredSince(UtcDateTime);

impl ExpiredSince {
  pub fn now() -> Self { Self(UtcDateTime::now()) }

  pub fn timestamp(&self) -> i64 { *&self.0.0.timestamp() }

  pub fn timestamp_millis(&self) -> i64 { *&self.0.0.timestamp_millis() }

  pub fn from_rfc3399<S: AsRef<str>>(s: S) -> Result<Self, String> { Ok(ExpiredSince(UtcDateTime::from_rfc3339(s)?)) }

  pub fn is_expired_now(&self) -> bool {
    let now = Utc::now();
    &self.inner().0 <= &now
  }

  pub fn naive_date_time(&self) -> NaiveDateTime { self.0.0.naive_utc() }
}
//</editor-fold>

//<editor-fold desc="Basic String Value">
/// Creator
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct Creator(String);

/// Owner
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct Owner(String);

/// Owner
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct EntityId(String);
#[derive(Debug)]
pub struct EntityIdIr(pub EntityId);
/// Updater
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct Updater(String);
//</editor-fold>

//<editor-fold desc="UtcDateTime Def">
#[derive(Debug, Clone)]
pub struct UtcDateTime(DateTime<Utc>);

impl UtcDateTime {
  pub fn now() -> Self { UtcDateTime(Utc::now()) }

  pub fn from_rfc3339<S: AsRef<str>>(s: S) -> Result<Self, String> {
    let date_time = DateTime::parse_from_rfc3339(s.as_ref()).map_err(|e| format!("AvailableSince parse error: {}", e.to_string()))?;
    let date_time = date_time.with_timezone(&Utc);
    Ok(Self(date_time))
  }

  pub fn from_timestamp(timestamp: i64) -> Option<Self> {
    let naive = NaiveDateTime::from_timestamp_opt(timestamp, 0);
    if let Some(naive) = naive {
      let date_time = DateTime::<Utc>::from_utc(naive, Utc);
      Some(Self(date_time))
    } else {
      None
    }
  }

  pub fn from_timestamp_millis(timestamp: i64) -> Option<Self> {
    let naive = NaiveDateTime::from_timestamp_millis(timestamp);
    if let Some(naive) = naive {
      let date_time = DateTime::<Utc>::from_utc(naive, Utc);
      Some(Self(date_time))
    } else {
      None
    }
  }

  pub fn timestamp(&self) -> i64 { *&self.0.timestamp() }

  pub fn timestamp_millis(&self) -> i64 { *&self.0.timestamp_millis() }

  pub fn naive_date_time(&self) -> NaiveDateTime { self.0.naive_utc() }
}
//</editor-fold>

#[derive(Debug, Clone)]
pub struct Amount(f64);
impl From<f64> for Amount {
  fn from(value: f64) -> Self { Amount((value * 100.00).round() / 100.00) }
}
impl Amount {
  pub fn new<T: Into<f64>>(v: T) -> Self {
    let x: f64 = v.into();
    From::from(x)
  }

  pub fn inner(&self) -> &f64 { &self.0 }

  pub fn into_inner(self) -> f64 { self.0 }
}
impl From<Amount> for Value {
  fn from(value: Amount) -> Self { Value::from(value.0) }
}
#[derive(Debug)]
pub struct F64Ir {
  v: f64,
}
impl ConvIr<Amount> for F64Ir {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let v = f64::from_value_opt(v)?;
    Ok(Self { v })
  }

  fn commit(self) -> Amount { Amount(self.v) }

  fn rollback(self) -> Value { Value::from(self.v) }
}
impl FromValue for Amount {
  type Intermediate = F64Ir;
}

//<editor-fold desc="ConvIr<T> for TimestampIr">
#[derive(Debug)]
pub struct TimestampIr {
  ndt: NaiveDateTime,
}
impl ConvIr<UtcDateTime> for TimestampIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let ndt = NaiveDateTime::from_value_opt(v)?;
    Ok(Self { ndt })
  }

  fn commit(self) -> UtcDateTime {
    let dt = DateTime::from_utc(self.ndt, Utc);
    UtcDateTime(dt)
  }

  fn rollback(self) -> Value { Value::from(self.ndt) }
}

impl ConvIr<CreateAt> for TimestampIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let ndt = NaiveDateTime::from_value_opt(v)?;
    Ok(Self { ndt })
  }

  fn commit(self) -> CreateAt {
    let dt = DateTime::from_utc(self.ndt, Utc);
    CreateAt(UtcDateTime(dt))
  }

  fn rollback(self) -> Value { Value::from(self.ndt) }
}

impl ConvIr<UpdateAt> for TimestampIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let ndt = NaiveDateTime::from_value_opt(v)?;
    Ok(Self { ndt })
  }

  fn commit(self) -> UpdateAt {
    let dt = DateTime::from_utc(self.ndt, Utc);
    UpdateAt(UtcDateTime(dt))
  }

  fn rollback(self) -> Value { Value::from(self.ndt) }
}

impl ConvIr<AvailableSince> for TimestampIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let ndt = NaiveDateTime::from_value_opt(v)?;
    Ok(Self { ndt })
  }

  fn commit(self) -> AvailableSince {
    let dt = DateTime::from_utc(self.ndt, Utc);
    AvailableSince(UtcDateTime(dt))
  }

  fn rollback(self) -> Value { Value::from(self.ndt) }
}

impl ConvIr<ExpiredSince> for TimestampIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let ndt = NaiveDateTime::from_value_opt(v)?;
    Ok(Self { ndt })
  }

  fn commit(self) -> ExpiredSince {
    let dt = DateTime::from_utc(self.ndt, Utc);
    ExpiredSince(UtcDateTime(dt))
  }

  fn rollback(self) -> Value { Value::from(self.ndt) }
}
//</editor-fold>

//<editor-fold desc="Deleted and ConvIr">
/// Deleted is a logic deletion flag
#[derive(Debug, Clone, Domain)]
#[domain_commands(value)]
pub struct Deleted(bool);

#[derive(Debug)]
pub struct DeletedIr {
  b: bool,
}

impl ConvIr<Deleted> for DeletedIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let b = bool::from_value_opt(v)?;
    Ok(Self { b })
  }

  fn commit(self) -> Deleted { Deleted(self.b) }

  fn rollback(self) -> Value { Value::from(self.b) }
}
//</editor-fold>

//<editor-fold desc="ConvIr<T> for StrIr">
#[derive(Debug)]
pub struct StrIr {
  pub bytes: Vec<u8>,
}
impl ConvIr<Creator> for StrIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let bytes = Vec::<u8>::from_value_opt(v)?;
    Ok(StrIr { bytes })
  }

  fn commit(self) -> Creator {
    let creator = String::from_utf8_lossy(&self.bytes).to_string();
    Creator(creator)
  }

  fn rollback(self) -> Value { Value::from(self.bytes) }
}
impl ConvIr<Updater> for StrIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let bytes = Vec::<u8>::from_value_opt(v)?;
    Ok(StrIr { bytes })
  }

  fn commit(self) -> Updater {
    let updater = String::from_utf8_lossy(&self.bytes).to_string();
    Updater(updater)
  }

  fn rollback(self) -> Value { Value::from(self.bytes) }
}
impl ConvIr<Owner> for StrIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let bytes = Vec::<u8>::from_value_opt(v)?;
    Ok(StrIr { bytes })
  }

  fn commit(self) -> Owner {
    let owner = String::from_utf8_lossy(&self.bytes).to_string();
    Owner(owner)
  }

  fn rollback(self) -> Value { Value::from(self.bytes) }
}
impl ConvIr<EntityId> for StrIr {
  fn new(v: Value) -> Result<Self, FromValueError> {
    let bytes = Vec::<u8>::from_value_opt(v)?;
    Ok(StrIr { bytes })
  }

  fn commit(self) -> EntityId {
    let id = String::from_utf8_lossy(&self.bytes).to_string();
    EntityId(id)
  }

  fn rollback(self) -> Value { Value::from(self.bytes) }
}
//</editor-fold>

//<editor-fold desc="impl FromValue">
impl FromValue for CreateAt {
  type Intermediate = TimestampIr;
}
impl FromValue for UpdateAt {
  type Intermediate = TimestampIr;
}
impl FromValue for AvailableSince {
  type Intermediate = TimestampIr;
}
impl FromValue for ExpiredSince {
  type Intermediate = TimestampIr;
}
impl FromValue for UtcDateTime {
  type Intermediate = TimestampIr;
}
impl FromValue for Deleted {
  type Intermediate = DeletedIr;
}
impl FromValue for Creator {
  type Intermediate = StrIr;
}
impl FromValue for Updater {
  type Intermediate = StrIr;
}
impl FromValue for Owner {
  type Intermediate = StrIr;
}
impl FromValue for EntityId {
  type Intermediate = StrIr;
}
//</editor-fold>

//<editor-fold desc="From<&T> for Value">
impl From<&UtcDateTime> for Value {
  fn from(x: &UtcDateTime) -> Self {
    let naive = x.0.naive_utc();
    Value::from(naive)
  }
}

//</editor-fold>

impl EntityId {
  pub fn new_with_prefix<T: AsRef<str>>(prefix: T) -> Self {
    let id = format!("{}{}", prefix.as_ref(), Ulid::new().to_string());
    EntityId::new(id)
  }
}

impl ops::Add<Duration> for UtcDateTime {
  type Output = UtcDateTime;

  fn add(self, rhs: Duration) -> Self::Output {
    let dt = self.0 + rhs;
    UtcDateTime(dt)
  }
}

impl ops::Sub for UtcDateTime {
  type Output = Duration;

  fn sub(self, rhs: Self) -> Self::Output { self.0 - rhs.0 }
}

impl PartialEq for UtcDateTime {
  fn eq(&self, other: &Self) -> bool { (&self.0).eq(&other.0) }
}
impl PartialOrd for UtcDateTime {
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> { (&self.0).partial_cmp(&other.0) }
}

#[cfg(test)]
mod test {
  use crate::value::Amount;

  #[test]
  fn test_amount() {
    let amt = Amount::new(0.64523);
    println!("amt = {:?}", amt);
  }
}