aoe2rec_py/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use pyo3::prelude::*;

#[pymodule]
mod aoe2rec_py {
    use pyo3::{pyclass, pyfunction, pymethods, Bound, PyAny, PyResult, Python};
    use pythonize::pythonize;

    #[pyfunction]
    fn parse_rec(py: Python<'_>, data: Vec<u8>) -> PyResult<Bound<'_, PyAny>> {
        let rec = aoe2rec::Savegame::from_bytes(data.try_into().unwrap()).unwrap();
        let pyrec = pythonize(py, &rec).unwrap();
        Ok(pyrec)
    }

    #[pyclass]
    struct Savegame(aoe2rec::Savegame);
    #[pymethods]
    impl Savegame {
        #[new]
        fn from_bytes(data: Vec<u8>) -> PyResult<Self> {
            let savegame = aoe2rec::Savegame::from_bytes(data.try_into().unwrap()).unwrap();
            Ok(Savegame(savegame))
        }
    }
}