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
//! The two enum shapes this crate distinguishes, as declarative macros.
//!
//! OICP 2.3 has no formal notion of an extensible enum — every enumerated type in the
//! specification is written as a closed list. In practice that is not how the wire behaves:
//!
//! * Hubject edits the OICP 2.3 documents **in place**, with no version bump. `Plug`,
//!   `ValueAddedService` and `PaymentOption` have each grown values this way.
//! * A hub forwards records between two parties. Discarding a `Plug` value this crate has never
//!   heard of turns a faithful forward into silent data loss.
//!
//! So the default in this crate is [`oicp_open_enum!`](crate::oicp_open_enum): every documented
//! value becomes a variant, and anything else is **kept** in a `Custom(String)` variant that
//! re-serialises byte-identically. [`Validate`](crate::types::Validate) reports the unknown value
//! as [`ViolationCode::PatternMismatch`](crate::types::ViolationCode::PatternMismatch) — so it is
//! visible in a conformance report — but decoding never fails and the value is never rewritten.
//!
//! [`oicp_enum!`](crate::oicp_enum) generates the strict shape, for the handful of types where an
//! unknown value genuinely is a protocol error rather than an extension: `ActionType`, where
//! guessing would risk deleting a fleet, and `AuthorizationStatus`, where "neither Authorized nor
//! NotAuthorized" has no safe interpretation.
//!
//! Both macros are exported, so a party defining its own extension types can use them.

/// Why a string is not a member of a closed OICP enum.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnknownVariant {
    enum_name: &'static str,
    value: String,
    allowed: &'static [&'static str],
}

impl UnknownVariant {
    /// Creates an error for `value`, which is not one of `allowed`.
    #[must_use]
    pub fn new(enum_name: &'static str, value: impl Into<String>, allowed: &'static [&'static str]) -> Self {
        Self { enum_name, value: value.into(), allowed }
    }

    /// The value that was not recognised.
    #[must_use]
    pub fn value(&self) -> &str {
        &self.value
    }

    /// The name of the enum that rejected it.
    #[must_use]
    pub const fn enum_name(&self) -> &'static str {
        self.enum_name
    }

    /// Every value the enum does accept.
    #[must_use]
    pub const fn allowed(&self) -> &'static [&'static str] {
        self.allowed
    }
}

impl core::fmt::Display for UnknownVariant {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{:?} is not a valid {}; expected one of ", self.value, self.enum_name)?;
        for (i, a) in self.allowed.iter().enumerate() {
            if i > 0 {
                f.write_str(", ")?;
            }
            write!(f, "{a}")?;
        }
        Ok(())
    }
}

impl std::error::Error for UnknownVariant {}

