comparison rust/hg-cpython/src/filepatterns.rs @ 42960:7a01778bc7b7

rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf Differential Revision: https://phab.mercurial-scm.org/D6774
author Rapha?l Gom?s <rgomes@octobus.net>
date Sun, 01 Sep 2019 20:53:14 +0200
parents ce6797ef6eab
children a264e8a91798
comparison
equal deleted inserted replaced
42959:3fe40dd6355d 42960:7a01778bc7b7
11 //! and can be used as replacement for the the pure `filepatterns` Python 11 //! and can be used as replacement for the the pure `filepatterns` Python
12 //! module. 12 //! module.
13 //! 13 //!
14 use crate::exceptions::{PatternError, PatternFileError}; 14 use crate::exceptions::{PatternError, PatternFileError};
15 use cpython::{ 15 use cpython::{
16 PyBytes, PyDict, PyModule, PyObject, PyResult, PyTuple, Python, ToPyObject, 16 PyBytes, PyDict, PyModule, PyObject, PyResult, PyString, PyTuple, Python,
17 ToPyObject,
17 }; 18 };
18 use hg::{build_single_regex, read_pattern_file, LineNumber, PatternTuple}; 19 use hg::{
20 build_single_regex, read_pattern_file, utils::files::get_path_from_bytes,
21 LineNumber, PatternTuple,
22 };
23 use std::path::PathBuf;
19 24
20 /// Rust does not like functions with different return signatures. 25 /// Rust does not like functions with different return signatures.
21 /// The 3-tuple version is always returned by the hg-core function, 26 /// The 3-tuple version is always returned by the hg-core function,
22 /// the (potential) conversion is handled at this level since it is not likely 27 /// the (potential) conversion is handled at this level since it is not likely
23 /// to have any measurable impact on performance. 28 /// to have any measurable impact on performance.
31 py: Python, 36 py: Python,
32 file_path: PyObject, 37 file_path: PyObject,
33 warn: bool, 38 warn: bool,
34 source_info: bool, 39 source_info: bool,
35 ) -> PyResult<PyTuple> { 40 ) -> PyResult<PyTuple> {
36 match read_pattern_file(file_path.extract::<PyBytes>(py)?.data(py), warn) { 41 let bytes = file_path.extract::<PyBytes>(py)?;
42 let path = get_path_from_bytes(bytes.data(py));
43 match read_pattern_file(path, warn) {
37 Ok((patterns, warnings)) => { 44 Ok((patterns, warnings)) => {
38 if source_info { 45 if source_info {
39 let itemgetter = |x: &PatternTuple| { 46 let itemgetter = |x: &PatternTuple| {
40 (PyBytes::new(py, &x.0), x.1, PyBytes::new(py, &x.2)) 47 (PyBytes::new(py, &x.0), x.1, PyBytes::new(py, &x.2))
41 }; 48 };
56 } 63 }
57 } 64 }
58 65
59 fn warnings_to_py_bytes( 66 fn warnings_to_py_bytes(
60 py: Python, 67 py: Python,
61 warnings: &[(Vec<u8>, Vec<u8>)], 68 warnings: &[(PathBuf, Vec<u8>)],
62 ) -> Vec<(PyBytes, PyBytes)> { 69 ) -> Vec<(PyString, PyBytes)> {
63 warnings 70 warnings
64 .iter() 71 .iter()
65 .map(|(path, syn)| (PyBytes::new(py, path), PyBytes::new(py, syn))) 72 .map(|(path, syn)| {
73 (
74 PyString::new(py, &path.to_string_lossy()),
75 PyBytes::new(py, syn),
76 )
77 })
66 .collect() 78 .collect()
67 } 79 }
68 80
69 fn build_single_regex_wrapper( 81 fn build_single_regex_wrapper(
70 py: Python, 82 py: Python,