use std::{self, fmt::Display};
use serde::{de, ser};
use crate::dds::values::result::Error as DDSError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{0}")]
Message(String),
#[error("io::Error: {0}")]
Io(#[from] std::io::Error),
#[error("CDR serialization requires sequence length to be specified at the start.")]
SequenceLengthUnknown,
#[error("unexpected end of input")]
Eof,
#[error("Expected 0 or 1 as Boolean, got: {0}")]
BadBoolean(u8),
#[error("UTF-8 error: {0}")]
BadString(std::str::Utf8Error),
#[error("Bad Unicode character code: {0}")]
BadChar(u32),
#[error("Option value must have discriminant 0 or 1, read: {0}")]
BadOption(u32),
#[error("Trailing garbage, {:?} bytes", .0.len())]
TrailingCharacters(Vec<u8>),
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Self::Message(msg.to_string())
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Self::Message(msg.to_string())
}
}
impl From<Error> for DDSError {
fn from(ser_error: Error) -> Self {
Self::Serialization {
reason: format!("{:?}", ser_error),
}
}
}