minacalc_rs/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Custom error types for minacalc operations
5#[derive(Debug)]
6pub enum MinaCalcError {
7    /// Calculator creation failed
8    CalculatorCreationFailed,
9    /// No notes provided for calculation
10    NoNotesProvided,
11    /// Invalid music rate (must be positive)
12    InvalidMusicRate(f32),
13    /// Invalid score goal (must be between 0 and 100)
14    InvalidScoreGoal(f32),
15    /// Calculation failed
16    CalculationFailed(String),
17    /// Invalid note data
18    InvalidNoteData(String),
19    /// Memory allocation failed
20    MemoryAllocationFailed,
21    /// Internal C++ error
22    InternalError(String),
23    /// Osu! related error
24    OsuError(OsuError),
25}
26
27/// Custom error types for osu! beatmap operations
28#[derive(Debug)]
29pub enum OsuError {
30    /// Unsupported column position
31    UnsupportedColumn(f32),
32    /// Unsupported hit object kind
33    UnsupportedHitObjectKind(String),
34    /// Failed to convert hit object
35    HitObjectConversion(String),
36    /// Beatmap validation failed
37    ValidationFailed(String),
38    /// Failed to parse beatmap file
39    ParseFailed(String),
40    /// Unsupported game mode
41    UnsupportedGameMode(String),
42    /// Unsupported key count
43    UnsupportedKeyCount(f32),
44}
45
46impl fmt::Display for MinaCalcError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            MinaCalcError::CalculatorCreationFailed => write!(f, "Failed to create calculator"),
50            MinaCalcError::NoNotesProvided => write!(f, "No notes provided for calculation"),
51            MinaCalcError::InvalidMusicRate(rate) => write!(f, "Invalid music rate: {} (must be positive)", rate),
52            MinaCalcError::InvalidScoreGoal(goal) => write!(f, "Invalid score goal: {} (must be between 0 and 100)", goal),
53            MinaCalcError::CalculationFailed(msg) => write!(f, "Calculation failed: {}", msg),
54            MinaCalcError::InvalidNoteData(msg) => write!(f, "Invalid note data: {}", msg),
55            MinaCalcError::MemoryAllocationFailed => write!(f, "Memory allocation failed"),
56            MinaCalcError::InternalError(msg) => write!(f, "Internal error: {}", msg),
57            MinaCalcError::OsuError(osu_err) => write!(f, "Osu! error: {}", osu_err),
58        }
59    }
60}
61
62impl fmt::Display for OsuError {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        match self {
65            OsuError::UnsupportedColumn(x) => write!(f, "Unsupported column position: {}", x),
66            OsuError::UnsupportedHitObjectKind(kind) => write!(f, "Unsupported hit object kind: {}", kind),
67            OsuError::HitObjectConversion(msg) => write!(f, "Hit object conversion failed: {}", msg),
68            OsuError::ValidationFailed(msg) => write!(f, "Beatmap validation failed: {}", msg),
69            OsuError::ParseFailed(msg) => write!(f, "Failed to parse beatmap: {}", msg),
70            OsuError::UnsupportedGameMode(mode) => write!(f, "Unsupported game mode: {}", mode),
71            OsuError::UnsupportedKeyCount(count) => write!(f, "Unsupported key count: {}", count),
72        }
73    }
74}
75
76impl Error for MinaCalcError {}
77impl Error for OsuError {}
78
79// Conversion from OsuError to MinaCalcError
80impl From<OsuError> for MinaCalcError {
81    fn from(osu_err: OsuError) -> Self {
82        MinaCalcError::OsuError(osu_err)
83    }
84}
85
86// Type alias for common result types
87pub type MinaCalcResult<T> = Result<T, MinaCalcError>;
88pub type OsuResult<T> = Result<T, OsuError>;