ocpp-kit 0.5.0

Open Charge Point Protocol (OCPP 1.6J, 2.0.1, 2.1) toolkit for Rust: typed messages, OCPP-J framing, a sans-I/O protocol engine, and transports for charging stations, CSMS and local controllers.
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
//! Primitive types shared by every OCPP version.

use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::String;
use core::fmt;
use core::str::FromStr;

use serde::{Deserialize, Serialize};

use crate::validate::{Validate, ValidationPath, Violations};

// ---------------------------------------------------------------------------
// DateTime
// ---------------------------------------------------------------------------

/// An OCPP `dateTime`: an instant, serialized as RFC 3339 with an explicit offset.
///
/// Parsing accepts more than it emits, because the field is full of stations that omit the
/// offset or use a space separator. Which of those are tolerated is decided by
/// [`DecodeOptions::datetime`](crate::decode::DecodeOptions::datetime): the strict
/// `Deserialize` impl below requires an offset, and the decoder repairs the input first when
/// leniency is enabled. Serialization always emits UTC (`…Z`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DateTime(jiff::Timestamp);

impl DateTime {
    /// The Unix epoch.
    pub const UNIX_EPOCH: Self = Self(jiff::Timestamp::UNIX_EPOCH);

    #[must_use]
    /// Wraps a `jiff` timestamp.
    pub const fn from_timestamp(timestamp: jiff::Timestamp) -> Self {
        Self(timestamp)
    }

    #[must_use]
    /// The underlying `jiff` timestamp.
    pub const fn timestamp(self) -> jiff::Timestamp {
        self.0
    }

    /// The current time from the system clock.
    #[cfg(feature = "std")]
    #[must_use]
    pub fn now() -> Self {
        Self(jiff::Timestamp::now())
    }

    /// Parses an RFC 3339 timestamp. The offset is mandatory, and so is the `T` between date
    /// and time: RFC 3339's `date-time` grammar — which the schemas' `format` names — has no
    /// other separator. A space is a [`DateTimeLeniency::AllowSpace`] repair.
    ///
    /// [`DateTimeLeniency::AllowSpace`]: crate::decode::DateTimeLeniency::AllowSpace
    pub fn parse(text: &str) -> Result<Self, DateTimeError> {
        if !matches!(text.as_bytes().get(10), Some(b'T' | b't')) {
            return Err(DateTimeError {
                input: text.to_owned(),
            });
        }
        jiff::Timestamp::from_str(text)
            .map(Self)
            .map_err(|_| DateTimeError {
                input: text.to_owned(),
            })
    }

    /// Parses leniently: also accepts a missing offset (interpreted as UTC) and a space
    /// instead of `T`, both of which occur in the field.
    ///
    /// Reachable from the decoder via
    /// [`DateTimeLeniency`](crate::decode::DateTimeLeniency).
    pub fn parse_lenient(text: &str) -> Result<Self, DateTimeError> {
        if let Ok(value) = Self::parse(text) {
            return Ok(value);
        }
        let normalized = normalize_datetime(text);
        Self::parse(&normalized)
    }
}

/// Rewrites the common non-conforming timestamp spellings into strict RFC 3339.
///
/// Returns the input unchanged when it does not look like a bare local timestamp.
#[must_use]
pub fn normalize_datetime(text: &str) -> String {
    let trimmed = text.trim();
    let with_t = match trimmed.find(' ') {
        Some(index) if !trimmed.contains('T') => {
            let mut s = trimmed.to_owned();
            s.replace_range(index..=index, "T");
            s
        }
        _ => trimmed.to_owned(),
    };
    let has_offset = with_t.ends_with('Z')
        || with_t.ends_with('z')
        // `+01:00` / `-05:00`, but not the date's own separators.
        || with_t.rfind(['+', '-']).is_some_and(|i| i > 10);
    if has_offset {
        with_t
    } else {
        format!("{with_t}Z")
    }
}

impl fmt::Display for DateTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<jiff::Timestamp> for DateTime {
    fn from(value: jiff::Timestamp) -> Self {
        Self(value)
    }
}

impl From<DateTime> for jiff::Timestamp {
    fn from(value: DateTime) -> Self {
        value.0
    }
}

