rust/hg-pyo3/src/transaction.rs
author Rapha?l Gom?s <rgomes@octobus.net>
Tue, 18 Feb 2025 11:33:20 +0100
changeset 52976 d934d730c6c2
parent 52827 50c0c74ca266
permissions -rw-r--r--
pyO3: remove useless (and wrong) `__doc__` attribute on modules PyO3 overrides the `__doc__` attributes to use the module's docstring.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52827
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     1
use hg::{transaction::Transaction, utils::files::get_bytes_from_path};
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     2
use pyo3::{intern, types::PyBytes, PyObject, Python};
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     3
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     4
/// Wrapper around a Python transaction object, to keep `hg-core` oblivious
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     5
/// of the fact it's being called from Python.
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     6
pub struct PyTransaction {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     7
    inner: PyObject,
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     8
}
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     9
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    10
impl PyTransaction {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    11
    pub fn new(inner: PyObject) -> Self {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    12
        Self { inner }
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    13
    }
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    14
}
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    15
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    16
impl Clone for PyTransaction {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    17
    fn clone(&self) -> Self {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    18
        Python::with_gil(|py| Self {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    19
            inner: self.inner.clone_ref(py),
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    20
        })
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    21
    }
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    22
}
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    23
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    24
impl Transaction for PyTransaction {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    25
    fn add(&mut self, file: impl AsRef<std::path::Path>, offset: usize) {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    26
        Python::with_gil(|py| {
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    27
            let file = PyBytes::new(py, &get_bytes_from_path(file.as_ref()));
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    28
            self.inner
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    29
                .call_method(py, intern!(py, "add"), (file, offset), None)
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    30
                .expect("transaction add failed");
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    31
        })
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    32
    }
50c0c74ca266 rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    33
}