use alloc::{
borrow::Cow,
format,
string::String,
vec::Vec,
};
use burn::{
prelude::Shape,
tensor::Slice,
};
use crate::errors::{
BunsenError,
BunsenErrorKind,
Detailed,
};
#[derive(Debug, Clone, thiserror::Error, PartialEq)]
pub enum SlicingError {
#[error("slices out of bounds: {msg}")]
OutOfBounds {
msg: String,
shape: Shape,
slices: Vec<Slice>,
},
#[error("slices of the wrong rank: {msg}")]
InvalidRank {
msg: String,
shape: Shape,
slices: Vec<Slice>,
},
}
impl Detailed for SlicingError {
fn details(&self) -> Option<Cow<'_, str>> {
let (Self::OutOfBounds { shape, slices, .. } | Self::InvalidRank { shape, slices, .. }) =
self;
Some(Cow::Owned(format!("shape: {shape}\nslices: {slices:?}")))
}
}
impl From<SlicingError> for BunsenError {
#[track_caller]
fn from(error: SlicingError) -> Self {
BunsenError::from_detailed(BunsenErrorKind::Illegal, error)
}
}