Debian Codegraph @

rust/hg-pyo3/src/transaction.rs

raw
1 use hg::transaction::Transaction;
2 use hg::utils::files::get_bytes_from_path;
3 use pyo3::intern;
4 use pyo3::types::PyBytes;
5 use pyo3::Py;
6 use pyo3::PyAny;
7 use pyo3::Python;
8
9 /// Wrapper around a Python transaction object, to keep `hg-core` oblivious
10 /// of the fact it's being called from Python.
11 pub struct PyTransaction {
12 inner: Py<PyAny>,
13 }
14
15 impl PyTransaction {
16 pub fn new(inner: Py<PyAny>) -> Self {
17 Self { inner }
18 }
19 }
20
21 impl Clone for PyTransaction {
22 fn clone(&self) -> Self {
23 Python::attach(|py| Self { inner: self.inner.clone_ref(py) })
24 }
25 }
26
27 impl Transaction for PyTransaction {
28 fn add(&mut self, file: impl AsRef<std::path::Path>, offset: usize) {
29 Python::attach(|py| {
30 let file = PyBytes::new(py, &get_bytes_from_path(file.as_ref()));
31 self.inner
32 .call_method(py, intern!(py, "add"), (file, offset), None)
33 .expect("transaction add failed");
34 })
35 }
36 }