ndbioimage 0.2.0

Read bio image formats using the bio-formats java package.
use strum::IntoStaticStr;
use thiserror::Error;

/// the error type used throughout the crate
#[derive(Debug, Error, IntoStaticStr)]
pub enum Error {
    /// an io error
    #[error(transparent)]
    IO(#[from] std::io::Error),
    /// an ndarray shape error
    #[error(transparent)]
    Shape(#[from] ndarray::ShapeError),
    #[cfg(feature = "bioformats_java")]
    /// an error from the j4rs java bridge
    #[error(transparent)]
    J4rs(#[from] j4rs::errors::J4RsError),
    /// an infallible conversion
    #[error(transparent)]
    Infallible(#[from] std::convert::Infallible),
    /// an integer parse error
    #[error(transparent)]
    ParseIntError(#[from] std::num::ParseIntError),
    /// an ome metadata error
    #[error(transparent)]
    Ome(#[from] ome_metadata::error::Error),
    #[cfg(feature = "bioformats_java")]
    /// an error while downloading (e.g. the bioformats jar)
    #[error(transparent)]
    Downloader(#[from] downloader::Error),
    /// an error parsing an enum string with strum
    #[error(transparent)]
    Strum(#[from] strum::ParseError),
    #[cfg(feature = "tiffwrite")]
    /// an indicatif progress bar template error
    #[error(transparent)]
    TemplateError(#[from] indicatif::style::TemplateError),
    #[cfg(feature = "tiffwrite")]
    /// an error from the tiffwrite crate
    #[error(transparent)]
    TiffWrite(#[from] tiffwrite::error::Error),
    #[cfg(feature = "tiffseq")]
    /// a yaml (de)serialization error
    #[error(transparent)]
    SerdeYaml(#[from] serde_yaml::Error),
    #[cfg(any(feature = "tiffseq", feature = "tiff"))]
    /// an error from the tiff crate
    #[error(transparent)]
    Tiff(#[from] tiff::TiffError),
    #[cfg(feature = "python")]
    /// a postcard (de)serialization error
    #[error(transparent)]
    PostCard(#[from] postcard::Error),
    #[cfg(feature = "czi")]
    /// an error from the libczi binding
    #[error(transparent)]
    LibCzi(#[from] libczirw_sys::error::Error),
    /// a regex error
    #[error(transparent)]
    RegexError(#[from] regex::Error),
    #[cfg(feature = "czi")]
    /// an xmltree error
    #[error(transparent)]
    XmlTree(#[from] xmltree::Error),
    #[cfg(feature = "czi")]
    /// an xmltree parse error
    #[error(transparent)]
    XmlTreeParse(#[from] xmltree::ParseError),
    #[cfg(feature = "czi")]
    /// a czi-specific error
    #[error(transparent)]
    Czi(#[from] crate::readers::czi::CziError),
    #[cfg(feature = "movie")]
    /// an error joining a tokio task
    #[error(transparent)]
    TokioJoin(#[from] tokio::task::JoinError),
    #[cfg(feature = "bioformats_rust")]
    /// an error from the bioformats rust crate
    #[error(transparent)]
    BioFormats(#[from] bioformats::error::BioFormatsError),

    /// the axis string could not be parsed
    #[error("invalid axis: {0}")]
    InvalidAxis(String),
    /// the axis was not found in the axes
    #[error("axis {0} not found in axes {1}")]
    AxisNotFound(String, String),
    /// a conversion error
    #[error("conversion error: {0}")]
    TryInto(String),
    /// the target file already exists
    #[error("file already exists {0}")]
    FileAlreadyExists(String),
    /// could not download ffmpeg
    #[error("could not download ffmpeg: {0}")]
    FfmpegDownload(String),
    /// an ffmpeg error
    #[error("FFmpeg error: {0}")]
    Ffmpeg(String),
    /// the index is out of bounds
    #[error("index {0} out of bounds {1}")]
    OutOfBounds(isize, isize),
    /// the axis was not included in the view
    #[error("axis {0} has length {1}, but was not included")]
    OutOfBoundsAxis(String, usize),
    /// the dimensionality of the data does not match
    #[error("dimensionality mismatch: {0} != {0}")]
    DimensionalityMismatch(usize, usize),
    /// the axis already has an operation
    #[error("axis {0}: {1} is already operated on!")]
    AxisAlreadyOperated(usize, String),
    /// not enough free dimensions
    #[error("not enough free dimensions")]
    NotEnoughFreeDimensions,
    /// cannot cast a pixel value to the requested type
    #[error("cannot cast {0} to {1}")]
    Cast(String, String),
    /// the view is empty
    #[error("empty view")]
    EmptyView,
    /// the color string could not be parsed
    #[error("invalid color: {0}")]
    InvalidColor(String),
    /// no image or pixels found in the metadata
    #[error("no image or pixels found")]
    NoImageOrPixels,
    /// the attenuation value is invalid
    #[error("invalid attenuation value: {0}")]
    InvalidAttenuation(String),
    /// the file name is invalid
    #[error("not a valid file name")]
    InvalidFileName,
    /// the file has no parent directory
    #[error("file has no parent")]
    NoParent,
    /// the pixel type is unknown
    #[error("unknown pixel type {0}")]
    UnknownPixelType(String),
    /// cannot compute the mean of an empty axis
    #[error("no mean")]
    NoMean,
    /// the tiff file lock is poisoned
    #[error("tiff is locked")]
    TiffLock,
    /// this feature is not implemented
    #[error("not implemented: {0}")]
    NotImplemented(String),
    /// a string could not be parsed
    #[error("cannot parse: {0}")]
    Parse(String),
    /// cannot convert the libczi pixel type
    #[error("cannot convert libczi pixel type: {0}")]
    Conversion(String),
    /// no reader could open the file
    #[error("no reader found for {0}, tried: {1}")]
    NoReader(String, String),
    /// the reader cannot open the file
    #[error("reader {0} cannot open file {1} because {2}")]
    InvalidReader(String, String, String),
    /// the file does not exist
    #[error("file does not exist: {0}")]
    FileDoesNotExist(String),
    /// cannot remove axes that have a size != 1
    #[error("cannot remove axes {0}, size {1} != 1")]
    SizeMismatch(String, usize),
}

impl Error {
    /// the name of the error variant as a static string
    pub fn variant_name(&self) -> &'static str {
        self.into()
    }
}