osu_file_parser/osu_file/hitobjects/
error.rs

1//! Module defining `error` types that's used for the `hitobject` related modules.
2
3use std::num::ParseIntError;
4
5use strum_macros::{EnumString, IntoStaticStr};
6use thiserror::Error;
7
8use crate::helper::macros::verbose_error_to_error;
9
10#[derive(Debug, Error)]
11#[error(transparent)]
12pub struct ParseError(#[from] ParseHitObjectError);
13
14#[derive(Debug, Error)]
15#[error("Expected combo skip count to be 3 bits")]
16pub struct ComboSkipCountTooHigh;
17
18#[derive(Debug, Error, IntoStaticStr, EnumString)]
19#[non_exhaustive]
20/// Error used when there was a problem parsing a `str` into a `ColonSet`.
21pub enum ParseColonSetError {
22    /// When the first item failed to parse.
23    #[error("Failed to parse the first item")]
24    InvalidFirstItem,
25    /// The colon separator is missing.
26    #[error("The colon separator is missing")]
27    MissingSeparator,
28    #[error("Failed to parse the second item")]
29    InvalidSecondItem,
30}
31
32verbose_error_to_error!(ParseColonSetError);
33
34#[derive(Debug, Error, EnumString, IntoStaticStr)]
35#[non_exhaustive]
36/// Error used when there was a problem parsing a `str` into a [`HitObject`][super::HitObject].
37pub enum ParseHitObjectError {
38    /// Invalid `hit_sound` value.
39    #[error("Invalid `hit_sound` value")]
40    InvalidHitSound,
41    /// Missing `hit_sound` field.
42    #[error("Missing `hit_sound` field")]
43    MissingHitSound,
44    /// Missing `hit_sample` field.
45    #[error("Missing `hit_sample` field")]
46    MissingHitSample,
47    /// Invalid `hit_sample` value.
48    #[error("Invalid `hit_sample` value")]
49    InvalidHitSample,
50    /// Invalid `x` value.
51    #[error("Invalid `x` value")]
52    InvalidX,
53    /// Missing `x` field.
54    #[error("Missing `x` field")]
55    MissingX,
56    /// Invalid `y` value.
57    #[error("Invalid `y` value")]
58    InvalidY,
59    /// Missing `y` field.
60    #[error("Missing `y` field")]
61    MissingY,
62    /// Missing `obj_type` field.
63    #[error("Missing `obj_type` field")]
64    MissingObjType,
65    /// Invalid `obj_type` value.
66    #[error("Invalid `obj_type` value")]
67    InvalidObjType,
68    /// Missing `time` field.
69    #[error("Missing `time` field")]
70    MissingTime,
71    /// Invalid `time` value.
72    #[error("Invalid `time` value")]
73    InvalidTime,
74    /// Invalid `curve_type` value.
75    #[error("Invalid `curve_type` value")]
76    InvalidCurveType,
77    /// Missing `curve_type` field.
78    #[error("Missing `curve_type` field")]
79    MissingCurveType,
80    /// Invalid `curve_point` value.
81    #[error("Invalid `curve_point` value")]
82    InvalidCurvePoint,
83    /// Missing `curve_point` field.
84    #[error("Missing `curve_point` field")]
85    MissingCurvePoint,
86    /// Invalid `edge_sound` value.
87    #[error("Invalid `edge_sound` value")]
88    InvalidEdgeSound,
89    /// Missing `edge_sound` field.
90    #[error("Missing `edge_sound` field")]
91    MissingEdgeSound,
92    /// Invalid `edge_set` value.
93    #[error("Invalid `edge_set` value")]
94    InvalidEdgeSet,
95    /// Missing `edge_set` field.
96    #[error("Missing `edge_set` field")]
97    MissingEdgeSet,
98    /// Invalid `slides_count` value.
99    #[error("Invalid `slides_count` value")]
100    InvalidSlidesCount,
101    /// Missing `slides_count` field.
102    #[error("Missing `slides_count` field")]
103    MissingSlidesCount,
104    /// Invalid `length` value.
105    #[error("Invalid `length` value")]
106    InvalidLength,
107    /// Missing `length` field.
108    #[error("Missing `length` field")]
109    MissingLength,
110    /// Invalid `end_time` value.
111    #[error("Invalid `end_time` value")]
112    InvalidEndTime,
113    /// Missing `end_time` field.
114    #[error("Missing `end_time` field")]
115    MissingEndTime,
116    /// Unknown object type.
117    #[error("Unknown object type")]
118    UnknownObjType,
119}
120
121verbose_error_to_error!(ParseHitObjectError);
122
123#[derive(Debug, Error, EnumString, IntoStaticStr)]
124#[non_exhaustive]
125/// Error used when there was a problem parsing a `str` into a [`hitsample`][super::types::HitSample].
126pub enum ParseHitSampleError {
127    #[error("Invalid `sample_set` value")]
128    InvalidSampleSet,
129    #[error("Invalid `index` value")]
130    InvalidIndex,
131    #[error("Invalid `volume` value")]
132    InvalidVolume,
133    #[error("Missing field separator")]
134    MissingSeparator,
135}
136
137verbose_error_to_error!(ParseHitSampleError);
138
139#[derive(Debug, Error)]
140#[non_exhaustive]
141/// Error used when there was a problem parsing a `str` into a [`sampleset`][super::types::SampleSet].
142pub enum ParseSampleSetError {
143    /// There was a problem parsing a `str` as an integer.
144    #[error("There was a problem parsing the `str` into an integer first")]
145    ParseValueError(#[from] ParseIntError),
146}
147
148#[derive(Debug, Error)]
149#[non_exhaustive]
150/// Error used when the user tried to set [`volume`][super::types::Volume]'s field as something invalid.
151pub enum VolumeSetError {
152    #[error("The volume was too high, expected to be in range 1 ~ 100")]
153    VolumeTooHigh,
154    /// The volume has a value `0`, which is "invalid".
155    /// In the osu file documentation, the volume of 0 means the `timingpoint`'s volume is used instead.
156    /// I handle that special case differently to make it more clear to the user what's going on.
157    #[error("The volume was attempted to set to 0, expected to be in range 1 ~ 100")]
158    VolumeTooLow,
159}
160
161#[derive(Debug, Error)]
162#[non_exhaustive]
163/// Error used when there was a problem parsing a `volume` from a `str`.
164pub enum ParseVolumeError {
165    #[error(transparent)]
166    VolumeSetError(#[from] VolumeSetError),
167    #[error(transparent)]
168    ParseIntError(#[from] ParseIntError),
169}
170
171#[derive(Debug, Error)]
172#[non_exhaustive]
173pub enum ParseCurveTypeError {
174    #[error("Unknown `CurveType` variant")]
175    UnknownVariant,
176}
177
178#[derive(Debug, Error)]
179#[non_exhaustive]
180pub enum ParseHitSoundError {
181    #[error(transparent)]
182    ParseIntError(#[from] ParseIntError),
183}