annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
770
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 use pyo3::prelude::*;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use pyo3::types::PyBytes;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use touhou_formats::th06::pbg3;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use std::fs::File;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use std::io::BufReader;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 #[pyclass]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 struct PBG3 {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 inner: pbg3::PBG3<BufReader<File>>,
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 #[pymethods]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 impl PBG3 {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 #[staticmethod]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 fn from_filename(filename: &str) -> PyResult<PBG3> {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 let inner = pbg3::from_path_buffered(filename)?;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 Ok(PBG3 {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 inner
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 })
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 #[getter]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 fn file_list(&self) -> Vec<String> {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 self.inner.list_files().cloned().collect()
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 fn list_files(&self) -> Vec<String> {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 self.inner.list_files().cloned().collect()
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 fn get_file(&mut self, py: Python, name: &str) -> PyObject {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 let data = self.inner.get_file(name, true).unwrap();
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 PyBytes::new(py, &data).into_py(py)
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 #[pymodule]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 fn libtouhou(_py: Python, m: &PyModule) -> PyResult<()> {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 m.add_class::<PBG3>()?;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 Ok(())
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 }