/// Defines a **closed** OICP enum: a fixed set of strings, where anything else is an error.
///
/// Reserved for the types where an unrecognised value has no safe interpretation. See the
/// [open-enum rule](crate::types#open-enums-by-default) for why that is the minority.
///
/// ```
/// use oicp_kit::oicp_enum;
///
/// oicp_enum! {
///     /// Whether the user may charge.
///     pub enum AuthorizationStatus {
///         /// User is authorized.
///         Authorized = "Authorized",
///         /// User is not authorized.
///         NotAuthorized = "NotAuthorized",
///     }
/// }
///
/// assert_eq!(AuthorizationStatus::Authorized.as_str(), "Authorized");
/// assert!("Maybe".parse::<AuthorizationStatus>().is_err());
/// ```
///
/// Attributes that document the enum — `#[cfg_attr(docsrs, doc(cfg(…)))]` above all — go **inside**
/// the invocation, on the `pub enum` line, so they land on the item this expands to.
#[macro_export]
macro_rules! oicp_enum {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident {
            $(
                $(#[$vmeta:meta])*
                $variant:ident = $wire:literal
            ),* $(,)?
        }
    ) => {
        $(#[$meta])*
        #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
        $vis enum $name {
            $(
                $(#[$vmeta])*
                #[doc = concat!("\n\nWire value: `", $wire, "`")]
                $variant,
            )*
        }

        #[allow(dead_code)]
        impl $name {
            /// Every value this enum accepts, in declaration order.
            pub const ALL: &'static [Self] = &[ $( Self::$variant ),* ];
            /// Every wire value this enum accepts, in declaration order.
            pub const ALL_WIRE: &'static [&'static str] = &[ $( $wire ),* ];

            /// The value as it appears on the wire.
            #[must_use]
            pub const fn as_str(&self) -> &'static str {
                match self { $( Self::$variant => $wire, )* }
            }

            /// Parses a wire value, ignoring ASCII case.
            ///
            /// OICP enum values are case-sensitive, so this is only for peers known to get the
            /// case wrong; [`FromStr`](core::str::FromStr) is the strict version.
            #[must_use]
            pub fn from_str_ignore_case(s: &str) -> Option<Self> {
                $( if s.eq_ignore_ascii_case($wire) { return Some(Self::$variant); } )*
                None
            }
        }

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

        impl core::str::FromStr for $name {
            type Err = $crate::types::UnknownVariant;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $( $wire => Ok(Self::$variant), )*
                    other => Err($crate::types::UnknownVariant::new(
                        stringify!($name), other, Self::ALL_WIRE,
                    )),
                }
            }
        }

        impl $crate::types::Validate for $name {
            fn validate_in(&self, _v: &mut $crate::types::Validator) {}
        }

        impl serde::Serialize for $name {
            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                s.serialize_str(self.as_str())
            }
        }

        impl<'de> serde::Deserialize<'de> for $name {
            fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                struct V;
                impl serde::de::Visitor<'_> for V {
                    type Value = $name;
                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                        write!(f, "one of the {} values of {}", $name::ALL_WIRE.len(), stringify!($name))
                    }
                    fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<$name, E> {
                        <$name as core::str::FromStr>::from_str(v).map_err(E::custom)
                    }
                }
                d.deserialize_str(V)
            }
        }

        #[cfg(feature = "schema")]
        impl schemars::JsonSchema for $name {
            fn schema_name() -> std::borrow::Cow<'static, str> { stringify!($name).into() }
            fn json_schema(_g: &mut schemars::SchemaGenerator) -> schemars::Schema {
                schemars::json_schema!({ "type": "string", "enum": Self::ALL_WIRE })
            }
        }
    };
}

