oicp-kit 0.1.0

OICP (Open InterCharge Protocol) toolkit for EV roaming through Hubject: spec-exact typed models for OICP 2.3, mTLS client, axum server, EVSE delta sync, CDR pre-flight and a Hubject broker simulator.
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Charging notifications — telling the EMP what is happening while it happens.

use serde::{Deserialize, Serialize};

use super::cdr::MeterValuesInBetween;
use crate::types::{
    DateTime, EvseId, Extensions, Identification, Number, OperatorId, PartnerSessionId, SessionId, Text,
    Validate, Validator, ViolationCode, strict_builder, validate_fields,
};
use crate::{oicp_enum, oicp_open_enum};

oicp_enum! {
    /// Which of the four charging notifications this is.
    ///
    /// The discriminator on the shared `charging-notifications` endpoint. Closed, because it is
    /// what selects the shape of the rest of the body — an unrecognised value leaves nothing to
    /// decode.
    pub enum ChargingNotificationType {
        /// Energy has started flowing.
        Start = "Start",
        /// The session is running; here is how far it has got.
        Progress = "Progress",
        /// Energy has stopped flowing, but the session is still open.
        End = "End",
        /// Something went wrong.
        Error = "Error",
    }
}

oicp_open_enum! {
    /// What went wrong at the charging point.
    pub enum ErrorType {
        /// The process cannot start or stop; the driver should check the plug.
        ConnectorError = "ConnectorError",
        /// The process stopped abruptly and the station needs a physical check.
        CriticalError = "CriticalError",
    }
}

/// Energy has started flowing.
///
/// The spec is careful about why this exists: authorization and plugging in *do not* mean the car
/// is charging. Without this notification an EMP cannot tell a driver whether their car is
/// actually taking energy.
///
/// Spec: `eRoamingChargingNotifications Start`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, bon::Builder)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[builder(finish_fn = build_unchecked)]
pub struct ChargingNotificationStart {
    /// Always [`ChargingNotificationType::Start`].
    #[serde(rename = "Type")]
    pub notification_type: ChargingNotificationType,
    /// The session.
    #[serde(rename = "SessionID")]
    pub session_id: SessionId,
    /// The CPO's own session id.
    #[serde(rename = "CPOPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub cpo_partner_session_id: Option<PartnerSessionId>,
    /// The EMP's own session id.
    #[serde(rename = "EMPPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub emp_partner_session_id: Option<PartnerSessionId>,
    /// Who is charging.
    #[serde(rename = "Identification", default, skip_serializing_if = "Option::is_none")]
    pub identification: Option<Identification>,
    /// Where.
    #[serde(rename = "EvseID")]
    pub evse_id: EvseId,
    /// When energy started flowing.
    #[serde(rename = "ChargingStart")]
    pub charging_start: DateTime,
    /// When the session started.
    #[serde(rename = "SessionStart", default, skip_serializing_if = "Option::is_none")]
    pub session_start: Option<DateTime>,
    /// The meter reading at the start, in kWh.
    #[serde(rename = "MeterValueStart", default, skip_serializing_if = "Option::is_none")]
    pub meter_value_start: Option<Number>,
    /// The operator.
    #[serde(rename = "OperatorID")]
    pub operator_id: OperatorId,
    /// The tariff product.
    #[serde(rename = "PartnerProductID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub partner_product_id: Option<Text<50>>,
    /// Undocumented fields, preserved verbatim.
    #[serde(flatten, default, skip_serializing_if = "Extensions::is_empty")]
    #[builder(default)]
    pub extensions: Extensions,
}

