ocpp-types 0.1.1

Strongly typed OCPP 1.6J, 2.0.1, and 2.1 message types for Rust. no_std, no alloc, embedded-friendly.
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! OCPP-J WebSocket message envelopes (`CALL`/`CALLRESULT`/`CALLERROR`, and
//! 2.1's `CALLRESULTERROR`/`SEND`) -- the array-based wrapper every OCPP
//! message travels in, as opposed to the message payload types themselves
//! (`v16`/`v201`/`v21`). See the OCPP-J specification, section 4.

/// A unique identifier correlating a `Call`/`SendMessage` with its
/// `CallResult`/`CallError`/`CallResultError`. Identical across 1.6J,
/// 2.0.1, and 2.1: a string of at most 36 characters, to allow for
/// UUIDs/GUIDs (per the OCPP-J specification).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MessageId(heapless::String<36>);

impl MessageId {
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl TryFrom<&str> for MessageId {
    type Error = ();

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        heapless::String::try_from(value).map(Self)
    }
}

/// Placeholder for OCPP-J's `errorDetails`, which the specification
/// deliberately leaves undefined in shape ("this JSON object describes
/// error details in an undefined way") and recommends leaving empty in
/// practice. Serializes as `{}`; deserializes by accepting and discarding
/// whatever object is present, so it round-trips regardless of what a
/// real implementation happens to send. Use a custom type via
/// [`CallError`]/[`CallResultError`]'s `D` parameter for typed details.
#[cfg(feature = "serde")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct EmptyPayload;

#[cfg(feature = "serde")]
impl serde::Serialize for EmptyPayload {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;
        serializer.serialize_map(Some(0))?.end()
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for EmptyPayload {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct EmptyPayloadVisitor;

        impl<'de> serde::de::Visitor<'de> for EmptyPayloadVisitor {
            type Value = EmptyPayload;

            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                f.write_str("a JSON object")
            }

            fn visit_map<A: serde::de::MapAccess<'de>>(
                self,
                mut map: A,
            ) -> Result<Self::Value, A::Error> {
                while map
                    .next_entry::<serde::de::IgnoredAny, serde::de::IgnoredAny>()?
                    .is_some()
                {}
                Ok(EmptyPayload)
            }
        }

        deserializer.deserialize_map(EmptyPayloadVisitor)
    }
}

/// An OCPP-J `CALL`: `[2, "<MessageId>", "<Action>", {Payload}]`. `T`'s
/// action name is written on serialize and validated (not merely assumed)
/// on deserialize, so a `Call<T>` you get back always really is the `T`
/// you asked for.
#[cfg(feature = "serde")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Call<T> {
    pub message_id: MessageId,
    pub payload: T,
}

#[cfg(feature = "serde")]
impl<T: crate::Action + serde::Serialize> serde::Serialize for Call<T> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeTuple;

        let mut tup = serializer.serialize_tuple(4)?;
        tup.serialize_element(&2u8)?;
        tup.serialize_element(&self.message_id)?;
        tup.serialize_element(T::ACTION)?;
        tup.serialize_element(&self.payload)?;
        tup.end()
    }
}

#[cfg(feature = "serde")]
impl<'de, T: crate::Action + serde::Deserialize<'de>> serde::Deserialize<'de> for Call<T> {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct CallVisitor<T>(core::marker::PhantomData<T>);

        impl<'de, T: crate::Action + serde::Deserialize<'de>> serde::de::Visitor<'de> for CallVisitor<T> {
            type Value = Call<T>;

            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "a CALL array [2, messageId, \"{}\", payload]", T::ACTION)
            }

            fn visit_seq<A: serde::de::SeqAccess<'de>>(
                self,
                mut seq: A,
            ) -> Result<Self::Value, A::Error> {
                let message_type_id: u8 = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
                if message_type_id != 2 {
                    return Err(serde::de::Error::invalid_value(
                        serde::de::Unexpected::Unsigned(message_type_id as u64),
                        &"2 (CALL)",
                    ));
                }

                let message_id: MessageId = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;

                let action: &str = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(2, &self))?;
                if action != T::ACTION {
                    return Err(serde::de::Error::invalid_value(
                        serde::de::Unexpected::Str(action),
                        &T::ACTION,
                    ));
                }

                let payload: T = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(3, &self))?;

                Ok(Call {
                    message_id,
                    payload,
                })
            }
        }

        deserializer.deserialize_tuple(4, CallVisitor(core::marker::PhantomData))
    }
}