impl FromStr for DateTime {
    type Err = DateTimeError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Self::parse(text)
    }
}

impl Serialize for DateTime {
    fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        ser.collect_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for DateTime {
    fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        struct Visitor;

        impl serde::de::Visitor<'_> for Visitor {
            type Value = DateTime;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("an RFC 3339 date-time (`T` separator, explicit offset)")
            }

            fn visit_str<E: serde::de::Error>(self, text: &str) -> Result<DateTime, E> {
                DateTime::parse(text).map_err(E::custom)
            }
        }

        // A visitor rather than `<&str>::deserialize`: a JSON string containing an escape
        // cannot be borrowed, and asking for a borrowed one would report that as
        // "invalid type: string" — a *type* violation, when the value is really the problem.
        de.deserialize_str(Visitor)
    }
}

impl Validate for DateTime {
    fn validate_at(&self, _path: &mut ValidationPath, _out: &mut Violations) {}
}

/// A timestamp that is not valid RFC 3339.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DateTimeError {
    input: String,
}

impl fmt::Display for DateTimeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:?} is not an RFC 3339 date-time (`T` separator, explicit offset)",
            self.input
        )
    }
}

#[cfg(feature = "std")]
impl std::error::Error for DateTimeError {}

// ---------------------------------------------------------------------------
// Decimal
// ---------------------------------------------------------------------------

pub use crate::decimal::{Decimal, ParseDecimalError};

// ---------------------------------------------------------------------------
// integer
// ---------------------------------------------------------------------------

/// Deserializers for OCPP `integer` members whose schema is JSON Schema draft-06 or later.
///
/// From draft-06 on, `"type": "integer"` matches *any number with a zero fractional part*, so
/// `20000.0` is a valid `integer` for every OCPP 2.0.1 and 2.1 schema (and the OCPP 1.6
/// security-extension schemas). `serde`'s own `i32` refuses it. The generated types use these
/// functions on exactly the members whose schema file says draft-06; the OCPP 1.6 core
/// schemas are draft-04, where an integer is "a JSON number without a fraction or exponent
/// part", and keep `serde`'s exact reading (a lenient decode can still repair them, see
/// [`WholeFloats`](crate::decode::WholeFloats)).
///
/// A real fraction (`20000.5`) is still a type error, and a value outside `i32` is still a
/// property error — the same classification an integer literal gets.
#[doc(hidden)]
pub mod integer {
    use alloc::vec::Vec;
    use core::fmt;

    use serde::de::{self, Deserialize, Deserializer, Unexpected, Visitor};

    /// One `i32` that may arrive as `20000` or `20000.0`.
    struct Int(i32);

    struct IntVisitor;

    impl Visitor<'_> for IntVisitor {
        type Value = Int;

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

        fn visit_i64<E: de::Error>(self, v: i64) -> Result<Int, E> {
            i32::try_from(v)
                .map(Int)
                .map_err(|_| E::invalid_value(Unexpected::Signed(v), &self))
        }

        fn visit_u64<E: de::Error>(self, v: u64) -> Result<Int, E> {
            i32::try_from(v)
                .map(Int)
                .map_err(|_| E::invalid_value(Unexpected::Unsigned(v), &self))
        }

        #[allow(
            clippy::cast_possible_truncation,
            clippy::cast_precision_loss,
            clippy::float_cmp
        )]
        fn visit_f64<E: de::Error>(self, v: f64) -> Result<Int, E> {
            // `f64::fract` is `std`-only; a round trip through `i64` is the `no_std` test for
            // a zero fractional part, guarded so the cast cannot saturate.
            let whole = v.is_finite() && v.abs() < 9.0e15 && (v as i64) as f64 == v;
            if !whole {
                return Err(E::invalid_type(Unexpected::Float(v), &self));
            }
            self.visit_i64(v as i64)
        }
    }

    impl<'de> Deserialize<'de> for Int {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            deserializer.deserialize_any(IntVisitor)
        }
    }

    /// A required `integer` member.
    pub fn one<'de, D: Deserializer<'de>>(deserializer: D) -> Result<i32, D::Error> {
        Int::deserialize(deserializer).map(|v| v.0)
    }

    /// An optional `integer` member.
    pub fn option<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<i32>, D::Error> {
        Option::<Int>::deserialize(deserializer).map(|v| v.map(|v| v.0))
    }

    /// A required array of `integer`.
    pub fn list<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<i32>, D::Error> {
        Vec::<Int>::deserialize(deserializer).map(|v| v.into_iter().map(|v| v.0).collect())
    }

    /// An optional array of `integer`.
    pub fn option_list<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<Vec<i32>>, D::Error> {
        Option::<Vec<Int>>::deserialize(deserializer)
            .map(|v| v.map(|v| v.into_iter().map(|v| v.0).collect()))
    }
}

