Patchwork D7870: rust-utils: add `PrettyPrint` trait

login
register
mail settings
Submitter phabricator
Date Jan. 14, 2020, 5:35 p.m.
Message ID <differential-rev-PHID-DREV-glypldo7j4g2r4lkqo5i-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/44334/
State Superseded
Headers show

Comments

phabricator - Jan. 14, 2020, 5:35 p.m.
Alphare created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This will be used as a general interface for displaying things to the user.
  The upcoming `IncludeMatcher` will use it to store its patterns in a
  user-displayable string.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7870

AFFECTED FILES
  rust/hg-core/src/utils.rs

CHANGE DETAILS




To: Alphare, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel
phabricator - Jan. 15, 2020, 3:16 p.m.
kevincox added a comment.
kevincox accepted this revision.


  I'm not convinced PrettyPrint is the best name. I might call it something more to do with escaping instead of "pretty". However I can't think of anything great.

INLINE COMMENTS

> utils.rs:146
> +                acc.push(HEX_DIGITS[((*c & 0xf0) >> 4) as usize]);
> +                acc.push(HEX_DIGITS[(*c & 0xf) as usize]);
> +            }

write!(acc, "\\x{:x}", self).unwrap();

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7870/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7870

To: Alphare, #hg-reviewers, kevincox
Cc: durin42, kevincox, mercurial-devel
phabricator - Jan. 15, 2020, 9:56 p.m.
Alphare added a comment.


  In D7870#115935 <https://phab.mercurial-scm.org/D7870#115935>, @kevincox wrote:
  
  > I'm not convinced PrettyPrint is the best name. I might call it something more to do with escaping instead of "pretty". However I can't think of anything great.
  
  Yeah, I am not convinced either. I've sent a new patch, maybe that's better?

INLINE COMMENTS

> kevincox wrote in utils.rs:146
>   write!(acc, "\\x{:x}", self).unwrap();

Oh nice!

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7870/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7870

To: Alphare, #hg-reviewers, kevincox
Cc: durin42, kevincox, mercurial-devel
phabricator - Jan. 16, 2020, 12:44 p.m.
kevincox added a comment.
kevincox accepted this revision.


  I think it's better. Maybe still a little vauge but I'll let subject experts make that decision.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7870/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7870

To: Alphare, #hg-reviewers, kevincox
Cc: durin42, kevincox, mercurial-devel
phabricator - Feb. 6, 2020, 4:43 p.m.
This revision now requires changes to proceed.
martinvonz added a comment.
martinvonz requested changes to this revision.


  I'm also not sure about the name, but I don't have a better proposal. We can revisit that later when it's clearer how it will be used.

INLINE COMMENTS

> utils.rs:154-157
> +        self.iter().fold(vec![], |mut acc, item| {
> +            acc.extend(item.escaped_bytes());
> +            acc
> +        })

i.e. `self.iter().flat_map(|item| item.escaped_bytes()).collect()`?

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7870/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7870

To: Alphare, #hg-reviewers, kevincox, martinvonz
Cc: martinvonz, durin42, kevincox, mercurial-devel
phabricator - Feb. 6, 2020, 4:45 p.m.
Alphare added inline comments.

INLINE COMMENTS

> martinvonz wrote in utils.rs:154-157
> i.e. `self.iter().flat_map(|item| item.escaped_bytes()).collect()`?

Sometimes I forget about `flat_map`. Thanks!

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7870/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7870

To: Alphare, #hg-reviewers, kevincox, martinvonz
Cc: martinvonz, durin42, kevincox, mercurial-devel

Patch

diff --git a/rust/hg-core/src/utils.rs b/rust/hg-core/src/utils.rs
--- a/rust/hg-core/src/utils.rs
+++ b/rust/hg-core/src/utils.rs
@@ -7,6 +7,9 @@ 
 
 //! Contains useful functions, traits, structs, etc. for use in core.
 
+use crate::utils::hg_path::HgPath;
+use std::ops::Deref;
+
 pub mod files;
 pub mod hg_path;
 pub mod path_auditor;
@@ -112,3 +115,61 @@ 
         }
     }
 }
+
+const HEX_DIGITS: &[u8] = b"0123456789abcdef";
+
+pub trait PrettyPrint {
+    fn pretty_print(&self) -> Vec<u8>;
+}
+
+impl PrettyPrint for u8 {
+    fn pretty_print(&self) -> Vec<u8> {
+        let mut acc = vec![];
+        match self {
+            c @ b'\'' | c @ b'\\' => {
+                acc.push(b'\\');
+                acc.push(*c);
+            }
+            b'\t' => {
+                acc.extend(br"\\t");
+            }
+            b'\n' => {
+                acc.extend(br"\\n");
+            }
+            b'\r' => {
+                acc.extend(br"\\r");
+            }
+            c if (*c < b' ' || *c >= 127) => {
+                acc.push(b'\\');
+                acc.push(b'x');
+                acc.push(HEX_DIGITS[((*c & 0xf0) >> 4) as usize]);
+                acc.push(HEX_DIGITS[(*c & 0xf) as usize]);
+            }
+            c => {
+                acc.push(*c);
+            }
+        }
+        acc
+    }
+}
+
+impl<'a, T: PrettyPrint> PrettyPrint for &'a [T] {
+    fn pretty_print(&self) -> Vec<u8> {
+        self.iter().fold(vec![], |mut acc, item| {
+            acc.extend(item.pretty_print());
+            acc
+        })
+    }
+}
+
+impl<T: PrettyPrint> PrettyPrint for Vec<T> {
+    fn pretty_print(&self) -> Vec<u8> {
+        self.deref().pretty_print()
+    }
+}
+
+impl<'a> PrettyPrint for &'a HgPath {
+    fn pretty_print(&self) -> Vec<u8> {
+        self.as_bytes().pretty_print()
+    }
+}