impl Validate for ChargingNotificationStart {
    fn validate_in(&self, v: &mut Validator) {
        check_type(v, self.notification_type, ChargingNotificationType::Start);
        if let (Some(session_start), true) = (&self.session_start, self.charging_start.is_well_formed())
            && session_start.is_well_formed()
            && session_start > &self.charging_start
        {
            v.report_at(
                "ChargingStart",
                ViolationCode::Inconsistent,
                "energy started flowing before the session began",
            );
        }
        validate_fields!(
            self,
            v,
            session_id as "SessionID",
            cpo_partner_session_id as "CPOPartnerSessionID",
            emp_partner_session_id as "EMPPartnerSessionID",
            identification as "Identification",
            evse_id as "EvseID",
            charging_start as "ChargingStart",
            session_start as "SessionStart",
            meter_value_start as "MeterValueStart",
            operator_id as "OperatorID",
            partner_product_id as "PartnerProductID",
        );
    }
}

/// How far the session has got.
///
/// The spec asks for either [`charging_duration`](Self::charging_duration) or
/// [`consumed_energy_progress`](Self::consumed_energy_progress) — both may be sent, but neither
/// alone makes a useful notification, and neither at all makes an empty one.
///
/// See erratum [`OICP23-E006`](crate::types::ERRATA): the EMP document defines the duration
/// self-referentially. The CPO document's definition is the implementable one:
/// `ChargingDuration = EventOccurred - ChargingStart`, in milliseconds.
///
/// Spec: `eRoamingChargingNotifications Progress`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, bon::Builder)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[builder(finish_fn = build_unchecked)]
pub struct ChargingNotificationProgress {
    /// Always [`ChargingNotificationType::Progress`].
    #[serde(rename = "Type")]
    pub notification_type: ChargingNotificationType,
    /// The session.
    #[serde(rename = "SessionID")]
    pub session_id: SessionId,
    /// The CPO's own session id.
    #[serde(rename = "CPOPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub cpo_partner_session_id: Option<PartnerSessionId>,
    /// The EMP's own session id.
    #[serde(rename = "EMPPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub emp_partner_session_id: Option<PartnerSessionId>,
    /// Who is charging.
    #[serde(rename = "Identification", default, skip_serializing_if = "Option::is_none")]
    pub identification: Option<Identification>,
    /// Where.
    #[serde(rename = "EvseID")]
    pub evse_id: EvseId,
    /// When energy started flowing.
    #[serde(rename = "ChargingStart")]
    pub charging_start: DateTime,
    /// When these figures were taken.
    #[serde(rename = "EventOccurred")]
    pub event_occurred: DateTime,
    /// `EventOccurred - ChargingStart`, in milliseconds.
    #[serde(rename = "ChargingDuration", default, skip_serializing_if = "Option::is_none")]
    pub charging_duration: Option<i64>,
    /// When the session started.
    #[serde(rename = "SessionStart", default, skip_serializing_if = "Option::is_none")]
    pub session_start: Option<DateTime>,
    /// Energy delivered since the start of charging, in kWh.
    #[serde(rename = "ConsumedEnergyProgress", default, skip_serializing_if = "Option::is_none")]
    pub consumed_energy_progress: Option<Number>,
    /// The meter reading at the start, in kWh.
    #[serde(rename = "MeterValueStart", default, skip_serializing_if = "Option::is_none")]
    pub meter_value_start: Option<Number>,
    /// Readings taken in between, in kWh.
    #[serde(rename = "MeterValueInBetween", default, skip_serializing_if = "Option::is_none")]
    pub meter_value_in_between: Option<MeterValuesInBetween>,
    /// The operator.
    #[serde(rename = "OperatorID")]
    pub operator_id: OperatorId,
    /// The tariff product.
    #[serde(rename = "PartnerProductID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub partner_product_id: Option<Text<50>>,
    /// Undocumented fields, preserved verbatim.
    #[serde(flatten, default, skip_serializing_if = "Extensions::is_empty")]
    #[builder(default)]
    pub extensions: Extensions,
}

impl ChargingNotificationProgress {
    /// The duration the timestamps imply, in milliseconds.
    ///
    /// The definition from the CPO document — see erratum `OICP23-E006`.
    #[must_use]
    pub fn implied_duration_ms(&self) -> Option<i64> {
        (self.event_occurred.as_offset()? - self.charging_start.as_offset()?)
            .whole_milliseconds()
            .try_into()
            .ok()
    }
}

