refactor: create performance calculators through traits (#33)

* create perf calc through IntoPerformance traits

* added tests

* adjusted documentation

* fix doctest

* simplify From impls
This commit is contained in:
Badewanne3
2024-04-01 19:14:38 +02:00
committed by GitHub
parent 616f2b5758
commit 9acf85e934
24 changed files with 772 additions and 446 deletions
+54
View File
@@ -13,6 +13,7 @@ impl<'map, M: IGameMode> MapOrAttrs<'map, M> {
/// If `self` is of variant `Map`, store `attrs` in `self`, and return a
/// mutable reference to it.
pub fn insert_attrs(&mut self, attrs: M::DifficultyAttributes) -> &mut M::DifficultyAttributes {
// TODO: dont match, just overwrite
match self {
MapOrAttrs::Map(_) => {
*self = Self::Attrs(attrs);
@@ -67,3 +68,56 @@ where
}
}
}
impl<'map, M: IGameMode> From<Converted<'map, M>> for MapOrAttrs<'map, M> {
fn from(converted: Converted<'map, M>) -> Self {
Self::Map(converted)
}
}
macro_rules! from_attrs {
(
$(
$module:ident {
$mode:ident, $diff:ident, $perf:ident
}
,)*
) => {
$(
impl From<crate::$module::$diff> for MapOrAttrs<'_, crate::$module::$mode> {
fn from(attrs: crate::$module::$diff) -> Self {
Self::Attrs(attrs)
}
}
impl From<crate::$module::$perf> for MapOrAttrs<'_, crate::$module::$mode> {
fn from(attrs: crate::$module::$perf) -> Self {
Self::Attrs(attrs.difficulty)
}
}
)*
};
}
from_attrs!(
osu {
Osu,
OsuDifficultyAttributes,
OsuPerformanceAttributes
},
taiko {
Taiko,
TaikoDifficultyAttributes,
TaikoPerformanceAttributes
},
catch {
Catch,
CatchDifficultyAttributes,
CatchPerformanceAttributes
},
mania {
Mania,
ManiaDifficultyAttributes,
ManiaPerformanceAttributes
},
);