epcis-models 0.1.0

Type-safe GS1 EPCIS 2.0 event models implemented natively in Rust.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! EPCIS 2.0 event model definitions.

#![deny(missing_docs)]
#![deny(unsafe_code)]
#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]

use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use crate::types::{
    Action, BizLocation, BizTransaction, Destination, Epc,
    ErrorDeclaration, QuantityElement, ReadPoint, SensorElement, Source,
};
use crate::cbv::{BizStep, Disposition};

/// Persistent disposition settings for tracking updates in an `ObjectEvent`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PersistentDisposition {
    /// List of dispositions to unset
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unset: Option<Vec<Disposition>>,
    /// List of dispositions to set
    #[serde(skip_serializing_if = "Option::is_none")]
    pub set: Option<Vec<Disposition>>,
}

/// An `ObjectEvent` captures an observation, addition, or deletion of specific items.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ObjectEvent {
    /// Date and time when the event occurred
    pub event_time: DateTime<Utc>,
    /// Timezone offset (e.g. "+01:00")
    pub event_time_zone_offset: String,
    /// Record timestamp (optional, set by repository)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_time: Option<DateTime<Utc>>,
    /// Event identifier (often URN like urn:uuid)
    #[serde(rename = "eventID", skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    /// Error declaration for corrective events
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_declaration: Option<ErrorDeclaration>,
    
    /// Action specifying lifecycle state change
    pub action: Action,
    /// List of item EPCs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub epc_list: Option<Vec<Epc>>,
    /// List of class/quantity items
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantity_list: Option<Vec<QuantityElement>>,
    
    /// Business step (e.g. receiving)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_step: Option<BizStep>,
    /// Item disposition (e.g. `in_transit`)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<Disposition>,
    /// Read point identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub read_point: Option<ReadPoint>,
    /// Business location identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_location: Option<BizLocation>,
    /// List of business transactions linked to the event
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_transaction_list: Option<Vec<BizTransaction>>,
    /// List of source identifiers
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_list: Option<Vec<Source>>,
    /// List of destination identifiers
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination_list: Option<Vec<Destination>>,
    /// List of sensor records
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sensor_element_list: Option<Vec<SensorElement>>,
    /// Persistent state changes of disposition
    #[serde(skip_serializing_if = "Option::is_none")]
    pub persistent_disposition: Option<PersistentDisposition>,

    /// Extra custom JSON elements
    #[serde(flatten)]
    pub extensions: serde_json::Map<String, serde_json::Value>,
}

impl ObjectEvent {
    /// Creates a new `ObjectEvent` with required properties.
    #[must_use]
    pub fn new(event_time: DateTime<Utc>, tz_offset: String, action: Action) -> Self {
        Self {
            event_time,
            event_time_zone_offset: tz_offset,
            record_time: None,
            event_id: None,
            error_declaration: None,
            action,
            epc_list: None,
            quantity_list: None,
            biz_step: None,
            disposition: None,
            read_point: None,
            biz_location: None,
            biz_transaction_list: None,
            source_list: None,
            destination_list: None,
            sensor_element_list: None,
            persistent_disposition: None,
            extensions: serde_json::Map::new(),
        }
    }
}

/// An `AggregationEvent` captures an event where objects are aggregated into a parent container.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AggregationEvent {
    /// Date and time when the event occurred
    pub event_time: DateTime<Utc>,
    /// Timezone offset
    pub event_time_zone_offset: String,
    /// Record timestamp (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_time: Option<DateTime<Utc>>,
    /// Event identifier
    #[serde(rename = "eventID", skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    /// Error declaration details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_declaration: Option<ErrorDeclaration>,
    
    /// Action specifying parent-child state change (ADD, DELETE, OBSERVE)
    pub action: Action,
    /// Parent container EPC
    #[serde(rename = "parentID", skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<Epc>,
    /// List of child EPCs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub child_e_p_cs: Option<Vec<Epc>>,
    /// List of child quantities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub child_quantity_list: Option<Vec<QuantityElement>>,
    
    /// Business step (e.g. packing)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_step: Option<BizStep>,
    /// Disposition
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<Disposition>,
    /// Read point
    #[serde(skip_serializing_if = "Option::is_none")]
    pub read_point: Option<ReadPoint>,
    /// Business location
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_location: Option<BizLocation>,
    /// Business transactions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_transaction_list: Option<Vec<BizTransaction>>,
    /// Source list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_list: Option<Vec<Source>>,
    /// Destination list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination_list: Option<Vec<Destination>>,
    /// Sensor element list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sensor_element_list: Option<Vec<SensorElement>>,

    /// Extra custom fields
    #[serde(flatten)]
    pub extensions: serde_json::Map<String, serde_json::Value>,
}

