1use core::fmt;
4
5use serde::Serialize;
6use serde::de::DeserializeOwned;
7
8use crate::validate::{Validate, ValidationPath, Violations};
9
10pub trait Sealed {}
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16pub enum MessageKind {
17 Call,
20 Send,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
27pub enum Origin {
28 ChargingStation,
30 Csms,
32 Both,
34}
35
36impl Origin {
37 #[must_use]
39 pub const fn from_charging_station(self) -> bool {
40 matches!(self, Origin::ChargingStation | Origin::Both)
41 }
42
43 #[must_use]
45 pub const fn from_csms(self) -> bool {
46 matches!(self, Origin::Csms | Origin::Both)
47 }
48}
49
50pub trait ActionName:
52 Copy + Eq + Ord + core::hash::Hash + fmt::Debug + fmt::Display + Sized + 'static
53{
54 fn all() -> &'static [Self];
56
57 fn as_str(&self) -> &'static str;
59
60 fn from_wire(name: &str) -> Option<Self>;
62
63 fn kind(&self) -> MessageKind;
65
66 fn origin(&self) -> Origin;
68
69 fn block(&self) -> &'static str;
71}
72
73pub trait Request: Serialize + DeserializeOwned + Validate + Sealed {
75 type Action: ActionName;
77 const ACTION: Self::Action;
79 const KIND: MessageKind;
81 const ORIGIN: Origin;
83 type Response: Response;
85}
86
87pub trait Confirmed: Request {}
93
94pub trait Unconfirmed: Request<Response = NoResponse> {}
98
99pub trait Response: Serialize + DeserializeOwned + Validate + Sealed {}
101
102#[derive(Clone, Copy, Debug, PartialEq, Eq)]
106pub enum NoResponse {}
107
108impl Sealed for NoResponse {}
109impl Response for NoResponse {}
110
111impl Validate for NoResponse {
112 fn validate_at(&self, _path: &mut ValidationPath, _out: &mut Violations) {}
113}
114
115impl Serialize for NoResponse {
116 fn serialize<S: serde::Serializer>(&self, _ser: S) -> Result<S::Ok, S::Error> {
117 match *self {}
118 }
119}
120
121impl<'de> serde::Deserialize<'de> for NoResponse {
122 fn deserialize<D: serde::Deserializer<'de>>(_de: D) -> Result<Self, D::Error> {
123 Err(serde::de::Error::custom("a SEND message has no response"))
124 }
125}
126
127#[derive(Clone, Copy, Debug, PartialEq, Eq)]
129pub struct UnknownAction;
130
131impl fmt::Display for UnknownAction {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 f.write_str("unknown OCPP action for this version")
134 }
135}
136
137#[cfg(feature = "std")]
138impl std::error::Error for UnknownAction {}
139
140#[derive(Clone, Copy, Debug, PartialEq, Eq)]
142pub struct UnknownVariant {
143 pub type_name: &'static str,
145}
146
147impl fmt::Display for UnknownVariant {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 write!(f, "not a defined value of {}", self.type_name)
150 }
151}
152
153#[cfg(feature = "std")]
154impl std::error::Error for UnknownVariant {}