Debian Codegraph @

rust/hg-pyo3/src/diff.rs

raw
1 use hg::revlog::diff::text_delta;
2 use hg::revlog::manifest;
3 use pyo3::pyfunction;
4 use pyo3::types::PyModule;
5 use pyo3::types::PyModuleMethods;
6 use pyo3::wrap_pyfunction;
7 use pyo3::Bound;
8 use pyo3::PyAny;
9 use pyo3::PyResult;
10 use pyo3::Python;
11
12 use crate::utils::new_submodule;
13 use crate::utils::take_buffer_with_slice;
14
15 #[pyfunction]
16 pub fn manifest_diff(
17 m1: Bound<'_, PyAny>,
18 m2: Bound<'_, PyAny>,
19 ) -> PyResult<Vec<u8>> {
20 // Safety: we keep `_buf` alive for the entire existence of the slices,
21 // as they are an internal detail of `manifest::manifest_delta`
22 let (_buf, m1) = unsafe { take_buffer_with_slice(&m1)? };
23 // Safety: same as above
24 let (_buf, m2) = unsafe { take_buffer_with_slice(&m2)? };
25 Ok(manifest::manifest_delta(&m1, &m2))
26 }
27
28 #[pyfunction]
29 pub fn line_diff(
30 m1: Bound<'_, PyAny>,
31 m2: Bound<'_, PyAny>,
32 ) -> PyResult<Vec<u8>> {
33 // Safety: we keep `_buf` alive for the entire existence of the slices,
34 // as they are an internal detail of diff::text_delta
35 let (_buf, m1) = unsafe { take_buffer_with_slice(&m1)? };
36 // Safety: same as above
37 let (_buf, m2) = unsafe { take_buffer_with_slice(&m2)? };
38 Ok(text_delta(&m1, &m2))
39 }
40
41 pub fn init_module<'py>(
42 py: Python<'py>,
43 package: &str,
44 ) -> PyResult<Bound<'py, PyModule>> {
45 let m = new_submodule(py, package, "diff")?;
46 m.add_function(wrap_pyfunction!(manifest_diff, &m)?)?;
47 m.add_function(wrap_pyfunction!(line_diff, &m)?)?;
48
49 Ok(m)
50 }