impl AggregationEvent {
    /// Creates a new `AggregationEvent` with required properties.
    #[must_use]
    pub fn new(event_time: DateTime<Utc>, tz_offset: String, action: Action) -> Self {
        Self {
            event_time,
            event_time_zone_offset: tz_offset,
            record_time: None,
            event_id: None,
            error_declaration: None,
            action,
            parent_id: None,
            child_e_p_cs: None,
            child_quantity_list: None,
            biz_step: None,
            disposition: None,
            read_point: None,
            biz_location: None,
            biz_transaction_list: None,
            source_list: None,
            destination_list: None,
            sensor_element_list: None,
            extensions: serde_json::Map::new(),
        }
    }
}

/// A `TransformationEvent` captures the consumption of input items and the creation of output items.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransformationEvent {
    /// Date and time when the event occurred
    pub event_time: DateTime<Utc>,
    /// Timezone offset
    pub event_time_zone_offset: String,
    /// Record timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_time: Option<DateTime<Utc>>,
    /// Event identifier
    #[serde(rename = "eventID", skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    /// Error declaration details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_declaration: Option<ErrorDeclaration>,
    
    /// Input item EPCs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_e_p_c_list: Option<Vec<Epc>>,
    /// Input item quantities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_quantity_list: Option<Vec<QuantityElement>>,
    /// Output item EPCs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_e_p_c_list: Option<Vec<Epc>>,
    /// Output item quantities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_quantity_list: Option<Vec<QuantityElement>>,
    /// Unique transformation logic identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transformation_id: Option<String>,
    
    /// Business step
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_step: Option<BizStep>,
    /// Disposition
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<Disposition>,
    /// Read point
    #[serde(skip_serializing_if = "Option::is_none")]
    pub read_point: Option<ReadPoint>,
    /// Business location
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_location: Option<BizLocation>,
    /// Business transactions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_transaction_list: Option<Vec<BizTransaction>>,
    /// Source list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_list: Option<Vec<Source>>,
    /// Destination list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination_list: Option<Vec<Destination>>,
    /// Sensor element list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sensor_element_list: Option<Vec<SensorElement>>,

    /// Extra custom fields
    #[serde(flatten)]
    pub extensions: serde_json::Map<String, serde_json::Value>,
}

impl TransformationEvent {
    /// Creates a new `TransformationEvent` with required properties.
    #[must_use]
    pub fn new(event_time: DateTime<Utc>, tz_offset: String) -> Self {
        Self {
            event_time,
            event_time_zone_offset: tz_offset,
            record_time: None,
            event_id: None,
            error_declaration: None,
            input_e_p_c_list: None,
            input_quantity_list: None,
            output_e_p_c_list: None,
            output_quantity_list: None,
            transformation_id: None,
            biz_step: None,
            disposition: None,
            read_point: None,
            biz_location: None,
            biz_transaction_list: None,
            source_list: None,
            destination_list: None,
            sensor_element_list: None,
            extensions: serde_json::Map::new(),
        }
    }
}

