return Result<_, Self> instead of Option

This commit is contained in:
MaxOhn
2024-03-08 11:49:57 +01:00
parent d711840a11
commit e3dcec8584
2 changed files with 15 additions and 17 deletions
+7 -5
View File
@@ -82,7 +82,7 @@ impl<'map> Performance<'map> {
/// Attempt to convert the map to the specified mode.
///
/// Returns `None` if the conversion is incompatible or the internal
/// Returns `Err(self)` if the conversion is incompatible or the internal
/// beatmap was already replaced with difficulty attributes, i.e. if
/// [`Performance::from_attributes`] or [`Performance::generate_state`] was
/// called.
@@ -91,13 +91,15 @@ impl<'map> Performance<'map> {
/// the internal beatmap was replaced, use [`mode_or_ignore`] instead.
///
/// [`mode_or_ignore`]: Self::mode_or_ignore
pub fn try_mode(self, mode: GameMode) -> Option<Self> {
// Both variants have the same size
#[allow(clippy::result_large_err)]
pub fn try_mode(self, mode: GameMode) -> Result<Self, Self> {
match (self, mode) {
(Self::Osu(o), _) => o.try_mode(mode),
(Self::Osu(o), _) => o.try_mode(mode).map_err(Self::Osu),
(this @ Self::Taiko(_), GameMode::Taiko)
| (this @ Self::Catch(_), GameMode::Catch)
| (this @ Self::Mania(_), GameMode::Mania) => Some(this),
_ => None,
| (this @ Self::Mania(_), GameMode::Mania) => Ok(this),
(this, _) => Err(this),
}
}
+8 -12
View File
@@ -95,7 +95,7 @@ impl<'map> OsuPerformance<'map> {
/// Attempt to convert the map to the specified mode.
///
/// Returns `None` if the internal beatmap was already replaced with
/// Returns `Err(self)` if the internal beatmap was already replaced with
/// [`OsuDifficultyAttributes`], i.e. if
/// [`OsuPerformance::from_attributes`] or
/// [`OsuPerformance::generate_state`] was called.
@@ -104,18 +104,14 @@ impl<'map> OsuPerformance<'map> {
/// replaced, use [`mode_or_ignore`] instead.
///
/// [`mode_or_ignore`]: Self::mode_or_ignore
pub fn try_mode(self, mode: GameMode) -> Option<Performance<'map>> {
// The `Ok`-variant is larger in size
#[allow(clippy::result_large_err)]
pub fn try_mode(self, mode: GameMode) -> Result<Performance<'map>, Self> {
match mode {
GameMode::Osu => Some(Performance::Osu(self)),
GameMode::Taiko => TaikoPerformance::try_from(self)
.map(Performance::Taiko)
.ok(),
GameMode::Catch => CatchPerformance::try_from(self)
.map(Performance::Catch)
.ok(),
GameMode::Mania => ManiaPerformance::try_from(self)
.map(Performance::Mania)
.ok(),
GameMode::Osu => Ok(Performance::Osu(self)),
GameMode::Taiko => TaikoPerformance::try_from(self).map(Performance::Taiko),
GameMode::Catch => CatchPerformance::try_from(self).map(Performance::Catch),
GameMode::Mania => ManiaPerformance::try_from(self).map(Performance::Mania),
}
}