impl Validate for ChargingNotificationProgress {
    fn validate_in(&self, v: &mut Validator) {
        check_type(v, self.notification_type, ChargingNotificationType::Progress);
        if self.charging_duration.is_none() && self.consumed_energy_progress.is_none() {
            v.report(
                ViolationCode::MissingConditional,
                "a progress notification carries at least one of ChargingDuration or \
                 ConsumedEnergyProgress; with neither it tells the driver nothing",
            );
        }
        if let (Some(stated), Some(implied)) = (self.charging_duration, self.implied_duration_ms()) {
            // A minute of slack: the CPO's clock and the notification's timestamps are not the
            // same instrument. Beyond that, one of the two is wrong.
            if (stated - implied).abs() > 60_000 {
                v.report_at(
                    "ChargingDuration",
                    ViolationCode::Inconsistent,
                    format!(
                        "ChargingDuration is {stated} ms, but EventOccurred - ChargingStart is {implied} ms"
                    ),
                );
            }
        }
        if self.charging_duration.is_some_and(|d| d < 0) {
            v.report_at("ChargingDuration", ViolationCode::OutOfRange, "a duration cannot be negative");
        }
        validate_fields!(
            self,
            v,
            session_id as "SessionID",
            cpo_partner_session_id as "CPOPartnerSessionID",
            emp_partner_session_id as "EMPPartnerSessionID",
            identification as "Identification",
            evse_id as "EvseID",
            charging_start as "ChargingStart",
            event_occurred as "EventOccurred",
            session_start as "SessionStart",
            consumed_energy_progress as "ConsumedEnergyProgress",
            meter_value_start as "MeterValueStart",
            meter_value_in_between as "MeterValueInBetween",
            operator_id as "OperatorID",
            partner_product_id as "PartnerProductID",
        );
    }
}

/// Energy has stopped flowing, but the car is still plugged in.
///
/// The distinction matters commercially: this is the moment a blocking fee may start, which is
/// what [`penalty_time_start`](Self::penalty_time_start) records.
///
/// Spec: `eRoamingChargingNotifications End`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, bon::Builder)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[builder(finish_fn = build_unchecked)]
pub struct ChargingNotificationEnd {
    /// Always [`ChargingNotificationType::End`].
    #[serde(rename = "Type")]
    pub notification_type: ChargingNotificationType,
    /// The session.
    #[serde(rename = "SessionID")]
    pub session_id: SessionId,
    /// The CPO's own session id.
    #[serde(rename = "CPOPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub cpo_partner_session_id: Option<PartnerSessionId>,
    /// The EMP's own session id.
    #[serde(rename = "EMPPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub emp_partner_session_id: Option<PartnerSessionId>,
    /// Who charged.
    #[serde(rename = "Identification", default, skip_serializing_if = "Option::is_none")]
    pub identification: Option<Identification>,
    /// Where.
    #[serde(rename = "EvseID")]
    pub evse_id: EvseId,
    /// When energy started flowing.
    #[serde(rename = "ChargingStart", default, skip_serializing_if = "Option::is_none")]
    pub charging_start: Option<DateTime>,
    /// When energy stopped flowing.
    #[serde(rename = "ChargingEnd")]
    pub charging_end: DateTime,
    /// When the session started.
    #[serde(rename = "SessionStart", default, skip_serializing_if = "Option::is_none")]
    pub session_start: Option<DateTime>,
    /// When the session ended.
    #[serde(rename = "SessionEnd", default, skip_serializing_if = "Option::is_none")]
    pub session_end: Option<DateTime>,
    /// Energy delivered, in kWh.
    #[serde(rename = "ConsumedEnergy", default, skip_serializing_if = "Option::is_none")]
    pub consumed_energy: Option<Number>,
    /// The meter reading at the start, in kWh.
    #[serde(rename = "MeterValueStart", default, skip_serializing_if = "Option::is_none")]
    pub meter_value_start: Option<Number>,
    /// The meter reading at the end, in kWh.
    #[serde(rename = "MeterValueEnd", default, skip_serializing_if = "Option::is_none")]
    pub meter_value_end: Option<Number>,
    /// Readings taken in between, in kWh.
    #[serde(rename = "MeterValueInBetween", default, skip_serializing_if = "Option::is_none")]
    pub meter_value_in_between: Option<MeterValuesInBetween>,
    /// The operator.
    #[serde(rename = "OperatorID")]
    pub operator_id: OperatorId,
    /// The tariff product.
    #[serde(rename = "PartnerProductID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub partner_product_id: Option<Text<50>>,
    /// When a blocking fee starts, after the grace period.
    #[serde(rename = "PenaltyTimeStart", default, skip_serializing_if = "Option::is_none")]
    pub penalty_time_start: Option<DateTime>,
    /// Undocumented fields, preserved verbatim.
    #[serde(flatten, default, skip_serializing_if = "Extensions::is_empty")]
    #[builder(default)]
    pub extensions: Extensions,
}

