use thiserror::Error;
#[derive(Debug, Error)]
pub enum FrameworkError {
#[error("Core tensor error: {0}")]
Core(#[from] tenflowers_core::TensorError),
#[error("Dataset error: {0}")]
Dataset(String),
#[error("Framework error: {0}")]
Other(String),
}
impl FrameworkError {
pub fn other(msg: impl Into<String>) -> Self {
Self::Other(msg.into())
}
}
impl From<Box<dyn std::error::Error>> for FrameworkError {
fn from(e: Box<dyn std::error::Error>) -> Self {
Self::Other(e.to_string())
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for FrameworkError {
fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
Self::Other(e.to_string())
}
}
pub type Result<T> = std::result::Result<T, FrameworkError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_other_construction() {
let e = FrameworkError::other("something went wrong");
assert!(e.to_string().contains("something went wrong"));
}
#[test]
fn test_result_ok() {
let r: Result<u32> = Ok(7);
assert!(matches!(r, Ok(7)));
}
#[test]
fn test_result_err_other() {
let r: Result<u32> = Err(FrameworkError::other("fail"));
assert!(r.is_err());
}
#[test]
fn test_from_boxed_error() {
let boxed: Box<dyn std::error::Error> = "boxed".into();
let e = FrameworkError::from(boxed);
assert!(e.to_string().contains("boxed"));
}
}