/// An `AssociationEvent` captures the assembly or disassembly of reusable assets.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AssociationEvent {
    /// Date and time when the event occurred
    pub event_time: DateTime<Utc>,
    /// Timezone offset
    pub event_time_zone_offset: String,
    /// Record timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_time: Option<DateTime<Utc>>,
    /// Event identifier
    #[serde(rename = "eventID", skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    /// Error declaration details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_declaration: Option<ErrorDeclaration>,
    
    /// Action specifying parent-child state change (ADD, DELETE, OBSERVE)
    pub action: Action,
    /// Parent container EPC
    #[serde(rename = "parentID", skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<Epc>,
    /// List of child EPCs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub child_e_p_cs: Option<Vec<Epc>>,
    /// List of child quantities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub child_quantity_list: Option<Vec<QuantityElement>>,
    
    /// Business step
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_step: Option<BizStep>,
    /// Disposition
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<Disposition>,
    /// Read point
    #[serde(skip_serializing_if = "Option::is_none")]
    pub read_point: Option<ReadPoint>,
    /// Business location
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_location: Option<BizLocation>,
    /// Business transactions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_transaction_list: Option<Vec<BizTransaction>>,
    /// Source list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_list: Option<Vec<Source>>,
    /// Destination list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination_list: Option<Vec<Destination>>,
    /// Sensor element list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sensor_element_list: Option<Vec<SensorElement>>,

    /// Extra custom fields
    #[serde(flatten)]
    pub extensions: serde_json::Map<String, serde_json::Value>,
}

impl AssociationEvent {
    /// Creates a new `AssociationEvent` with required properties.
    #[must_use]
    pub fn new(event_time: DateTime<Utc>, tz_offset: String, action: Action) -> Self {
        Self {
            event_time,
            event_time_zone_offset: tz_offset,
            record_time: None,
            event_id: None,
            error_declaration: None,
            action,
            parent_id: None,
            child_e_p_cs: None,
            child_quantity_list: None,
            biz_step: None,
            disposition: None,
            read_point: None,
            biz_location: None,
            biz_transaction_list: None,
            source_list: None,
            destination_list: None,
            sensor_element_list: None,
            extensions: serde_json::Map::new(),
        }
    }
}

/// A `TransactionEvent` captures items associated with a business transaction.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionEvent {
    /// Date and time when the event occurred
    pub event_time: DateTime<Utc>,
    /// Timezone offset
    pub event_time_zone_offset: String,
    /// Record timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_time: Option<DateTime<Utc>>,
    /// Event identifier
    #[serde(rename = "eventID", skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    /// Error declaration details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_declaration: Option<ErrorDeclaration>,
    
    /// Action specifying parent-child state change
    pub action: Action,
    /// Parent identifier
    #[serde(rename = "parentID", skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<Epc>,
    /// List of item EPCs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub epc_list: Option<Vec<Epc>>,
    /// List of quantities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantity_list: Option<Vec<QuantityElement>>,
    
    /// Business step
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_step: Option<BizStep>,
    /// Disposition
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<Disposition>,
    /// Read point
    #[serde(skip_serializing_if = "Option::is_none")]
    pub read_point: Option<ReadPoint>,
    /// Business location
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_location: Option<BizLocation>,
    /// Business transactions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_transaction_list: Option<Vec<BizTransaction>>,
    /// Source list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_list: Option<Vec<Source>>,
    /// Destination list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination_list: Option<Vec<Destination>>,
    /// Sensor element list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sensor_element_list: Option<Vec<SensorElement>>,

    /// Extra custom fields
    #[serde(flatten)]
    pub extensions: serde_json::Map<String, serde_json::Value>,
}

impl TransactionEvent {
    /// Creates a new `TransactionEvent` with required properties.
    #[must_use]
    pub fn new(event_time: DateTime<Utc>, tz_offset: String, action: Action) -> Self {
        Self {
            event_time,
            event_time_zone_offset: tz_offset,
            record_time: None,
            event_id: None,
            error_declaration: None,
            action,
            parent_id: None,
            epc_list: None,
            quantity_list: None,
            biz_step: None,
            disposition: None,
            read_point: None,
            biz_location: None,
            biz_transaction_list: None,
            source_list: None,
            destination_list: None,
            sensor_element_list: None,
            extensions: serde_json::Map::new(),
        }
    }
}

/// Sum-type representing any EPCIS event.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum EPCISEvent {
    /// An `ObjectEvent` variant.
    ObjectEvent(ObjectEvent),
    /// An `AggregationEvent` variant.
    AggregationEvent(AggregationEvent),
    /// A `TransformationEvent` variant.
    TransformationEvent(TransformationEvent),
    /// An `AssociationEvent` variant.
    AssociationEvent(AssociationEvent),
    /// A `TransactionEvent` variant.
    TransactionEvent(TransactionEvent),
}