legacylisten 0.2.0

A simple CLI audio player with strange features.
Documentation
use std::{error, fmt, io, path::StripPrefixError, string::FromUtf8Error};

use crossbeam_channel::RecvError;
use nix::errno::Errno;
use rodio::{decoder::DecoderError, PlayError, StreamError};

/// `legacylisten`'s Error type.
///
/// This type bundles all errors which could occur.
#[derive(Debug)]
pub enum Error
{
    Io(io::Error),
    Trash(trash::Error),
    Decoder(DecoderError),
    Stream(StreamError),
    Play(PlayError),
    Errno(Errno),
    Utf8(FromUtf8Error),
    /// The CSV file which stores the playing likelihoods and
    /// the volumes was malformatted.
    MalformattedSongsCsv,
    Recv(RecvError),
    Walkdir(walkdir::Error),
    StripPrefixError(StripPrefixError),
    MalformattedListOfCommandCharacters,
    L10nError(legacytranslate::Error),
    Custom(String),
    Vec(Vec<Error>),
}

impl fmt::Display for Error
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>
    {
        match self
        {
            Self::Io(err) => write!(f, "IO error: {}", err),
            Self::Trash(err) => write!(f, "Trash error: {}", err),
            Self::Decoder(err) => write!(f, "Decoder error: {}", err),
            Self::Stream(err) => write!(f, "Stream error: {}", err),
            Self::Play(err) => write!(f, "Play error: {}", err),
            Self::Errno(err) => write!(f, "Errno: {}", err),
            Self::Utf8(err) => write!(f, "From UTF8 error: {}", err),
            Self::MalformattedSongsCsv => write!(f, "Malformatted songs.csv file"),
            Self::Recv(err) => write!(f, "Recv error: {}", err),
            Self::Walkdir(err) => write!(f, "Walkdir error: {}", err),
            Self::StripPrefixError(err) => write!(f, "Strip prefix error: {}", err),
            Self::MalformattedListOfCommandCharacters =>
            {
                write!(f, "Malformatted list of command characters")
            }
            Self::L10nError(err) => write!(f, "Localization error: {}", err),
            Self::Custom(err) => write!(f, "Custom error: {}", err),
            Self::Vec(v) =>
            {
                if v.len() == 1
                {
                    write!(f, "{}", v[0])
                }
                else
                {
                    writeln!(f, "Multiple errors ({}):", v.len())?;
                    for (i, e) in v.iter().enumerate()
                    {
                        write!(f, "{}: {}", i, e)?;
                        if i != v.len() + 1
                        {
                            writeln!(f)?;
                        }
                    }

                    Ok(())
                }
            }
        }
    }
}

impl error::Error for Error {}

impl From<io::Error> for Error
{
    fn from(err: io::Error) -> Self
    {
        Self::Io(err)
    }
}

impl From<trash::Error> for Error
{
    fn from(err: trash::Error) -> Self
    {
        Self::Trash(err)
    }
}

impl From<DecoderError> for Error
{
    fn from(err: DecoderError) -> Self
    {
        Self::Decoder(err)
    }
}

impl From<StreamError> for Error
{
    fn from(err: StreamError) -> Self
    {
        Self::Stream(err)
    }
}

impl From<PlayError> for Error
{
    fn from(err: PlayError) -> Self
    {
        Self::Play(err)
    }
}

impl From<Errno> for Error
{
    fn from(err: Errno) -> Self
    {
        Self::Errno(err)
    }
}

impl From<FromUtf8Error> for Error
{
    fn from(err: FromUtf8Error) -> Self
    {
        Self::Utf8(err)
    }
}

impl From<RecvError> for Error
{
    fn from(err: RecvError) -> Self
    {
        Self::Recv(err)
    }
}

impl From<walkdir::Error> for Error
{
    fn from(err: walkdir::Error) -> Self
    {
        Self::Walkdir(err)
    }
}

impl From<StripPrefixError> for Error
{
    fn from(err: StripPrefixError) -> Self
    {
        Self::StripPrefixError(err)
    }
}

impl From<legacytranslate::Error> for Error
{
    fn from(err: legacytranslate::Error) -> Self
    {
        Self::L10nError(err)
    }
}

impl From<String> for Error
{
    fn from(err: String) -> Self
    {
        Self::Custom(err)
    }
}

impl<T> From<Vec<T>> for Error
where
    Self: From<T>,
{
    fn from(err: Vec<T>) -> Self
    {
        Self::Vec(err.into_iter().map(Into::into).collect())
    }
}