/// Defines an OICP enum that keeps values it does not know.
///
/// The documented values become variants; anything else lands in `Custom(String)` and is written
/// back verbatim. This is the default shape in this crate — see the
/// [open-enum rule](crate::types#open-enums-by-default).
///
/// ```
/// use oicp_kit::oicp_open_enum;
///
/// oicp_open_enum! {
///     /// The plug type of a charging facility.
///     pub enum Plug {
///         /// IEC 62196-1 type 2.
///         Type2Outlet = "Type 2 Outlet",
///         /// DC CHAdeMO connector.
///         ChaDeMo = "CHAdeMO",
///     }
/// }
///
/// // A value Hubject added after this crate was written survives untouched.
/// let future: Plug = "MCS".parse().unwrap();
/// assert!(!future.is_known());
/// assert_eq!(future.as_str(), "MCS");
/// assert_eq!(serde_json::to_string(&future).unwrap(), r#""MCS""#);
/// ```
#[macro_export]
macro_rules! oicp_open_enum {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident {
            $(
                $(#[$vmeta:meta])*
                $variant:ident = $wire:literal
            ),* $(,)?
        }
    ) => {
        $(#[$meta])*
        ///
        /// A value OICP 2.3 does not document is **kept** rather than rejected: it lands in
        /// `Custom` and re-serialises byte-identically. [`Validate`](crate::types::Validate)
        /// reports it, so a conformance run still sees it.
        #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
        $vis enum $name {
            $(
                $(#[$vmeta])*
                #[doc = concat!("\n\nWire value: `", $wire, "`")]
                $variant,
            )*
            /// A value this crate does not know, preserved exactly as it arrived.
            Custom(String),
        }

        #[allow(dead_code)]
        impl $name {
            /// Every documented value, in declaration order.
            pub const ALL: &'static [Self] = &[ $( Self::$variant ),* ];
            /// Every documented wire value, in declaration order.
            pub const ALL_WIRE: &'static [&'static str] = &[ $( $wire ),* ];

            /// The value as it appears on the wire.
            #[must_use]
            pub fn as_str(&self) -> &str {
                match self {
                    $( Self::$variant => $wire, )*
                    Self::Custom(s) => s.as_str(),
                }
            }

            /// Whether this is a value the specification documents.
            #[must_use]
            pub fn is_known(&self) -> bool {
                !matches!(self, Self::Custom(_))
            }

            /// Parses a wire value, ignoring ASCII case for the documented values.
            ///
            /// A value that matches nothing becomes `Custom` with its original text, so this
            /// never fails.
            #[must_use]
            pub fn from_str_ignore_case(s: &str) -> Self {
                $( if s.eq_ignore_ascii_case($wire) { return Self::$variant; } )*
                Self::Custom(s.to_owned())
            }
        }

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

        impl core::str::FromStr for $name {
            type Err = core::convert::Infallible;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok(match s {
                    $( $wire => Self::$variant, )*
                    other => Self::Custom(other.to_owned()),
                })
            }
        }

        impl From<&str> for $name {
            fn from(s: &str) -> Self {
                match s {
                    $( $wire => Self::$variant, )*
                    other => Self::Custom(other.to_owned()),
                }
            }
        }

        impl $crate::types::Validate for $name {
            fn validate_in(&self, v: &mut $crate::types::Validator) {
                if let Self::Custom(value) = self {
                    v.report(
                        $crate::types::ViolationCode::PatternMismatch,
                        format!(
                            "{value:?} is not one of the {} values OICP 2.3 documents for {}; \
                             it is preserved, but a peer that has not agreed on it will not \
                             understand it",
                            Self::ALL_WIRE.len(), stringify!($name),
                        ),
                    );
                }
            }
        }

        impl serde::Serialize for $name {
            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                s.serialize_str(self.as_str())
            }
        }

        impl<'de> serde::Deserialize<'de> for $name {
            fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                struct V;
                impl serde::de::Visitor<'_> for V {
                    type Value = $name;
                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                        write!(f, "a {} value", stringify!($name))
                    }
                    fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<$name, E> {
                        Ok($name::from(v))
                    }
                }
                d.deserialize_str(V)
            }
        }

        #[cfg(feature = "schema")]
        impl schemars::JsonSchema for $name {
            fn schema_name() -> std::borrow::Cow<'static, str> { stringify!($name).into() }
            fn json_schema(_g: &mut schemars::SchemaGenerator) -> schemars::Schema {
                // `enum` would make a documented value the only legal one; OICP grows these
                // lists in place, so the schema documents the known values without closing them.
                schemars::json_schema!({ "type": "string", "examples": Self::ALL_WIRE })
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use crate::types::Validate;

    oicp_open_enum! {
        /// Test enum.
        pub enum Sample {
            /// A.
            Alpha = "Alpha",
            /// B.
            Beta = "Beta",
        }
    }

    oicp_enum! {
        /// Closed test enum.
        pub enum Closed {
            /// A.
            Alpha = "Alpha",
        }
    }

    #[test]
    fn open_enum_preserves_unknown_values_byte_for_byte() {
        let json = r#""SomethingHubjectAddedLater""#;
        let value: Sample = serde_json::from_str(json).unwrap();
        assert!(!value.is_known());
        assert_eq!(serde_json::to_string(&value).unwrap(), json);
        // Preserved, but still reported.
        assert_eq!(value.validate().unwrap_err().len(), 1);
    }

    #[test]
    fn open_enum_known_values_validate_clean() {
        let value: Sample = serde_json::from_str(r#""Beta""#).unwrap();
        assert_eq!(value, Sample::Beta);
        assert!(value.validate().is_ok());
    }

    #[test]
    fn closed_enum_rejects_unknown_values() {
        assert!(serde_json::from_str::<Closed>(r#""Beta""#).is_err());
    }

    #[test]
    fn case_insensitive_parsing_is_opt_in() {
        assert_eq!(Sample::from_str_ignore_case("alpha"), Sample::Alpha);
        assert_eq!(Sample::from("alpha"), Sample::Custom("alpha".into()));
    }
}