/// An OCPP-J `CALLRESULT`: `[3, "<MessageId>", {Payload}]`. Unlike
/// [`Call`], there's no action name on the wire -- the recipient already
/// knows which action a result is for by correlating `message_id` against
/// the `Call` it sent, which is exactly what choosing `T` here represents.
#[cfg(feature = "serde")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallResult<T> {
    pub message_id: MessageId,
    pub payload: T,
}

#[cfg(feature = "serde")]
impl<T: serde::Serialize> serde::Serialize for CallResult<T> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeTuple;

        let mut tup = serializer.serialize_tuple(3)?;
        tup.serialize_element(&3u8)?;
        tup.serialize_element(&self.message_id)?;
        tup.serialize_element(&self.payload)?;
        tup.end()
    }
}

#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for CallResult<T> {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct CallResultVisitor<T>(core::marker::PhantomData<T>);

        impl<'de, T: serde::Deserialize<'de>> serde::de::Visitor<'de> for CallResultVisitor<T> {
            type Value = CallResult<T>;

            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                f.write_str("a CALLRESULT array [3, messageId, payload]")
            }

            fn visit_seq<A: serde::de::SeqAccess<'de>>(
                self,
                mut seq: A,
            ) -> Result<Self::Value, A::Error> {
                let message_type_id: u8 = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
                if message_type_id != 3 {
                    return Err(serde::de::Error::invalid_value(
                        serde::de::Unexpected::Unsigned(message_type_id as u64),
                        &"3 (CALLRESULT)",
                    ));
                }

                let message_id: MessageId = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;

                let payload: T = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(2, &self))?;

                Ok(CallResult {
                    message_id,
                    payload,
                })
            }
        }

        deserializer.deserialize_tuple(3, CallResultVisitor(core::marker::PhantomData))
    }
}

/// Defines an error envelope type (`CallError`/`CallResultError`): both
/// are `[<discriminant>, "<MessageId>", "<errorCode>", "<errorDescription>",
/// {errorDetails}]`, differing only in which prior message they're a
/// response to and their `MessageTypeId`. A macro rather than two
/// hand-written copies, so a fix to one can't drift from the other.
macro_rules! error_envelope {
    ($name:ident, $discriminant:literal, $expecting:literal) => {
        #[cfg(feature = "serde")]
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct $name<E, D = EmptyPayload, const DESC_CAP: usize = 1024> {
            pub message_id: MessageId,
            pub error_code: E,
            pub error_description: heapless::String<DESC_CAP>,
            pub error_details: D,
        }

        #[cfg(feature = "serde")]
        impl<E: serde::Serialize, D: serde::Serialize, const DESC_CAP: usize> serde::Serialize
            for $name<E, D, DESC_CAP>
        {
            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                use serde::ser::SerializeTuple;

                let mut tup = serializer.serialize_tuple(5)?;
                tup.serialize_element(&$discriminant)?;
                tup.serialize_element(&self.message_id)?;
                tup.serialize_element(&self.error_code)?;
                tup.serialize_element(&self.error_description)?;
                tup.serialize_element(&self.error_details)?;
                tup.end()
            }
        }

        #[cfg(feature = "serde")]
        impl<'de, E: serde::Deserialize<'de>, D: serde::Deserialize<'de>, const DESC_CAP: usize>
            serde::Deserialize<'de> for $name<E, D, DESC_CAP>
        {
            fn deserialize<Dz: serde::Deserializer<'de>>(deserializer: Dz) -> Result<Self, Dz::Error> {
                struct Visitor<E, D, const DESC_CAP: usize>(core::marker::PhantomData<(E, D)>);

                impl<'de, E: serde::Deserialize<'de>, D: serde::Deserialize<'de>, const DESC_CAP: usize>
                    serde::de::Visitor<'de> for Visitor<E, D, DESC_CAP>
                {
                    type Value = $name<E, D, DESC_CAP>;

                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                        f.write_str($expecting)
                    }

                    fn visit_seq<A: serde::de::SeqAccess<'de>>(
                        self,
                        mut seq: A,
                    ) -> Result<Self::Value, A::Error> {
                        let message_type_id: u8 = seq
                            .next_element()?
                            .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
                        if message_type_id != $discriminant {
                            return Err(serde::de::Error::invalid_value(
                                serde::de::Unexpected::Unsigned(message_type_id as u64),
                                &$expecting,
                            ));
                        }

                        let message_id = seq
                            .next_element()?
                            .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
                        let error_code = seq
                            .next_element()?
                            .ok_or_else(|| serde::de::Error::invalid_length(2, &self))?;
                        let error_description = seq
                            .next_element()?
                            .ok_or_else(|| serde::de::Error::invalid_length(3, &self))?;
                        let error_details = seq
                            .next_element()?
                            .ok_or_else(|| serde::de::Error::invalid_length(4, &self))?;

                        Ok($name {
                            message_id,
                            error_code,
                            error_description,
                            error_details,
                        })
                    }
                }

                deserializer.deserialize_tuple(5, Visitor(core::marker::PhantomData))
            }
        }
    };
}

