librmo 0.4.4

A library to manage media files and play them
Documentation
use std::fmt;
use std::path::Path;
use std::time::SystemTime;
use walkdir::DirEntry;

#[derive(Debug)]
#[allow(dead_code)]
pub enum FileExtension {
    None,
    Mpeg,
    Xflac,
    Invalid,
}

impl fmt::Display for FileExtension {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            FileExtension::None => write!(f, "NONE"),
            FileExtension::Mpeg => write!(f, "MPEG"),
            FileExtension::Xflac => write!(f, "XFLAC"),
            FileExtension::Invalid => write!(f, "INVALID"),
            #[allow(unreachable_patterns)]
            _ => write!(f, "NOT VALID ENUM"),
        }
    }
}

#[derive(Debug)]
pub struct MediaFile {
    pub path: String,
    pub mtime: SystemTime,
    pub file_type: FileExtension,
}

impl fmt::Display for MediaFile {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Customize so only `x` and `y` are denoted.
        write!(
            f,
            "path: {:?}, mtime: {:?}, file_type {}",
            self.path, self.mtime, self.file_type
        )
    }
}

impl MediaFile {
    /// Using a [`DirEntry`] returns a [`MediaFile`]. If the file cannot resolve its modified time, will use [`SystemTime::now`] as the replacement
    ///
    /// # Errors
    ///
    /// This function will return an error if .
    pub fn build(dir_entry: &DirEntry) -> Result<MediaFile, &'static str> {
        let path_buf = dir_entry.path().to_path_buf();
        let mtime = match dir_entry.metadata() {
            Err(_) => SystemTime::now(),
            Ok(x) => x.modified().unwrap_or_else(|_| SystemTime::now()),
        };
        let file_type = find_extension(&path_buf);
        let path = match path_buf.as_path().to_str() {
            Some(x) => x.to_string(),
            None => String::new(),
        };
        Ok(MediaFile {
            path,
            mtime,
            file_type,
        })
    }
}

/// Given a [`std::path`] returns an enum representing the compatible file extensions libRMO can work with
fn find_extension(filename: &Path) -> FileExtension {
    let extension = match filename.extension() {
        Some(x) => x,
        None => return FileExtension::Invalid,
    };
    let parts: &str = match extension.to_str() {
        Some(x) => x,
        None => return FileExtension::Invalid,
    };

    match parts {
        "flac" => FileExtension::Xflac,
        "mp3" => FileExtension::Mpeg,
        &_ => FileExtension::None,
    }
}