use core::fmt;
use alloc::string::String;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Decode {
reason: DecodeReason,
offset: usize,
},
LimitExceeded {
limit: &'static str,
value: usize,
},
Encode(&'static str),
InvalidReference(&'static str),
NotFound(&'static str),
InvalidValue(&'static str),
Scl(String),
Io(String),
Service {
class: u32,
code: i64,
},
DataAccess(i64),
Rejected {
invoke_id: Option<i64>,
reason_tag: u32,
code: i64,
},
ControlRejected {
add_cause: i64,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DecodeReason {
Truncated,
BadLength,
IndefiniteLength,
UnexpectedTag,
MissingField,
BadValue,
TooDeep,
TrailingBytes,
NotAscii,
NotProcessBusFrame,
}
pub const fn service_error_class(class: u32) -> Option<&'static str> {
Some(match class {
0 => "vmd-state",
1 => "application-reference",
2 => "definition",
3 => "resource",
4 => "service",
5 => "service-preempt",
6 => "time-resolution",
7 => "access",
8 => "initiate",
9 => "conclude",
10 => "cancel",
11 => "file",
12 => "others",
_ => return None,
})
}
pub const fn data_access_reason(code: i64) -> Option<&'static str> {
Some(match code {
0 => "object-invalidated",
1 => "hardware-fault",
2 => "temporarily-unavailable",
3 => "object-access-denied",
4 => "object-undefined",
5 => "invalid-address",
6 => "type-unsupported",
7 => "type-inconsistent",
8 => "object-attribute-inconsistent",
9 => "object-access-unsupported",
10 => "object-non-existent",
11 => "object-value-invalid",
_ => return None,
})
}
pub type Result<T> = core::result::Result<T, Error>;
impl Error {
pub(crate) const fn decode(reason: DecodeReason, offset: usize) -> Self {
Error::Decode { reason, offset }
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Decode { reason, offset } => write!(f, "decode error at byte {offset}: {reason:?}"),
Error::LimitExceeded { limit, value } => write!(f, "limit `{limit}` exceeded by {value}"),
Error::Encode(what) => write!(f, "encode error: {what}"),
Error::InvalidReference(what) => write!(f, "invalid object reference: {what}"),
Error::NotFound(what) => write!(f, "not found: {what}"),
Error::InvalidValue(what) => write!(f, "invalid value: {what}"),
Error::Scl(msg) => write!(f, "SCL: {msg}"),
Error::Io(msg) => write!(f, "transport: {msg}"),
Error::Service { class, code } => match service_error_class(*class) {
Some(name) => write!(f, "MMS service error: {name} (class {class}), code {code}"),
None => write!(f, "MMS service error: class {class}, code {code}"),
},
Error::DataAccess(code) => match data_access_reason(*code) {
Some(name) => write!(f, "MMS data access error {code} ({name})"),
None => write!(f, "MMS data access error {code}"),
},
Error::Rejected { invoke_id, reason_tag, code } => match invoke_id {
Some(id) => write!(f, "the peer rejected invoke {id}: reason {reason_tag}/{code}"),
None => write!(f, "the peer rejected the PDU: reason {reason_tag}/{code}"),
},
Error::ControlRejected { add_cause } => write!(f, "control rejected, AddCause {add_cause}"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::Io(e.to_string())
}
}