#[cfg(feature = "alloc")]
use alloc::{
boxed::Box,
string::{String, ToString},
};
#[allow(dead_code)]
pub(crate) type Result<T> = core::result::Result<T, TensorError>;
#[derive(Debug, thiserror::Error)]
pub enum TensorError {
#[error(transparent)]
ShapeError(#[from] ndarray::ShapeError),
#[cfg(feature = "anyhow")]
#[error(transparent)]
AnyError(#[from] anyhow::Error),
#[cfg(feature = "alloc")]
#[error(transparent)]
BoxError(#[from] Box<dyn core::error::Error + Send + Sync>),
#[cfg(feature = "serde")]
#[error(transparent)]
DeserializeError(#[from] serde::de::value::Error),
#[error(transparent)]
FmtError(#[from] core::fmt::Error),
#[cfg(feature = "serde_json")]
#[error(transparent)]
JsonError(#[from] serde_json::Error),
#[cfg(feature = "std")]
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
#[cfg(feature = "rand")]
UniformError(#[from] rand_distr::uniform::Error),
#[cfg(feature = "alloc")]
#[error("Unknown Error: {0}")]
Unknown(String),
}
#[cfg(feature = "alloc")]
impl TensorError {
pub fn box_error<E>(error: E) -> Self
where
E: 'static + Send + Sync + core::error::Error,
{
Self::BoxError(Box::new(error))
}
pub fn unknown<S>(error: S) -> Self
where
S: ToString,
{
Self::Unknown(error.to_string())
}
}
#[cfg(feature = "alloc")]
impl From<String> for TensorError {
fn from(value: String) -> Self {
Self::Unknown(value)
}
}
#[cfg(feature = "alloc")]
impl From<&str> for TensorError {
fn from(value: &str) -> Self {
Self::Unknown(String::from(value))
}
}