rosu-map 0.2.1

Library to de- and encode .osu files
Documentation
use std::str::FromStr;

pub use self::decode::{General, GeneralKey, GeneralState, ParseGeneralError};

pub(crate) mod decode; // pub(crate) for intradoc-links

/// An osu! gamemode.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum GameMode {
    #[default]
    Osu,
    Taiko,
    Catch,
    Mania,
}

thiserror! {
    #[error("invalid game mode")]
    /// Error when failing to parse a [`GameMode`].
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub struct ParseGameModeError;
}

impl FromStr for GameMode {
    type Err = ParseGameModeError;

    fn from_str(mode: &str) -> Result<Self, Self::Err> {
        match mode {
            "0" => Ok(Self::Osu),
            "1" => Ok(Self::Taiko),
            "2" => Ok(Self::Catch),
            "3" => Ok(Self::Mania),
            _ => Err(ParseGameModeError),
        }
    }
}

impl From<u8> for GameMode {
    fn from(mode: u8) -> Self {
        match mode {
            0 => Self::Osu,
            1 => Self::Taiko,
            2 => Self::Catch,
            3 => Self::Mania,
            _ => Self::Osu,
        }
    }
}

/// The countdown type of a [`Beatmap`].
///
/// [`Beatmap`]: crate::beatmap::Beatmap
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum CountdownType {
    #[default]
    None,
    Normal,
    HalfSpeed,
    DoubleSpeed,
}

impl FromStr for CountdownType {
    type Err = ParseCountdownTypeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "0" | "None" => Ok(Self::None),
            "1" | "Normal" => Ok(Self::Normal),
            "2" | "Half speed" => Ok(Self::HalfSpeed),
            "3" | "Double speed" => Ok(Self::DoubleSpeed),
            _ => Err(ParseCountdownTypeError),
        }
    }
}

thiserror! {
    #[error("invalid countdown type")]
    /// Error when failing to parse a [`CountdownType`].
    #[derive(Debug)]
    pub struct ParseCountdownTypeError;
}