// ---------------------------------------------------------------------------
// CustomData
// ---------------------------------------------------------------------------

/// The OCPP 2.x `CustomDataType` extension point.
///
/// This is the one object the schemas deliberately leave open, so the unrecognised members
/// are kept in [`extra`](Self::extra) instead of being dropped — a Local Controller can relay
/// them and a CSMS can round-trip them.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CustomData {
    /// Vendor identifier, at most 255 characters.
    #[serde(rename = "vendorId")]
    pub vendor_id: String,
    /// Everything else the vendor put in this object.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

impl CustomData {
    /// Creates custom data for one vendor.
    #[must_use]
    pub fn new(vendor_id: impl Into<String>) -> Self {
        Self {
            vendor_id: vendor_id.into(),
            extra: serde_json::Map::new(),
        }
    }

    /// Adds one custom member.
    #[must_use]
    pub fn with(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        self.extra.insert(key.into(), value.into());
        self
    }
}

impl Validate for CustomData {
    fn validate_at(&self, path: &mut ValidationPath, out: &mut Violations) {
        path.push_key("vendorId");
        crate::validate::string(&self.vendor_id, None, Some(255), path, out);
        path.pop();
    }
}

// ---------------------------------------------------------------------------
// Identity
// ---------------------------------------------------------------------------

/// A Charging Station identity.
///
/// Carried as the last segment of the WebSocket URL path and, under security profiles 1 and
/// 2, as the HTTP Basic user name. At most 48 characters, and it must not contain `:`
/// because that would make the Basic credentials ambiguous (A00.FR.204).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct Identity(String);

/// The maximum length of a Charging Station identity, in characters.
pub const IDENTITY_MAX_LEN: usize = 48;

impl Identity {
    /// Validates and wraps an identity.
    pub fn new(value: impl Into<String>) -> Result<Self, IdentityError> {
        let value = value.into();
        if value.is_empty() {
            return Err(IdentityError::Empty);
        }
        let len = value.chars().count();
        if len > IDENTITY_MAX_LEN {
            return Err(IdentityError::TooLong(len));
        }
        // A00.FR.204 — the identity doubles as the Basic-Auth user name.
        if value.contains(':') {
            return Err(IdentityError::Colon);
        }
        if value.chars().any(char::is_control) {
            return Err(IdentityError::Control);
        }
        Ok(Self(value))
    }

    #[must_use]
    /// The identity as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Unwraps the identity.
    #[must_use]
    pub fn into_string(self) -> String {
        self.0
    }
}

impl fmt::Display for Identity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl FromStr for Identity {
    type Err = IdentityError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

impl<'de> Deserialize<'de> for Identity {
    fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        let value = String::deserialize(de)?;
        Self::new(value).map_err(serde::de::Error::custom)
    }
}

/// Why an identity was rejected.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum IdentityError {
    /// The identity is empty.
    Empty,
    /// Longer than [`IDENTITY_MAX_LEN`] characters.
    TooLong(usize),
    /// Contains `:`, which HTTP Basic authentication cannot represent (A00.FR.204).
    Colon,
    /// Contains a control character, which cannot appear in a URL path segment.
    Control,
}