impl Validate for ChargingNotificationEnd {
    fn validate_in(&self, v: &mut Validator) {
        check_type(v, self.notification_type, ChargingNotificationType::End);
        if let (Some(start), Some(end), Some(consumed)) =
            (self.meter_value_start, self.meter_value_end, self.consumed_energy)
            && end - start != consumed
        {
            v.report_at(
                "ConsumedEnergy",
                ViolationCode::Inconsistent,
                format!("ConsumedEnergy is {consumed} but the meter readings differ by {}", end - start),
            );
        }
        if let (Some(charging_start), true) = (&self.charging_start, self.charging_end.is_well_formed())
            && charging_start.is_well_formed()
            && charging_start > &self.charging_end
        {
            v.report_at("ChargingEnd", ViolationCode::Inconsistent, "charging ended before it started");
        }
        validate_fields!(
            self,
            v,
            session_id as "SessionID",
            cpo_partner_session_id as "CPOPartnerSessionID",
            emp_partner_session_id as "EMPPartnerSessionID",
            identification as "Identification",
            evse_id as "EvseID",
            charging_start as "ChargingStart",
            charging_end as "ChargingEnd",
            session_start as "SessionStart",
            session_end as "SessionEnd",
            consumed_energy as "ConsumedEnergy",
            meter_value_start as "MeterValueStart",
            meter_value_end as "MeterValueEnd",
            meter_value_in_between as "MeterValueInBetween",
            operator_id as "OperatorID",
            partner_product_id as "PartnerProductID",
            penalty_time_start as "PenaltyTimeStart",
        );
    }
}

/// Something went wrong at the charging point.
///
/// Spec: `eRoamingChargingNotifications Error`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, bon::Builder)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[builder(finish_fn = build_unchecked)]
pub struct ChargingNotificationError {
    /// Always [`ChargingNotificationType::Error`].
    #[serde(rename = "Type")]
    pub notification_type: ChargingNotificationType,
    /// The session.
    #[serde(rename = "SessionID")]
    pub session_id: SessionId,
    /// The CPO's own session id.
    #[serde(rename = "CPOPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub cpo_partner_session_id: Option<PartnerSessionId>,
    /// The EMP's own session id.
    #[serde(rename = "EMPPartnerSessionID", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub emp_partner_session_id: Option<PartnerSessionId>,
    /// Who was charging.
    #[serde(rename = "Identification", default, skip_serializing_if = "Option::is_none")]
    pub identification: Option<Identification>,
    /// The operator.
    #[serde(rename = "OperatorID")]
    pub operator_id: OperatorId,
    /// Where.
    #[serde(rename = "EvseID")]
    pub evse_id: EvseId,
    /// What kind of problem it is.
    #[serde(rename = "ErrorType")]
    pub error_type: ErrorType,
    /// What the CPO knows about it.
    #[serde(rename = "ErrorAdditionalInfo", default, skip_serializing_if = "Option::is_none")]
    #[builder(into)]
    pub error_additional_info: Option<Text<250>>,
    /// Undocumented fields, preserved verbatim.
    #[serde(flatten, default, skip_serializing_if = "Extensions::is_empty")]
    #[builder(default)]
    pub extensions: Extensions,
}