// An OCPP-J `CALLERROR`: `[4, "<MessageId>", "<errorCode>",
// "<errorDescription>", {errorDetails}]`. `E` is the version's own
// `RpcErrorCode` (e.g. `v16::RpcErrorCode`) -- error codes aren't shared
// across versions, so nothing here picks one for you. `D` defaults to
// `EmptyPayload` since the spec leaves `errorDetails` undefined in shape
// and recommends leaving it empty; supply your own `D` for typed details.
// `errorDescription` has no spec-given bound either, so it's a
// caller-choosable const generic (default 1024) rather than a guess.
error_envelope!(
    CallError,
    4u8,
    "a CALLERROR array [4, messageId, errorCode, errorDescription, errorDetails]"
);

// An OCPP-J `CALLRESULTERROR` (OCPP 2.1 only): identical shape to
// `CallError`, sent in response to a `CallResult` that itself contained
// errors, rather than to a `Call`.
error_envelope!(
    CallResultError,
    5u8,
    "a CALLRESULTERROR array [5, messageId, errorCode, errorDescription, errorDetails]"
);

/// An OCPP-J `SEND` (OCPP 2.1 only): `[6, "<MessageId>", "<Action>",
/// {Payload}]`. Structurally identical to [`Call`], but the receiver
/// SHALL NOT respond to it -- used for fire-and-forget messages like
/// frequent periodic monitoring values.
#[cfg(feature = "serde")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SendMessage<T> {
    pub message_id: MessageId,
    pub payload: T,
}

#[cfg(feature = "serde")]
impl<T: crate::Action + serde::Serialize> serde::Serialize for SendMessage<T> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeTuple;

        let mut tup = serializer.serialize_tuple(4)?;
        tup.serialize_element(&6u8)?;
        tup.serialize_element(&self.message_id)?;
        tup.serialize_element(T::ACTION)?;
        tup.serialize_element(&self.payload)?;
        tup.end()
    }
}