impl fmt::Display for IdentityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IdentityError::Empty => f.write_str("charging station identity is empty"),
            IdentityError::TooLong(len) => {
                write!(
                    f,
                    "charging station identity is {len} characters, maximum is {IDENTITY_MAX_LEN}"
                )
            }
            IdentityError::Colon => {
                f.write_str("charging station identity must not contain ':' (A00.FR.204)")
            }
            IdentityError::Control => {
                f.write_str("charging station identity contains a control character")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for IdentityError {}

// ---------------------------------------------------------------------------
// MessageId
// ---------------------------------------------------------------------------

/// The maximum length of an OCPP-J message id, in characters (Part 4 §4.1.1).
pub const MESSAGE_ID_MAX_LEN: usize = 36;

/// An OCPP-J `MessageId` (1.6J calls it `UniqueId`): a string of at most 36 characters that
/// correlates a `CALL` with its `CALLRESULT` / `CALLERROR`.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct MessageId(String);

impl MessageId {
    /// Validates and wraps a message id.
    pub fn new(value: impl Into<String>) -> Result<Self, MessageIdError> {
        let value = value.into();
        if value.is_empty() {
            return Err(MessageIdError::Empty);
        }
        let len = value.chars().count();
        if len > MESSAGE_ID_MAX_LEN {
            return Err(MessageIdError::TooLong(len));
        }
        Ok(Self(value))
    }

    /// Wraps an id exactly as a peer sent it, including one that is too long.
    ///
    /// The framing layer must echo the id it received verbatim or the peer cannot correlate
    /// the answer, so length is *reported* — see [`is_conforming`](Self::is_conforming) —
    /// rather than silently changed.
    #[must_use]
    pub fn from_wire(value: &str) -> Self {
        Self(value.to_owned())
    }

    /// Whether the id respects the 36-character limit.
    #[must_use]
    pub fn is_conforming(&self) -> bool {
        !self.0.is_empty() && self.0.chars().count() <= MESSAGE_ID_MAX_LEN
    }

    /// Wraps a value, truncating it to [`MESSAGE_ID_MAX_LEN`] characters.
    ///
    /// For ids this peer generates, so an over-long prefix cannot produce an invalid id.
    #[must_use]
    pub fn truncating(value: &str) -> Self {
        match value.char_indices().nth(MESSAGE_ID_MAX_LEN) {
            Some((cut, _)) => Self(value[..cut].to_owned()),
            None => Self(value.to_owned()),
        }
    }

    /// The id OCPP 2.x prescribes when the incoming `MessageId` itself could not be read
    /// (Part 4 §4.1.1).
    #[must_use]
    pub fn unreadable() -> Self {
        Self("-1".to_owned())
    }

    /// The id as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Unwraps the id.
    #[must_use]
    pub fn into_string(self) -> String {
        self.0
    }
}

impl fmt::Display for MessageId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl FromStr for MessageId {
    type Err = MessageIdError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

impl<'de> Deserialize<'de> for MessageId {
    fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        let value = String::deserialize(de)?;
        Self::new(value).map_err(serde::de::Error::custom)
    }
}

/// Why a message id was rejected.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MessageIdError {
    /// The id is empty.
    Empty,
    /// Longer than [`MESSAGE_ID_MAX_LEN`] characters.
    TooLong(usize),
}

