use std::fmt;
use microrti_core::{ExceptionKind, RtiError};
use crate::error::{DecoderError, EncoderError};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum Reason {
#[error(transparent)]
Decoder(#[from] DecoderError),
#[error(transparent)]
Encoder(#[from] EncoderError),
#[error("expected {expected}, found {found}")]
TypeMismatch {
expected: String,
found: String,
},
#[error("{detail}")]
OutOfRange {
detail: String,
},
#[error("{detail}")]
UnknownEnumerator {
detail: String,
},
#[error("{detail}")]
Alternative {
detail: String,
},
#[error("{detail}")]
Field {
detail: String,
},
#[error("{detail}")]
InvalidValue {
detail: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Failure {
pub member: Option<String>,
pub path: String,
pub offset: usize,
pub reason: Reason,
}
impl fmt::Display for Failure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(member) = &self.member {
write!(f, "{member}: ")?;
}
write!(f, "{} at offset {}: {}", self.path, self.offset, self.reason)
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum CodecError {
#[error("recursive data type definition: {cycle}")]
RecursiveDataType {
cycle: String,
},
#[error("the data type '{data_type}' nests data types deeper than {limit} levels")]
NestingTooDeep {
data_type: String,
limit: usize,
},
#[error("the FDD has no data type '{name}'")]
UnknownDataType {
name: String,
},
#[error("the FDD has no {kind} with the handle {handle}")]
UnknownHandle {
kind: &'static str,
handle: u32,
},
#[error("'{member}' is not {kind} of the class {class}")]
NotAMember {
kind: &'static str,
class: String,
member: String,
},
#[error("cannot encode {0}")]
Encode(Box<Failure>),
#[error("cannot decode {0}")]
Decode(Box<Failure>),
}
impl CodecError {
pub fn failure(&self) -> Option<&Failure> {
match self {
CodecError::Encode(failure) | CodecError::Decode(failure) => Some(failure),
_ => None,
}
}
pub fn path(&self) -> Option<&str> {
self.failure().map(|failure| failure.path.as_str())
}
pub fn offset(&self) -> Option<usize> {
self.failure().map(|failure| failure.offset)
}
pub fn reason(&self) -> Option<&Reason> {
self.failure().map(|failure| &failure.reason)
}
pub(crate) fn with_member(mut self, member: impl FnOnce() -> String) -> Self {
if let CodecError::Encode(failure) | CodecError::Decode(failure) = &mut self {
failure.member = Some(member());
}
self
}
}
impl From<CodecError> for RtiError {
fn from(error: CodecError) -> Self {
let kind = match &error {
CodecError::Encode(_) => ExceptionKind::CouldNotEncode,
CodecError::Decode(_) => ExceptionKind::CouldNotDecode,
CodecError::RecursiveDataType { .. } | CodecError::NestingTooDeep { .. } => {
ExceptionKind::InconsistentFOM
}
CodecError::UnknownDataType { .. }
| CodecError::UnknownHandle { .. }
| CodecError::NotAMember { .. } => ExceptionKind::RTIinternalError,
};
RtiError::new(kind, error.to_string())
}
}
#[derive(Debug)]
pub(crate) struct Fail {
segments: Vec<String>,
offset: usize,
reason: Reason,
}
impl Fail {
pub(crate) fn new(offset: usize, reason: Reason) -> Self {
Self { segments: Vec::new(), offset, reason }
}
pub(crate) fn decoder(at: usize, error: DecoderError) -> Self {
let offset = match &error {
DecoderError::Truncated { offset, .. } => *offset,
_ => at,
};
Self::new(offset, Reason::Decoder(error))
}
pub(crate) fn inside(mut self, segment: String) -> Self {
self.segments.push(segment);
self
}
pub(crate) fn into_failure(self, root: &str) -> Box<Failure> {
let mut path = root.to_owned();
for segment in self.segments.iter().rev() {
path.push_str(segment);
}
Box::new(Failure { member: None, path, offset: self.offset, reason: self.reason })
}
}