use core::fmt;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::validate::{Validate, ValidationPath, Violations};
pub trait Sealed {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MessageKind {
Call,
Send,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Origin {
ChargingStation,
Csms,
Both,
}
impl Origin {
#[must_use]
pub const fn from_charging_station(self) -> bool {
matches!(self, Origin::ChargingStation | Origin::Both)
}
#[must_use]
pub const fn from_csms(self) -> bool {
matches!(self, Origin::Csms | Origin::Both)
}
}
pub trait ActionName:
Copy + Eq + Ord + core::hash::Hash + fmt::Debug + fmt::Display + Sized + 'static
{
fn all() -> &'static [Self];
fn as_str(&self) -> &'static str;
fn from_wire(name: &str) -> Option<Self>;
fn kind(&self) -> MessageKind;
fn origin(&self) -> Origin;
fn block(&self) -> &'static str;
}
pub trait Request: Serialize + DeserializeOwned + Validate + Sealed {
type Action: ActionName;
const ACTION: Self::Action;
const KIND: MessageKind;
const ORIGIN: Origin;
type Response: Response;
}
pub trait Confirmed: Request {}
pub trait Unconfirmed: Request<Response = NoResponse> {}
pub trait Response: Serialize + DeserializeOwned + Validate + Sealed {}
pub trait DecodeRequest: Sized + Sealed {
type Action: ActionName;
fn decode_reporting(
action: Self::Action,
payload: &serde_json::value::RawValue,
options: &crate::decode::DecodeOptions,
) -> Result<crate::decode::Decoded<Self>, crate::decode::DecodeError>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NoResponse {}
impl Sealed for NoResponse {}
impl Response for NoResponse {}
impl Validate for NoResponse {
fn validate_at(&self, _path: &mut ValidationPath, _out: &mut Violations) {}
}
impl Serialize for NoResponse {
fn serialize<S: serde::Serializer>(&self, _ser: S) -> Result<S::Ok, S::Error> {
match *self {}
}
}
impl<'de> serde::Deserialize<'de> for NoResponse {
fn deserialize<D: serde::Deserializer<'de>>(_de: D) -> Result<Self, D::Error> {
Err(serde::de::Error::custom("a SEND message has no response"))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UnknownAction;
impl fmt::Display for UnknownAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("unknown OCPP action for this version")
}
}
#[cfg(feature = "std")]
impl std::error::Error for UnknownAction {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UnknownVariant {
pub type_name: &'static str,
}
impl fmt::Display for UnknownVariant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "not a defined value of {}", self.type_name)
}
}
#[cfg(feature = "std")]
impl std::error::Error for UnknownVariant {}