selene-core 0.4.2

selene-core is the backend for Selene, a local-first music player
Documentation
use std::{fs::File, io::Read};

use serde::{Deserialize, Serialize};

mod format_byte_checks;
pub use format_byte_checks::*;

/// All recognized audio containers
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Serialize, Deserialize)]
pub enum ContainerFormat {
    Flac,
    Mpa,
    Ogg,
    Wav,
    Aiff,
    Ape,
}

impl ContainerFormat {
    /// Gets container type by reading magic bytes
    ///
    /// Returns none if the container type is unsupported or unrecognized, or cannot be read
    pub fn from_file(handle: &mut File) -> Option<Self> {
        let mut buf = [0_u8; 16];

        handle.read_exact(&mut buf).ok()?;

        let container = if is_flac(&buf) {
            ContainerFormat::Flac
        } else if is_ogg(&buf) {
            ContainerFormat::Ogg
        } else if is_mpa(&buf) {
            ContainerFormat::Mpa
        } else if is_wav(&buf) {
            ContainerFormat::Wav
        } else if is_aiff(&buf) {
            ContainerFormat::Aiff
        } else if is_ape(&buf) {
            ContainerFormat::Ape
        } else {
            return None;
        };
        Some(container)
    }

    /// Returns the name as of the container that ffmpeg uses for encoding (the "`format_name`")
    #[must_use]
    pub fn format_name(&self) -> &str {
        match self {
            ContainerFormat::Aiff => "aiff",
            ContainerFormat::Ape => "ape",
            ContainerFormat::Flac => "flac",
            ContainerFormat::Mpa => "mp3",
            ContainerFormat::Ogg => "ogg",
            ContainerFormat::Wav => "wav",
        }
    }
}