impl Validate for ChargingNotificationError {
    fn validate_in(&self, v: &mut Validator) {
        check_type(v, self.notification_type, ChargingNotificationType::Error);
        validate_fields!(
            self,
            v,
            session_id as "SessionID",
            cpo_partner_session_id as "CPOPartnerSessionID",
            emp_partner_session_id as "EMPPartnerSessionID",
            identification as "Identification",
            operator_id as "OperatorID",
            evse_id as "EvseID",
            error_type as "ErrorType",
            error_additional_info as "ErrorAdditionalInfo",
        );
    }
}

fn check_type(v: &mut Validator, actual: ChargingNotificationType, expected: ChargingNotificationType) {
    if actual != expected {
        v.report_at(
            "Type",
            ViolationCode::Inconsistent,
            format!("this is a {expected} notification, but Type says {actual}"),
        );
    }
}

/// Any of the four charging notifications.
///
/// All four go to the same endpoint, `POST /notificationmgmt/v11/charging-notifications`, and are
/// told apart by their `Type` field. This is that discriminated union, so a server handler can
/// take one argument and `match`.
///
/// The dispatch is written by hand rather than with `#[serde(tag = "Type")]`, because `Type` is a
/// real field of each payload as well as the discriminator: serde's internally-tagged
/// representation consumes the tag, and the round trip would then lose it.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum ChargingNotification {
    /// Energy has started flowing.
    Start(ChargingNotificationStart),
    /// The session is running.
    Progress(ChargingNotificationProgress),
    /// Energy has stopped flowing.
    End(ChargingNotificationEnd),
    /// Something went wrong.
    Error(ChargingNotificationError),
}

impl ChargingNotification {
    /// Which notification this is.
    #[must_use]
    pub const fn notification_type(&self) -> ChargingNotificationType {
        match self {
            Self::Start(_) => ChargingNotificationType::Start,
            Self::Progress(_) => ChargingNotificationType::Progress,
            Self::End(_) => ChargingNotificationType::End,
            Self::Error(_) => ChargingNotificationType::Error,
        }
    }

    /// The session this notification is about.
    #[must_use]
    pub fn session_id(&self) -> &SessionId {
        match self {
            Self::Start(n) => &n.session_id,
            Self::Progress(n) => &n.session_id,
            Self::End(n) => &n.session_id,
            Self::Error(n) => &n.session_id,
        }
    }

    /// The charging spot this notification is about.
    #[must_use]
    pub fn evse_id(&self) -> &EvseId {
        match self {
            Self::Start(n) => &n.evse_id,
            Self::Progress(n) => &n.evse_id,
            Self::End(n) => &n.evse_id,
            Self::Error(n) => &n.evse_id,
        }
    }

    /// The operator that sent it.
    #[must_use]
    pub fn operator_id(&self) -> &OperatorId {
        match self {
            Self::Start(n) => &n.operator_id,
            Self::Progress(n) => &n.operator_id,
            Self::End(n) => &n.operator_id,
            Self::Error(n) => &n.operator_id,
        }
    }
}

impl Validate for ChargingNotification {
    fn validate_in(&self, v: &mut Validator) {
        match self {
            Self::Start(n) => n.validate_in(v),
            Self::Progress(n) => n.validate_in(v),
            Self::End(n) => n.validate_in(v),
            Self::Error(n) => n.validate_in(v),
        }
    }
}

impl Serialize for ChargingNotification {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Start(n) => n.serialize(s),
            Self::Progress(n) => n.serialize(s),
            Self::End(n) => n.serialize(s),
            Self::Error(n) => n.serialize(s),
        }
    }
}

impl<'de> Deserialize<'de> for ChargingNotification {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        use serde::de::Error as _;

