Mercurial > hg
changeset 45984:fada33872b5b
rhg: use `format_bytes!` for error messages
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.
Differential Revision: https://phab.mercurial-scm.org/D9407
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Thu, 26 Nov 2020 09:54:16 +0100 |
parents | 97205cf0ee4d |
children | b6b7626d3e06 |
files | rust/Cargo.lock rust/rhg/Cargo.toml rust/rhg/src/commands/root.rs rust/rhg/src/error.rs rust/rhg/src/ui.rs |
diffstat | 5 files changed, 50 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/Cargo.lock Mon Nov 30 10:18:36 2020 +0100 +++ b/rust/Cargo.lock Thu Nov 26 09:54:16 2020 +0100 @@ -210,6 +210,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" @@ -454,6 +474,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" @@ -653,6 +678,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)", @@ -887,6 +913,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" @@ -914,6 +942,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"
--- a/rust/rhg/Cargo.toml Mon Nov 30 10:18:36 2020 +0100 +++ b/rust/rhg/Cargo.toml Thu Nov 26 09:54:16 2020 +0100 @@ -1,7 +1,10 @@ [package] name = "rhg" version = "0.1.0" -authors = ["Antoine Cezar <antoine.cezar@octobus.net>"] +authors = [ + "Antoine Cezar <antoine.cezar@octobus.net>", + "Raphaël Gomès <raphael.gomes@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"
--- a/rust/rhg/src/commands/root.rs Mon Nov 30 10:18:36 2020 +0100 +++ b/rust/rhg/src/commands/root.rs Thu Nov 26 09:54:16 2020 +0100 @@ -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(()) }
--- a/rust/rhg/src/error.rs Mon Nov 30 10:18:36 2020 +0100 +++ b/rust/rhg/src/error.rs Thu Nov 26 09:54:16 2020 +0100 @@ -1,5 +1,6 @@ use crate::exitcode; use crate::ui::UiError; +use format_bytes::format_bytes; use hg::operations::{FindRootError, FindRootErrorKind}; use hg::requirements::RequirementsError; use hg::utils::files::get_bytes_from_path; @@ -44,27 +45,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::RequirementsError( RequirementsError::Corrupted, ) => Some(
--- a/rust/rhg/src/ui.rs Mon Nov 30 10:18:36 2020 +0100 +++ b/rust/rhg/src/ui.rs Thu Nov 26 09:54:16 2020 +0100 @@ -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)?;