impl fmt::Display for MessageIdError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MessageIdError::Empty => f.write_str("message id is empty"),
            MessageIdError::TooLong(len) => {
                write!(
                    f,
                    "message id is {len} characters, maximum is {MESSAGE_ID_MAX_LEN}"
                )
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for MessageIdError {}

/// Source of outgoing [`MessageId`]s.
///
/// Pluggable because constrained devices cannot afford an entropy source, and a Local
/// Controller needs ids that provably cannot collide with the CSMS's (Part 4 §6.4).
///
/// # The uniqueness rule
///
/// Part 4 §4.1.4 is stricter than it first looks: a `MessageId` must differ from every id the
/// same sender has used for a `CALL` or a `SEND` **on any WebSocket connection under the same
/// Charging Station identity** — not merely within one connection. A retransmission of the
/// same message *may* reuse its id, and nothing else may.
pub trait IdGenerator {
    /// Produces the next id.
    ///
    /// Implementations must satisfy the uniqueness rule above.
    fn next_id(&mut self) -> MessageId;
}

/// A random generator: a version 4 UUID per id, 36 characters exactly.
///
/// The default wherever an entropy source is available. 122 random bits make a collision
/// across reboots — which is what Part 4 §4.1.4 actually forbids — not worth reasoning about.
///
/// It also carries a counter, which only matters in the case that should never happen: if the
/// operating system's entropy source fails, the ids fall back to being *distinct* rather than
/// random. A repeating `MessageId` is the one outcome §4.1.4 rules out, and it is exactly what
/// a fixed fallback value would produce.
#[cfg(feature = "getrandom")]
#[derive(Clone, Copy, Debug, Default)]
pub struct RandomIds {
    counter: u64,
}

#[cfg(feature = "getrandom")]
impl RandomIds {
    /// A generator drawing from the operating system's entropy source.
    #[must_use]
    pub fn new() -> Self {
        Self { counter: 0 }
    }
}

#[cfg(feature = "getrandom")]
impl IdGenerator for RandomIds {
    fn next_id(&mut self) -> MessageId {
        self.counter = self.counter.wrapping_add(1);
        MessageId(uuid_v4().unwrap_or_else(|| degraded_uuid(self.counter)))
    }
}

/// A random version 4 UUID, in the canonical 36-character hyphenated form.
///
/// `None` when the operating system's entropy source failed — which on a supported target
/// means it is not merely low on entropy but unavailable. There is no safe constant to return:
/// two OCPP identifiers are required to be unique across a Charging Station's whole lifetime —
/// the `MessageId` (Part 4 §4.1.4) and the `transactionId` (E01.FR.08, which spells out "even
/// when the Charging Station is rebooted, repaired, firmware is updated etc." and recommends
/// UUIDs by name) — so a fixed fallback would break precisely the guarantee callers came for.
/// Callers that must produce *something* use a counter instead; see [`RandomIds`].
#[cfg(feature = "getrandom")]
#[must_use]
pub fn uuid_v4() -> Option<String> {
    let mut bytes = [0u8; 16];
    getrandom::fill(&mut bytes).ok()?;
    bytes[6] = (bytes[6] & 0x0F) | 0x40; // version 4
    bytes[8] = (bytes[8] & 0x3F) | 0x80; // variant RFC 4122
    Some(hyphenate(bytes))
}

/// A UUID-shaped id built from a counter, for the case where entropy is unavailable.
///
/// Version 4 is *not* claimed — the version nibble is `8`, which no RFC 4122 version uses —
/// so a reader can tell at a glance that these ids are unique within a process run and nothing
/// more. Across a reboot they repeat, which is why this is a last resort and not a mode.
#[cfg(feature = "getrandom")]
#[must_use]
fn degraded_uuid(counter: u64) -> String {
    let mut bytes = [0u8; 16];
    bytes[..8].copy_from_slice(&counter.to_be_bytes());
    bytes[6] = (bytes[6] & 0x0F) | 0x80;
    bytes[8] = (bytes[8] & 0x3F) | 0x80;
    hyphenate(bytes)
}

/// Renders 16 bytes as `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`.
#[cfg(feature = "getrandom")]
fn hyphenate(bytes: [u8; 16]) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut out = [0u8; 36];
    let mut index = 0;
    for (position, byte) in bytes.iter().enumerate() {
        if matches!(position, 4 | 6 | 8 | 10) {
            out[index] = b'-';
            index += 1;
        }
        out[index] = HEX[usize::from(byte >> 4)];
        out[index + 1] = HEX[usize::from(byte & 0x0F)];
        index += 2;
    }
    // Every byte written is ASCII hex or `-`.
    core::str::from_utf8(&out)
        .unwrap_or("00000000-0000-8000-8000-000000000000")
        .to_owned()
}

/// A counter-based generator: `<prefix><counter>`, base-36 encoded.
///
/// For targets with no entropy source. It satisfies Part 4 §4.1.4 **only if the prefix is
/// different on every boot** — a persisted boot counter, a serial number plus a session
/// number, anything that does not repeat. With a constant prefix the ids restart at zero
/// after a power cut and collide with the ones used before it, which is exactly what §4.1.4
/// forbids and what makes a CSMS mismatch a response to the wrong request.
///
/// A Local Controller uses the prefix for a second purpose: keeping its own ids disjoint from
/// the CSMS's (Part 4 §6.4).
#[derive(Clone, Debug)]
pub struct CounterIds {
    prefix: String,
    next: u64,
}

