use std::borrow::Cow;
use std::convert::From;
use std::fmt::{Debug, Error};
use std::{fmt, io};
use thiserror::Error;
#[cfg(feature = "experimental-serde")]
use serde::{de, ser};
mod conversion;
mod decoding_error;
mod encoding_error;
mod illegal_operation;
mod incomplete;
mod io_error;
pub use conversion::ConversionOperationError;
pub use conversion::ConversionOperationResult;
pub use conversion::IonTypeExpectation;
pub use conversion::TypeExpectation;
pub use decoding_error::DecodingError;
pub use encoding_error::EncodingError;
pub use illegal_operation::IllegalOperation;
pub use incomplete::IncompleteError;
pub use io_error::IoError;
use crate::position::Position;
use crate::result::conversion::{ConversionError, ValueTypeExpectation};
pub type IonResult<T> = Result<T, IonError>;
#[non_exhaustive]
#[derive(Clone, Debug, Error, PartialEq)]
pub enum IonError {
#[error("{0}")]
Io(#[from] IoError),
#[error("{0}")]
Incomplete(#[from] IncompleteError),
#[error("{0}")]
Encoding(#[from] EncodingError),
#[error("{0}")]
Decoding(#[from] DecodingError),
#[error("{0}")]
IllegalOperation(#[from] IllegalOperation),
#[error("{0}")]
Conversion(#[from] ConversionError),
}
impl From<io::Error> for IonError {
fn from(io_error: io::Error) -> Self {
IoError::from(io_error).into()
}
}
impl From<io::ErrorKind> for IonError {
fn from(error_kind: io::ErrorKind) -> Self {
let io_error = io::Error::from(error_kind);
IoError::from(io_error).into()
}
}
impl From<fmt::Error> for IonError {
fn from(error: Error) -> Self {
EncodingError::new(error.to_string()).into()
}
}
impl From<IonError> for fmt::Error {
fn from(_ion_error: IonError) -> Self {
fmt::Error
}
}
impl<FromType, ToType> From<ConversionOperationError<FromType, ToType>> for IonError
where
FromType: ValueTypeExpectation,
ToType: TypeExpectation,
{
fn from(err: ConversionOperationError<FromType, ToType>) -> Self {
ConversionError::from(err).into()
}
}
#[cfg(feature = "experimental-serde")]
impl de::Error for IonError {
fn custom<T>(error: T) -> Self
where
T: std::fmt::Display,
{
DecodingError::new(error.to_string()).into()
}
}
#[cfg(feature = "experimental-serde")]
impl ser::Error for IonError {
fn custom<T>(error: T) -> Self
where
T: std::fmt::Display,
{
EncodingError::new(error.to_string()).into()
}
}
pub(crate) trait IonFailure {
fn incomplete(label: impl Into<Cow<'static, str>>, position: impl Into<Position>) -> Self;
fn decoding_error<S: Into<Cow<'static, str>>>(description: S) -> Self;
fn encoding_error<S: Into<Cow<'static, str>>>(description: S) -> Self;
fn illegal_operation<S: Into<Cow<'static, str>>>(operation: S) -> Self;
}
impl IonFailure for IonError {
fn incomplete(label: impl Into<Cow<'static, str>>, position: impl Into<Position>) -> Self {
IncompleteError::new(label, position).into()
}
fn decoding_error<S: Into<Cow<'static, str>>>(description: S) -> Self {
DecodingError::new(description).into()
}
fn encoding_error<S: Into<Cow<'static, str>>>(description: S) -> Self {
EncodingError::new(description).into()
}
fn illegal_operation<S: Into<Cow<'static, str>>>(operation: S) -> Self {
IllegalOperation::new(operation).into()
}
}
impl<T> IonFailure for IonResult<T> {
fn incomplete(label: impl Into<Cow<'static, str>>, position: impl Into<Position>) -> Self {
Err(IonError::incomplete(label, position))
}
fn decoding_error<S: Into<Cow<'static, str>>>(description: S) -> Self {
Err(IonError::decoding_error(description))
}
fn encoding_error<S: Into<Cow<'static, str>>>(description: S) -> Self {
Err(IonError::encoding_error(description))
}
fn illegal_operation<S: Into<Cow<'static, str>>>(operation: S) -> Self {
Err(IonError::illegal_operation(operation))
}
}