Mercurial > touhou
comparison python/src/lib.rs @ 770:f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 30 Aug 2022 18:23:55 +0200 |
parents | |
children | 7492d384d122 |
comparison
equal
deleted
inserted
replaced
769:cae5f15ca5ed | 770:f6c287745a67 |
---|---|
1 use pyo3::prelude::*; | |
2 use pyo3::types::PyBytes; | |
3 use touhou_formats::th06::pbg3; | |
4 use std::fs::File; | |
5 use std::io::BufReader; | |
6 | |
7 #[pyclass] | |
8 struct PBG3 { | |
9 inner: pbg3::PBG3<BufReader<File>>, | |
10 } | |
11 | |
12 #[pymethods] | |
13 impl PBG3 { | |
14 #[staticmethod] | |
15 fn from_filename(filename: &str) -> PyResult<PBG3> { | |
16 let inner = pbg3::from_path_buffered(filename)?; | |
17 Ok(PBG3 { | |
18 inner | |
19 }) | |
20 } | |
21 | |
22 #[getter] | |
23 fn file_list(&self) -> Vec<String> { | |
24 self.inner.list_files().cloned().collect() | |
25 } | |
26 | |
27 fn list_files(&self) -> Vec<String> { | |
28 self.inner.list_files().cloned().collect() | |
29 } | |
30 | |
31 fn get_file(&mut self, py: Python, name: &str) -> PyObject { | |
32 let data = self.inner.get_file(name, true).unwrap(); | |
33 PyBytes::new(py, &data).into_py(py) | |
34 } | |
35 } | |
36 | |
37 #[pymodule] | |
38 fn libtouhou(_py: Python, m: &PyModule) -> PyResult<()> { | |
39 m.add_class::<PBG3>()?; | |
40 Ok(()) | |
41 } |