Skip to content

Commit

Permalink
feat: allow None for mod settings (#13)
Browse files Browse the repository at this point in the history
* refactor: slightly improved mods error

* feat: allow mod settings to be None

* fix: replace dict with Mapping for mods

* refactor: improve mods error for Difficulty too
  • Loading branch information
MaxOhn authored Dec 5, 2024
1 parent f103a91 commit cc3ac76
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 54 deletions.
6 changes: 3 additions & 3 deletions rosu_pp_py.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from enum import Enum
from typing import List, Optional, Union
from typing import List, Mapping, Optional, Union
from collections.abc import Iterator

GameMods = Union[int, str, GameMod, List[Union[GameMod, str, int]]]
GameMod = dict[str, Union[str, GameModSettings]]
GameMod = Mapping[str, Union[str, Optional[GameModSettings]]]
"""
Must contain item `'acronym': str` and optionally `'settings': GameModSettings`
"""
GameModSettings = dict[str, Union[bool, float, str]]
GameModSettings = Mapping[str, Union[bool, float, str]]

class GameMode(Enum):
"""
Expand Down
26 changes: 13 additions & 13 deletions src/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ impl PyDifficulty {
for (key, value) in kwargs {
extract_args! {
this.key = value {
mods: GameMods,
clock_rate: float,
ar: float,
ar_with_mods: bool,
cs: float,
cs_with_mods: bool,
hp: float,
hp_with_mods: bool,
od: float,
od_with_mods: bool,
passed_objects: int,
hardrock_offsets: bool,
lazer: bool,
mods: "type that matches GameMods alias",
clock_rate: "float",
ar: "float",
ar_with_mods: "bool",
cs: "float",
cs_with_mods: "bool",
hp: "float",
hp_with_mods: "bool",
od: "float",
od_with_mods: "bool",
passed_objects: "int",
hardrock_offsets: "bool",
lazer: "bool",
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ macro_rules! define_class {

macro_rules! extract_args {
( $this:ident . $key:ident = $value:ident {
$( $field:ident: $ty:ident, )+
$( $field:ident: $expected:literal, )+
} ) => {
match $key.extract()? {
$(
Expand All @@ -90,7 +90,7 @@ macro_rules! extract_args {
"kwarg '",
stringify!($field),
"': must be ",
stringify!($ty),
$expected,
)))?
}
),*
Expand Down
6 changes: 5 additions & 1 deletion src/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ impl<'py> FromPyObject<'py> for PyGameMod<'py> {

Ok(PyGameMod {
acronym: extract_struct_field(&acronym, "PyGameMod", "acronym")?,
settings: settings.as_ref().map(PyAnyMethods::extract).transpose()?,
settings: settings
.as_ref()
.map(PyAnyMethods::extract::<Option<Bound<'_, PyDict>>>)
.transpose()?
.flatten(),
})
}
}
Expand Down
50 changes: 25 additions & 25 deletions src/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,31 @@ impl PyPerformance {
for (key, value) in kwargs {
extract_args! {
this.key = value {
mods: GameMods,
clock_rate: float,
ar: float,
ar_with_mods: bool,
cs: float,
cs_with_mods: bool,
hp: float,
hp_with_mods: bool,
od: float,
od_with_mods: bool,
passed_objects: int,
hardrock_offsets: bool,
lazer: bool,
accuracy: float,
combo: int,
large_tick_hits: int,
small_tick_hits: int,
slider_end_hits: int,
n_geki: int,
n_katu: int,
n300: int,
n100: int,
n50: int,
misses: int,
hitresult_priority: HitResultPriority,
mods: "type that matches GameMods alias",
clock_rate: "float",
ar: "float",
ar_with_mods: "bool",
cs: "float",
cs_with_mods: "bool",
hp: "float",
hp_with_mods: "bool",
od: "float",
od_with_mods: "bool",
passed_objects: "int",
hardrock_offsets: "bool",
lazer: "bool",
accuracy: "float",
combo: "int",
large_tick_hits: "int",
small_tick_hits: "int",
slider_end_hits: "int",
n_geki: "int",
n_katu: "int",
n300: "int",
n100: "int",
n50: "int",
misses: "int",
hitresult_priority: "HitResultPriority",
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/score_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ impl PyScoreState {
for (key, value) in kwargs {
extract_args! {
this.key = value {
max_combo: int,
osu_large_tick_hits: int,
osu_small_tick_hits: int,
slider_end_hits: int,
n_geki: int,
n_katu: int,
n300: int,
n100: int,
n50: int,
misses: int,
max_combo: "int",
osu_large_tick_hits: "int",
osu_small_tick_hits: "int",
slider_end_hits: "int",
n_geki: "int",
n_katu: "int",
n300: "int",
n100: "int",
n50: "int",
misses: "int",
}
}
}
Expand Down

0 comments on commit cc3ac76

Please sign in to comment.