        let value = serde_json::Value::deserialize(d)?;
        let tag = value
            .get("Type")
            .and_then(serde_json::Value::as_str)
            .ok_or_else(|| D::Error::missing_field("Type"))?;
        let notification_type: ChargingNotificationType = tag.parse().map_err(D::Error::custom)?;
        macro_rules! parse {
            ($variant:ident) => {
                Self::$variant(serde_json::from_value(value).map_err(D::Error::custom)?)
            };
        }
        Ok(match notification_type {
            ChargingNotificationType::Start => parse!(Start),
            ChargingNotificationType::Progress => parse!(Progress),
            ChargingNotificationType::End => parse!(End),
            ChargingNotificationType::Error => parse!(Error),
        })
    }
}

strict_builder!(
    ChargingNotificationStart,
    ChargingNotificationStartBuilder,
    charging_notification_start_builder
);
strict_builder!(
    ChargingNotificationProgress,
    ChargingNotificationProgressBuilder,
    charging_notification_progress_builder
);
strict_builder!(ChargingNotificationEnd, ChargingNotificationEndBuilder, charging_notification_end_builder);
strict_builder!(
    ChargingNotificationError,
    ChargingNotificationErrorBuilder,
    charging_notification_error_builder
);

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

    fn progress() -> ChargingNotificationProgress {
        ChargingNotificationProgress {
            notification_type: ChargingNotificationType::Progress,
            session_id: "f98efba4-02d8-4fa0-b810-9a9d50d2c527".parse().unwrap(),
            cpo_partner_session_id: None,
            emp_partner_session_id: None,
            identification: None,
            evse_id: "DE*XYZ*ETEST1".parse().unwrap(),
            charging_start: "2020-09-23T14:17:53.038Z".parse().unwrap(),
            event_occurred: "2020-09-23T14:25:53.038Z".parse().unwrap(),
            charging_duration: Some(480_000),
            session_start: None,
            consumed_energy_progress: Some("9".parse().unwrap()),
            meter_value_start: Some(Number::ZERO),
            meter_value_in_between: None,
            operator_id: "DE*ABC".parse().unwrap(),
            partner_product_id: None,
            extensions: Extensions::new(),
        }
    }

    #[test]
    fn the_notification_union_dispatches_on_the_type_tag() {
        let json = serde_json::to_string(&ChargingNotification::Progress(progress())).unwrap();
        assert!(json.contains(r#""Type":"Progress""#));
        let back: ChargingNotification = serde_json::from_str(&json).unwrap();
        assert_eq!(back.notification_type(), ChargingNotificationType::Progress);
        assert_eq!(back.evse_id().as_str(), "DE*XYZ*ETEST1");
    }

    #[test]
    fn a_progress_notification_with_neither_figure_says_nothing() {
        let mut n = progress();
        n.charging_duration = None;
        n.consumed_energy_progress = None;
        assert_eq!(n.validate().unwrap_err().as_slice()[0].code, ViolationCode::MissingConditional);
    }

    #[test]
    fn a_duration_that_contradicts_the_timestamps_is_reported() {
        // 14:25:53 - 14:17:53 is 480 s = 480000 ms, which is what the spec's own example says.
        let n = progress();
        assert_eq!(n.implied_duration_ms(), Some(480_000));
        assert!(n.validate().is_ok());

        let wrong = ChargingNotificationProgress { charging_duration: Some(48_000), ..progress() };
        let err = wrong.validate().unwrap_err();
        assert_eq!(err.as_slice()[0].pointer, "/ChargingDuration");
    }

    #[test]
    fn clock_skew_within_a_minute_is_tolerated() {
        let n = ChargingNotificationProgress { charging_duration: Some(480_000 + 30_000), ..progress() };
        assert!(n.validate().is_ok(), "half a minute of skew is not a spec violation");
    }

    #[test]
    fn a_mislabelled_notification_is_reported() {
        let n =
            ChargingNotificationProgress { notification_type: ChargingNotificationType::Start, ..progress() };
        assert!(n.validate().unwrap_err().iter().any(|x| x.pointer == "/Type"));
    }
}