impl CounterIds {
    /// Starts a generator with the given prefix. Read the type's documentation first: the
    /// prefix is what makes the ids unique across reboots.
    #[must_use]
    pub fn with_prefix(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into(),
            next: 0,
        }
    }
}

impl Default for CounterIds {
    fn default() -> Self {
        Self::with_prefix("")
    }
}

impl IdGenerator for CounterIds {
    fn next_id(&mut self) -> MessageId {
        let value = self.next;
        self.next = self.next.wrapping_add(1);
        let mut digits = [0u8; 13];
        let mut index = digits.len();
        let mut remaining = value;
        loop {
            index -= 1;
            let digit = u8::try_from(remaining % 36).unwrap_or(0);
            digits[index] = if digit < 10 {
                b'0' + digit
            } else {
                b'a' + digit - 10
            };
            remaining /= 36;
            if remaining == 0 {
                break;
            }
        }
        let suffix = core::str::from_utf8(&digits[index..]).unwrap_or("0");
        MessageId::truncating(&format!("{}{suffix}", self.prefix))
    }
}

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

    #[test]
    fn a_strict_date_time_needs_the_t_separator() {
        assert!(DateTime::parse("2026-11-07T12:00:05Z").is_ok());
        assert!(DateTime::parse("2026-11-07t12:00:05z").is_ok()); // RFC 3339 §5.6: case-insensitive
        assert!(DateTime::parse("2026-11-07 12:00:05Z").is_err());
        assert_eq!(
            DateTime::parse_lenient("2026-11-07 12:00:05Z").unwrap(),
            DateTime::parse("2026-11-07T12:00:05Z").unwrap()
        );
    }

    #[test]
    fn identity_rejects_colon_per_a00_fr_204() {
        assert_eq!(Identity::new("cs:1"), Err(IdentityError::Colon));
        assert!(Identity::new("CS-0001").is_ok());
        assert_eq!(
            Identity::new("x".repeat(49)),
            Err(IdentityError::TooLong(49))
        );
    }

    #[test]
    fn message_id_truncates_to_36_characters() {
        let id = MessageId::truncating(&"a".repeat(80));
        assert_eq!(id.as_str().chars().count(), 36);
    }

    #[test]
    fn counter_ids_are_unique_and_prefixed() {
        let mut ids = CounterIds::with_prefix("lc-");
        assert_eq!(ids.next_id().as_str(), "lc-0");
        assert_eq!(ids.next_id().as_str(), "lc-1");
        for _ in 0..40 {
            ids.next_id();
        }
        assert_eq!(ids.next_id().as_str(), "lc-16");
    }

    #[cfg(feature = "getrandom")]
    #[test]
    fn random_ids_are_uuids_and_do_not_repeat() {
        let mut ids = RandomIds::new();
        let mut seen = std::collections::HashSet::new();
        for _ in 0..256 {
            let id = ids.next_id();
            assert_eq!(id.as_str().len(), 36, "{id}");
            assert!(id.is_conforming());
            assert_eq!(&id.as_str()[14..15], "4", "version nibble: {id}");
            assert!(seen.insert(id.into_string()));
        }
    }

    #[test]
    fn datetime_leniency_matches_field_behaviour() {
        assert!(DateTime::parse("2024-01-01T10:00:00").is_err());
        assert_eq!(
            DateTime::parse_lenient("2024-01-01T10:00:00").unwrap(),
            DateTime::parse("2024-01-01T10:00:00Z").unwrap()
        );
        assert_eq!(
            DateTime::parse_lenient("2024-01-01 10:00:00").unwrap(),
            DateTime::parse("2024-01-01T10:00:00Z").unwrap()
        );
        assert_eq!(
            DateTime::parse_lenient("2024-01-01T10:00:00+02:00").unwrap(),
            DateTime::parse("2024-01-01T08:00:00Z").unwrap()
        );
    }
}