Skip to content

Commit 374783d

Browse files
committed
add: more functions and macros
1 parent 76ea7e8 commit 374783d

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/macros.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#[macro_export]
22
macro_rules! pyo3_py_methods {
3-
($type:ident, impl {$($others:item)*}) => {
3+
($type:ident, impl {$($others:tt)*}) => {
44
#[pymethods] impl $type {
55
$($others)*
66
#[getter]pub fn attrs(&self) -> String { self.as_string() }
77
#[getter]pub fn attrs_dict<'a>(&self, py: Python<'a>) -> PyResult<&'a PyDict> { self.as_dict(py) }
88
}
99
};
10-
($type:ident, {$($attr:ident: $returns:ident),*}, impl {$($others:item)*}) => {
10+
($type:ident, {$($attr:ident: $returns:ident),*}, impl {$($others:tt)*}) => {
1111
#[pymethods] impl $type {
1212
$(#[getter]pub fn $attr(&self) -> $returns {self.0.$attr})* $($others)*
1313
#[getter]pub fn attrs(&self) -> String { self.as_string() }
@@ -29,7 +29,7 @@ macro_rules! pyo3_py_methods {
2929
#[getter]pub fn attrs_dict<'a>(&self, py: Python<'a>) -> PyResult<&'a PyDict> { self.as_dict(py) }
3030
}
3131
};
32-
($type:ident, {$($attr:ident: $returns:ident),*}; fn {$( $func:ident: $ret:ident),*}; impl {$($others:item)*}) => {
32+
($type:ident, {$($attr:ident: $returns:ident),*}; fn {$( $func:ident: $ret:ident),*}; impl {$($others:tt)*}) => {
3333
#[pymethods] impl $type {
3434
$(#[getter]pub fn $attr(&self) -> $returns {self.0.$attr})*
3535
$(#[getter]pub fn $func(&self) -> $ret {self.0.$func()})*
@@ -73,6 +73,13 @@ macro_rules! async_not_enabled_err {
7373
};
7474
}
7575

76+
#[macro_export]
77+
macro_rules! invalid_gamemode_err {
78+
() => {
79+
crate::python::exceptions::InvalidGameMode::new_err("Invalid osu! gamemode")
80+
};
81+
}
82+
7683
#[macro_export]
7784
macro_rules! set_calculator {
7885
($target:ident.$attr:ident, $calculator:ident) => {
@@ -123,3 +130,16 @@ macro_rules! pyo3_add_classes {
123130
$($m.add_class::<$class>()?;)*
124131
};
125132
}
133+
134+
#[macro_export]
135+
macro_rules! set_with_py_str {
136+
($obj:ident, $attr:ident, $value:ident; {$($a:ident),*}) => {
137+
match $attr {
138+
$(stringify!($a) => $obj.$a = match $value {
139+
Some(v) => v.extract()?,
140+
None => None
141+
},)*
142+
_ => {}
143+
}
144+
};
145+
}

src/methods/common.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use peace_performance::GameMode;
2-
use pyo3::PyErr;
2+
use pyo3::{PyAny, PyErr};
33
use std::path::PathBuf;
44

55
use std::fs::File as SyncFile;
@@ -64,6 +64,37 @@ pub fn osu_mode_int_str(mode: u8) -> String {
6464
.into()
6565
}
6666

67+
#[inline(always)]
68+
pub fn str_into_osu_mode(str: &str) -> Result<GameMode, PyErr> {
69+
Ok(match str {
70+
"std" => GameMode::STD,
71+
"taiko" => GameMode::TKO,
72+
"ctb" => GameMode::CTB,
73+
"mania" => GameMode::MNA,
74+
_ => return Err(crate::invalid_gamemode_err!()),
75+
})
76+
}
77+
78+
#[inline(always)]
79+
pub fn int_into_osu_mode(int: u8) -> Result<GameMode, PyErr> {
80+
Ok(match int {
81+
0 => GameMode::STD,
82+
1 => GameMode::TKO,
83+
2 => GameMode::CTB,
84+
3 => GameMode::MNA,
85+
_ => return Err(crate::invalid_gamemode_err!()),
86+
})
87+
}
88+
89+
pub fn py_any_into_osu_mode(py_input: &PyAny) -> Result<GameMode, PyErr> {
90+
if let Ok(str) = py_input.extract::<String>() {
91+
return str_into_osu_mode(&str);
92+
} else if let Ok(int) = py_input.extract::<u8>() {
93+
return int_into_osu_mode(int);
94+
}
95+
Err(crate::invalid_gamemode_err!())
96+
}
97+
6798
#[cfg(any(feature = "async_tokio", feature = "async_std"))]
6899
/// Async sleep
69100
#[inline(always)]

src/python/exceptions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ use pyo3::{create_exception, exceptions::PyException};
33
create_exception!(peace_performance, ParseBeatmapError, PyException);
44
create_exception!(peace_performance, ReadFileError, PyException);
55
create_exception!(peace_performance, AsyncNotEnabledError, PyException);
6+
create_exception!(peace_performance, InvalidGameMode, PyException);

src/python/functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ pub fn read_beatmap_async(_py: Python, _path: PathBuf) -> PyResult<&PyAny> {
6262

6363
#[pyfunction]
6464
pub fn new_calculator() -> Calculator {
65-
Calculator::new()
65+
Calculator::new_empty()
6666
}

0 commit comments

Comments
 (0)