#[cfg(feature = "serde")]
impl<'de, T: crate::Action + serde::Deserialize<'de>> serde::Deserialize<'de> for SendMessage<T> {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct SendVisitor<T>(core::marker::PhantomData<T>);

        impl<'de, T: crate::Action + serde::Deserialize<'de>> serde::de::Visitor<'de> for SendVisitor<T> {
            type Value = SendMessage<T>;

            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "a SEND array [6, messageId, \"{}\", payload]", T::ACTION)
            }

            fn visit_seq<A: serde::de::SeqAccess<'de>>(
                self,
                mut seq: A,
            ) -> Result<Self::Value, A::Error> {
                let message_type_id: u8 = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
                if message_type_id != 6 {
                    return Err(serde::de::Error::invalid_value(
                        serde::de::Unexpected::Unsigned(message_type_id as u64),
                        &"6 (SEND)",
                    ));
                }

                let message_id: MessageId = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;

                let action: &str = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(2, &self))?;
                if action != T::ACTION {
                    return Err(serde::de::Error::invalid_value(
                        serde::de::Unexpected::Str(action),
                        &T::ACTION,
                    ));
                }

                let payload: T = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(3, &self))?;

                Ok(SendMessage {
                    message_id,
                    payload,
                })
            }
        }

        deserializer.deserialize_tuple(4, SendVisitor(core::marker::PhantomData))
    }
}

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

    // The crate is `#![no_std]`, so `std` isn't implicitly in scope even in
    // test builds -- only needed here to spawn a thread with a larger
    // stack (see `send_message_serializes_and_round_trips_a_real_send_type_message`).
    extern crate std;

    #[cfg(feature = "serde")]
    #[test]
    fn empty_payload_serializes_as_an_empty_object() {
        let mut buf = [0u8; 16];
        let len = serde_json_core::to_slice(&EmptyPayload, &mut buf).unwrap();
        assert_eq!(&buf[..len], b"{}");
    }

    #[cfg(feature = "serde")]
    #[test]
    fn empty_payload_accepts_and_discards_a_populated_object() {
        let (parsed, _): (EmptyPayload, usize) =
            serde_json_core::from_str(r#"{"foo": 1, "bar": [1, 2, 3]}"#).unwrap();
        assert_eq!(parsed, EmptyPayload);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn call_serializes_and_round_trips_a_real_message_type() {
        use crate::v16::{AuthorizeRequest, IdTag};

        let call = Call {
            message_id: MessageId::try_from("19223201").unwrap(),
            payload: AuthorizeRequest {
                id_tag: IdTag::try_from("ABC123").unwrap(),
            },
        };

        let mut buf = [0u8; 128];
        let len = serde_json_core::to_slice(&call, &mut buf).unwrap();
        assert_eq!(
            core::str::from_utf8(&buf[..len]).unwrap(),
            r#"[2,"19223201","Authorize",{"idTag":"ABC123"}]"#
        );

        let (parsed, _): (Call<AuthorizeRequest>, usize) =
            serde_json_core::from_slice(&buf[..len]).unwrap();
        assert_eq!(parsed, call);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn call_rejects_the_wrong_message_type_id() {
        use crate::v16::AuthorizeRequest;

        let json = r#"[3,"1","Authorize",{"idTag":"ABC123"}]"#;
        let result: Result<(Call<AuthorizeRequest>, usize), _> = serde_json_core::from_str(json);
        assert!(result.is_err());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn call_rejects_a_mismatched_action() {
        use crate::v16::AuthorizeRequest;

        let json = r#"[2,"1","Heartbeat",{"idTag":"ABC123"}]"#;
        let result: Result<(Call<AuthorizeRequest>, usize), _> = serde_json_core::from_str(json);
        assert!(result.is_err());
    }

    // `HeartbeatResponse::current_time` is `heapless::String<N>` without
    // `alloc` and `alloc::string::String` with it -- this test constructs
    // the no-`alloc` shape specifically.
    #[cfg(all(feature = "serde", not(feature = "alloc")))]
    #[test]
    fn call_result_serializes_and_round_trips_a_real_message_type() {
        use crate::v16::HeartbeatResponse;

        let result: CallResult<HeartbeatResponse> = CallResult {
            message_id: MessageId::try_from("19223201").unwrap(),
            payload: HeartbeatResponse {
                current_time: heapless::String::try_from("2013-02-01T20:53:32.486Z").unwrap(),
            },
        };

        let mut buf = [0u8; 128];
        let len = serde_json_core::to_slice(&result, &mut buf).unwrap();
        assert_eq!(
            core::str::from_utf8(&buf[..len]).unwrap(),
            r#"[3,"19223201",{"currentTime":"2013-02-01T20:53:32.486Z"}]"#
        );

        let (parsed, _): (CallResult<HeartbeatResponse>, usize) =
            serde_json_core::from_slice(&buf[..len]).unwrap();
        assert_eq!(parsed, result);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn call_result_rejects_the_wrong_message_type_id() {
        use crate::v16::HeartbeatResponse;

        let json = r#"[2,"1",{"currentTime":"2013-02-01T20:53:32.486Z"}]"#;
        let result: Result<(CallResult<HeartbeatResponse>, usize), _> =
            serde_json_core::from_str(json);
        assert!(result.is_err());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn call_error_serializes_and_round_trips_a_real_error_code() {
        use crate::v16::RpcErrorCode;

        let error: CallError<RpcErrorCode> = CallError {
            message_id: MessageId::try_from("19223201").unwrap(),
            error_code: RpcErrorCode::NotImplemented,
            error_description: heapless::String::try_from("unrecognized action").unwrap(),
            error_details: EmptyPayload,
        };

        let mut buf = [0u8; 128];
        let len = serde_json_core::to_slice(&error, &mut buf).unwrap();
        assert_eq!(
            core::str::from_utf8(&buf[..len]).unwrap(),
            r#"[4,"19223201","NotImplemented","unrecognized action",{}]"#
        );

        let (parsed, _): (CallError<RpcErrorCode>, usize) =
            serde_json_core::from_slice(&buf[..len]).unwrap();
        assert_eq!(parsed, error);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn call_error_rejects_the_wrong_message_type_id() {
        use crate::v16::RpcErrorCode;

        let json = r#"[5,"1","NotImplemented","",{}]"#;
        let result: Result<(CallError<RpcErrorCode>, usize), _> = serde_json_core::from_str(json);
        assert!(result.is_err());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn call_result_error_uses_discriminant_five_not_four() {
        use crate::v201::RpcErrorCode;

        let error: CallResultError<RpcErrorCode> = CallResultError {
            message_id: MessageId::try_from("1").unwrap(),
            error_code: RpcErrorCode::FormatViolation,
            error_description: heapless::String::try_from("").unwrap(),
            error_details: EmptyPayload,
        };

        let mut buf = [0u8; 128];
        let len = serde_json_core::to_slice(&error, &mut buf).unwrap();
        assert!(core::str::from_utf8(&buf[..len]).unwrap().starts_with("[5,"));

        // A CALLERROR-shaped (discriminant 4) frame must NOT parse as a
        // CallResultError, even though every other field lines up.
        let call_error_json = r#"[4,"1","FormatViolation","",{}]"#;
        let result: Result<(CallResultError<RpcErrorCode>, usize), _> =
            serde_json_core::from_str(call_error_json);
        assert!(result.is_err());
    }

    #[cfg(all(feature = "serde", not(feature = "alloc")))]
    #[test]
    fn send_message_serializes_and_round_trips_a_real_send_type_message() {
        // `NotifyPeriodicEventStream` (OCPP 2.1) has no Request/Response
        // suffix at all in the spec -- it's a genuine SEND-only message,
        // not a CALL with an unusual name.
        //
        // This runs on a manually-spawned thread with a larger stack:
        // deserializing an envelope wrapping a type with two large
        // const-generic-defaulted fields (a 1024-byte string, a 16-item
        // vec) goes through several layers of serde's Visitor pattern,
        // and in an unoptimized debug build (no inlining) that's enough
        // to overflow the default test-thread stack. Release builds are
        // unaffected -- verified separately -- so this is purely a
        // `cargo test`-in-debug-mode artifact, not a real embedded-target
        // concern, but the test still needs to not crash in CI.
        std::thread::Builder::new()
            .stack_size(16 * 1024 * 1024)
            .spawn(|| {
                use crate::v21::NotifyPeriodicEventStream;

                let send: SendMessage<NotifyPeriodicEventStream> = SendMessage {
                    message_id: MessageId::try_from("19223201").unwrap(),
                    payload: NotifyPeriodicEventStream {
                        basetime: heapless::String::try_from("2024-08-27T12:30:40Z").unwrap(),
                        custom_data: None,
                        data: heapless::Vec::new(),
                        id: 123,
                        pending: 0,
                    },
                };

                let mut buf = [0u8; 256];
                let len = serde_json_core::to_slice(&send, &mut buf).unwrap();
                let json = core::str::from_utf8(&buf[..len]).unwrap();
                assert!(json.starts_with(r#"[6,"19223201","NotifyPeriodicEventStream","#));

                let (parsed, _): (SendMessage<NotifyPeriodicEventStream>, usize) =
                    serde_json_core::from_slice(&buf[..len]).unwrap();
                assert_eq!(parsed, send);
            })
            .unwrap()
            .join()
            .unwrap();
    }

    #[test]
    fn message_id_round_trips_a_short_string() {
        let id = MessageId::try_from("19223201").unwrap();
        assert_eq!(id.as_str(), "19223201");
    }

    #[test]
    fn message_id_rejects_strings_over_36_bytes() {
        let too_long = "a".repeat(37);
        assert!(MessageId::try_from(too_long.as_str()).is_err());
    }

    #[test]
    fn message_id_accepts_exactly_36_bytes() {
        let exactly_36 = "a".repeat(36);
        assert!(MessageId::try_from(exactly_36.as_str()).is_ok());
    }
}