D9407: rhg: use `format_bytes!` for error messages

Alphare (Raphaël Gomès) phabricator at mercurial-scm.org
Thu Nov 26 10:48:43 UTC 2020


Alphare created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This change also includes a formatting changing with the new `rustfmt` version,
  but I'm expecting it to have already been applied in another patch by the time
  this lands.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/Cargo.lock
  rust/hg-core/src/dirstate/dirstate_tree/node.rs
  rust/rhg/Cargo.toml
  rust/rhg/src/commands/root.rs
  rust/rhg/src/error.rs
  rust/rhg/src/ui.rs

CHANGE DETAILS

diff --git a/rust/rhg/src/ui.rs b/rust/rhg/src/ui.rs
--- a/rust/rhg/src/ui.rs
+++ b/rust/rhg/src/ui.rs
@@ -1,3 +1,4 @@
+use format_bytes::format_bytes;
 use std::borrow::Cow;
 use std::io;
 use std::io::{ErrorKind, Write};
@@ -87,7 +88,10 @@
     let mut stderr = io::stderr();
 
     stderr
-        .write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat())
+        .write_all(&format_bytes!(
+            b"abort: {}\n",
+            error.to_string().as_bytes()
+        ))
         .map_err(UiError::StderrError)?;
 
     stderr.flush().map_err(UiError::StderrError)?;
diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs
--- a/rust/rhg/src/error.rs
+++ b/rust/rhg/src/error.rs
@@ -1,5 +1,6 @@
 use crate::exitcode;
 use crate::ui::UiError;
+use format_bytes::format_bytes;
 use hg::operations::{FindRootError, FindRootErrorKind};
 use hg::utils::files::get_bytes_from_path;
 use std::convert::From;
@@ -37,27 +38,17 @@
     /// Return the message corresponding to the error kind if any
     pub fn get_error_message_bytes(&self) -> Option<Vec<u8>> {
         match self {
-            // TODO use formating macro
             CommandErrorKind::RootNotFound(path) => {
                 let bytes = get_bytes_from_path(path);
-                Some(
-                    [
-                        b"abort: no repository found in '",
-                        bytes.as_slice(),
-                        b"' (.hg not found)!\n",
-                    ]
-                    .concat(),
-                )
+                Some(format_bytes!(
+                    b"abort: no repository found in '{}' (.hg not found)!\n",
+                    bytes.as_slice()
+                ))
             }
-            // TODO use formating macro
-            CommandErrorKind::CurrentDirNotFound(e) => Some(
-                [
-                    b"abort: error getting current working directory: ",
-                    e.to_string().as_bytes(),
-                    b"\n",
-                ]
-                .concat(),
-            ),
+            CommandErrorKind::CurrentDirNotFound(e) => Some(format_bytes!(
+                b"abort: error getting current working directory: {}\n",
+                e.to_string().as_bytes(),
+            )),
             CommandErrorKind::Abort(message) => message.to_owned(),
             _ => None,
         }
diff --git a/rust/rhg/src/commands/root.rs b/rust/rhg/src/commands/root.rs
--- a/rust/rhg/src/commands/root.rs
+++ b/rust/rhg/src/commands/root.rs
@@ -1,6 +1,7 @@
 use crate::commands::Command;
 use crate::error::CommandError;
 use crate::ui::Ui;
+use format_bytes::format_bytes;
 use hg::operations::FindRoot;
 use hg::utils::files::get_bytes_from_path;
 
@@ -24,8 +25,7 @@
 
         let bytes = get_bytes_from_path(path_buf);
 
-        // TODO use formating macro
-        ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?;
+        ui.write_stdout(&format_bytes!(b"{}\n", bytes.as_slice()))?;
 
         Ok(())
     }
diff --git a/rust/rhg/Cargo.toml b/rust/rhg/Cargo.toml
--- a/rust/rhg/Cargo.toml
+++ b/rust/rhg/Cargo.toml
@@ -1,7 +1,10 @@
 [package]
 name = "rhg"
 version = "0.1.0"
-authors = ["Antoine Cezar <antoine.cezar at octobus.net>"]
+authors = [
+    "Antoine Cezar <antoine.cezar at octobus.net>",
+    "Raphaël Gomès <raphael.gomes at octobus.net>",
+]
 edition = "2018"
 
 [dependencies]
@@ -10,3 +13,4 @@
 log = "0.4.11"
 micro-timer = "0.3.1"
 env_logger = "0.7.1"
+format-bytes = "0.1.3"
diff --git a/rust/hg-core/src/dirstate/dirstate_tree/node.rs b/rust/hg-core/src/dirstate/dirstate_tree/node.rs
--- a/rust/hg-core/src/dirstate/dirstate_tree/node.rs
+++ b/rust/hg-core/src/dirstate/dirstate_tree/node.rs
@@ -93,11 +93,7 @@
                         EntryState::Removed => {
                             unreachable!("Removed file turning into a directory was dealt with earlier")
                         }
-                        _ => {
-                            Node::insert_in_file(
-                                file, new_entry, head, tail,
-                            )
-                        }
+                        _ => Node::insert_in_file(file, new_entry, head, tail),
                     }
                 }
             }
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -202,6 +202,26 @@
 ]
 
 [[package]]
+name = "format-bytes"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "format-bytes-macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "format-bytes-macros"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)",
+ "proc-macro2 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "fuchsia-cprng"
 version = "0.1.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -432,6 +452,11 @@
 ]
 
 [[package]]
+name = "proc-macro-hack"
+version = "0.5.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "proc-macro2"
 version = "1.0.21"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -623,6 +648,7 @@
 dependencies = [
  "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "format-bytes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "hg-core 0.1.0",
  "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "micro-timer 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -837,6 +863,8 @@
 "checksum either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
 "checksum flate2 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "766d0e77a2c1502169d4a93ff3b8c15a71fd946cd0126309752104e5f3c46d94"
+"checksum format-bytes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1a7374eb574cd29ae45878554298091c554c3286a17b3afa440a3e2710ae0790"
+"checksum format-bytes-macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4edcc04201cea17a0e6b937adebd46b93fba09924c7e6ed8c515a35ce8432cbc"
 "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
 "checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
 "checksum getrandom 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
@@ -863,6 +891,7 @@
 "checksum pkg-config 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
 "checksum ppv-lite86 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
 "checksum pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f81e1644e1b54f5a68959a29aa86cde704219254669da328ecfdf6a1f09d427"
+"checksum proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)" = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
 "checksum proc-macro2 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c"
 "checksum python27-sys 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "67cb041de8615111bf224dd75667af5f25c6e032118251426fed7f1b70ce4c8c"
 "checksum python3-sys 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90af11779515a1e530af60782d273b59ac79d33b0e253c071a728563957c76d4"



To: Alphare, #hg-reviewers
Cc: mercurial-patches, mercurial-devel


More information about the Mercurial-devel mailing list