Patchwork [4,of,6] rust-filepatterns: fix type of warnings tuple to (bytes, bytes)

login
register
mail settings
Submitter Yuya Nishihara
Date July 21, 2019, 6:56 a.m.
Message ID <adb7bc2932816deff985.1563692173@mimosa>
Download mbox | patch
Permalink /patch/40987/
State Accepted
Headers show

Comments

Yuya Nishihara - July 21, 2019, 6:56 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1563680817 -32400
#      Sun Jul 21 12:46:57 2019 +0900
# Node ID adb7bc2932816deff985967f42837391c0977b06
# Parent  aa1e9070fdec903532d9e41e944dedca48cac548
rust-filepatterns: fix type of warnings tuple to (bytes, bytes)

Otherwise warn() in match.py would fail if the warning contains non-ASCII
character.

We might want to add a thin ByteString wrapper around Vec<u8> to
implement ToPyObject<ObjectType = PyBytes>, but I'm not sure.

Patch

diff --git a/rust/hg-core/src/filepatterns.rs b/rust/hg-core/src/filepatterns.rs
--- a/rust/hg-core/src/filepatterns.rs
+++ b/rust/hg-core/src/filepatterns.rs
@@ -227,7 +227,7 @@  lazy_static! {
 }
 
 pub type PatternTuple = (Vec<u8>, LineNumber, Vec<u8>);
-type WarningTuple = (String, String);
+type WarningTuple = (Vec<u8>, Vec<u8>);
 
 pub fn parse_pattern_file_contents(
     lines: &[u8],
@@ -263,10 +263,7 @@  pub fn parse_pattern_file_contents(
             if let Some(rel_syntax) = SYNTAXES.get(syntax) {
                 current_syntax = rel_syntax;
             } else if warn {
-                warnings.push((
-                    String::from_utf8_lossy(file_path).to_string(),
-                    String::from_utf8_lossy(syntax).to_string(),
-                ));
+                warnings.push((file_path.to_owned(), syntax.to_owned()));
             }
             continue;
         }
diff --git a/rust/hg-cpython/src/filepatterns.rs b/rust/hg-cpython/src/filepatterns.rs
--- a/rust/hg-cpython/src/filepatterns.rs
+++ b/rust/hg-cpython/src/filepatterns.rs
@@ -40,17 +40,31 @@  fn read_pattern_file_wrapper(
                 };
                 let results: Vec<(PyBytes, LineNumber, PyBytes)> =
                     patterns.iter().map(itemgetter).collect();
-                return Ok((results, warnings).to_py_object(py));
+                return Ok((results, warnings_to_py_bytes(py, &warnings))
+                    .to_py_object(py));
             }
             let itemgetter = |x: &PatternTuple| PyBytes::new(py, &x.0);
             let results: Vec<PyBytes> =
                 patterns.iter().map(itemgetter).collect();
-            Ok((results, warnings).to_py_object(py))
+            Ok(
+                (results, warnings_to_py_bytes(py, &warnings))
+                    .to_py_object(py),
+            )
         }
         Err(e) => Err(PatternFileError::pynew(py, e)),
     }
 }
 
+fn warnings_to_py_bytes(
+    py: Python,
+    warnings: &[(Vec<u8>, Vec<u8>)],
+) -> Vec<(PyBytes, PyBytes)> {
+    warnings
+        .iter()
+        .map(|(path, syn)| (PyBytes::new(py, path), PyBytes::new(py, syn)))
+        .collect()
+}
+
 fn build_single_regex_wrapper(
     py: Python,
     kind: PyObject,