use crate::codec::{Decode, Encode, Reader, Result, Writer};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum StateError {
ServiceNotAllowed,
ServiceUnknown,
Other(u8),
}
impl StateError {
#[must_use]
pub const fn from_u8(v: u8) -> Self {
match v {
1 => Self::ServiceNotAllowed,
2 => Self::ServiceUnknown,
other => Self::Other(other),
}
}
#[must_use]
pub const fn as_u8(self) -> u8 {
match self {
Self::ServiceNotAllowed => 1,
Self::ServiceUnknown => 2,
Self::Other(v) => v,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ServiceError {
OperationNotPossible,
ServiceNotSupported,
OtherReason,
PduTooLong,
DecipheringError,
InvocationCounterError,
Other(u8),
}
impl ServiceError {
#[must_use]
pub const fn from_u8(v: u8) -> Self {
match v {
1 => Self::OperationNotPossible,
2 => Self::ServiceNotSupported,
3 => Self::OtherReason,
4 => Self::PduTooLong,
5 => Self::DecipheringError,
6 => Self::InvocationCounterError,
other => Self::Other(other),
}
}
#[must_use]
pub const fn as_u8(self) -> u8 {
match self {
Self::OperationNotPossible => 1,
Self::ServiceNotSupported => 2,
Self::OtherReason => 3,
Self::PduTooLong => 4,
Self::DecipheringError => 5,
Self::InvocationCounterError => 6,
Self::Other(v) => v,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExceptionResponse {
pub state_error: StateError,
pub service_error: ServiceError,
pub expected_invocation_counter: Option<u32>,
}
impl Encode for ExceptionResponse {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_u8(self.state_error.as_u8())?;
w.write_u8(self.service_error.as_u8())?;
if let Some(ic) = self.expected_invocation_counter {
w.write_u32(ic)?;
}
Ok(())
}
}
impl<'a> Decode<'a> for ExceptionResponse {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
let state_error = StateError::from_u8(r.u8()?);
let service_error = ServiceError::from_u8(r.u8()?);
let expected_invocation_counter =
if service_error == ServiceError::InvocationCounterError && r.remaining() >= 4 {
Some(r.u32()?)
} else {
None
};
Ok(Self { state_error, service_error, expected_invocation_counter })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConfirmedServiceError {
pub service: u8,
pub error_class: u8,
pub error_code: u8,
}
impl Encode for ConfirmedServiceError {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_bytes(&[self.service, self.error_class, self.error_code])
}
fn encoded_len(&self) -> usize {
3
}
}
impl<'a> Decode<'a> for ConfirmedServiceError {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self { service: r.u8()?, error_class: r.u8()?, error_code: r.u8()? })
}
}