##// END OF EJS Templates
rust: Use the DisplayBytes trait in config printing...
Simon Sapin -
r47249:eace48b4 default
parent child Browse files
Show More
@@ -244,16 +244,16 b' dependencies = ['
244
244
245 [[package]]
245 [[package]]
246 name = "format-bytes"
246 name = "format-bytes"
247 version = "0.1.3"
247 version = "0.2.0"
248 source = "registry+https://github.com/rust-lang/crates.io-index"
248 source = "registry+https://github.com/rust-lang/crates.io-index"
249 dependencies = [
249 dependencies = [
250 "format-bytes-macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
250 "format-bytes-macros 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
251 "proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)",
251 "proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)",
252 ]
252 ]
253
253
254 [[package]]
254 [[package]]
255 name = "format-bytes-macros"
255 name = "format-bytes-macros"
256 version = "0.1.2"
256 version = "0.3.0"
257 source = "registry+https://github.com/rust-lang/crates.io-index"
257 source = "registry+https://github.com/rust-lang/crates.io-index"
258 dependencies = [
258 dependencies = [
259 "proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)",
259 "proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -305,7 +305,7 b' dependencies = ['
305 "crossbeam-channel 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
305 "crossbeam-channel 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
306 "derive_more 0.99.11 (registry+https://github.com/rust-lang/crates.io-index)",
306 "derive_more 0.99.11 (registry+https://github.com/rust-lang/crates.io-index)",
307 "flate2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
307 "flate2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
308 "format-bytes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
308 "format-bytes 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
309 "home 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
309 "home 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
310 "im-rc 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
310 "im-rc 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
311 "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
311 "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -718,7 +718,7 b' dependencies = ['
718 "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
718 "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
719 "derive_more 0.99.11 (registry+https://github.com/rust-lang/crates.io-index)",
719 "derive_more 0.99.11 (registry+https://github.com/rust-lang/crates.io-index)",
720 "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
720 "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
721 "format-bytes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
721 "format-bytes 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
722 "hg-core 0.1.0",
722 "hg-core 0.1.0",
723 "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
723 "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
724 "micro-timer 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
724 "micro-timer 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -965,8 +965,8 b' dependencies = ['
965 "checksum either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
965 "checksum either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
966 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
966 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
967 "checksum flate2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "7411863d55df97a419aa64cb4d2f167103ea9d767e2c54a1868b7ac3f6b47129"
967 "checksum flate2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "7411863d55df97a419aa64cb4d2f167103ea9d767e2c54a1868b7ac3f6b47129"
968 "checksum format-bytes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1a7374eb574cd29ae45878554298091c554c3286a17b3afa440a3e2710ae0790"
968 "checksum format-bytes 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc35f5e45d6b31053cea13078ffc6fa52fa8617aa54b7ac2011720d9c009e04f"
969 "checksum format-bytes-macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4edcc04201cea17a0e6b937adebd46b93fba09924c7e6ed8c515a35ce8432cbc"
969 "checksum format-bytes-macros 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b05089e341a0460449e2210c3bf7b61597860b07f0deae58da38dbed0a4c6b6d"
970 "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
970 "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
971 "checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
971 "checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
972 "checksum getrandom 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
972 "checksum getrandom 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
@@ -29,7 +29,7 b' log = "0.4.8"'
29 memmap = "0.7.0"
29 memmap = "0.7.0"
30 zstd = "0.5.3"
30 zstd = "0.5.3"
31 rust-crypto = "0.2.36"
31 rust-crypto = "0.2.36"
32 format-bytes = "0.1.2"
32 format-bytes = "0.2.0"
33
33
34 # We don't use the `miniz-oxide` backend to not change rhg benchmarks and until
34 # We don't use the `miniz-oxide` backend to not change rhg benchmarks and until
35 # we have a clearer view of which backend is the fastest.
35 # we have a clearer view of which backend is the fastest.
@@ -12,6 +12,7 b' use crate::config::layer::{'
12 ConfigError, ConfigLayer, ConfigParseError, ConfigValue,
12 ConfigError, ConfigLayer, ConfigParseError, ConfigValue,
13 };
13 };
14 use crate::utils::files::get_bytes_from_path;
14 use crate::utils::files::get_bytes_from_path;
15 use format_bytes::{write_bytes, DisplayBytes};
15 use std::env;
16 use std::env;
16 use std::path::{Path, PathBuf};
17 use std::path::{Path, PathBuf};
17
18
@@ -23,13 +24,22 b' pub struct Config {'
23 layers: Vec<layer::ConfigLayer>,
24 layers: Vec<layer::ConfigLayer>,
24 }
25 }
25
26
26 impl std::fmt::Debug for Config {
27 impl DisplayBytes for Config {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 fn display_bytes(
29 &self,
30 out: &mut dyn std::io::Write,
31 ) -> std::io::Result<()> {
28 for (index, layer) in self.layers.iter().rev().enumerate() {
32 for (index, layer) in self.layers.iter().rev().enumerate() {
29 write!(
33 write_bytes!(
30 f,
34 out,
31 "==== Layer {} (trusted: {}) ====\n{:?}",
35 b"==== Layer {} (trusted: {}) ====\n{}",
32 index, layer.trusted, layer
36 index,
37 if layer.trusted {
38 &b"yes"[..]
39 } else {
40 &b"no"[..]
41 },
42 layer
33 )?;
43 )?;
34 }
44 }
35 Ok(())
45 Ok(())
@@ -9,7 +9,7 b''
9
9
10 use crate::errors::{HgError, IoResultExt};
10 use crate::errors::{HgError, IoResultExt};
11 use crate::utils::files::{get_bytes_from_path, get_path_from_bytes};
11 use crate::utils::files::{get_bytes_from_path, get_path_from_bytes};
12 use format_bytes::format_bytes;
12 use format_bytes::{write_bytes, DisplayBytes};
13 use lazy_static::lazy_static;
13 use lazy_static::lazy_static;
14 use regex::bytes::Regex;
14 use regex::bytes::Regex;
15 use std::collections::HashMap;
15 use std::collections::HashMap;
@@ -165,8 +165,11 b' impl ConfigLayer {'
165 }
165 }
166 }
166 }
167
167
168 impl std::fmt::Debug for ConfigLayer {
168 impl DisplayBytes for ConfigLayer {
169 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169 fn display_bytes(
170 &self,
171 out: &mut dyn std::io::Write,
172 ) -> std::io::Result<()> {
170 let mut sections: Vec<_> = self.sections.iter().collect();
173 let mut sections: Vec<_> = self.sections.iter().collect();
171 sections.sort_by(|e0, e1| e0.0.cmp(e1.0));
174 sections.sort_by(|e0, e1| e0.0.cmp(e1.0));
172
175
@@ -175,16 +178,13 b' impl std::fmt::Debug for ConfigLayer {'
175 items.sort_by(|e0, e1| e0.0.cmp(e1.0));
178 items.sort_by(|e0, e1| e0.0.cmp(e1.0));
176
179
177 for (item, config_entry) in items {
180 for (item, config_entry) in items {
178 writeln!(
181 write_bytes!(
179 f,
182 out,
180 "{}",
183 b"{}.{}={} # {}\n",
181 String::from_utf8_lossy(&format_bytes!(
184 section,
182 b"{}.{}={} # {}",
185 item,
183 section,
186 &config_entry.bytes,
184 item,
187 &self.origin,
185 &config_entry.bytes,
186 &self.origin.to_bytes(),
187 ))
188 )?
188 )?
189 }
189 }
190 }
190 }
@@ -224,13 +224,15 b' pub enum ConfigOrigin {'
224 * Others? */
224 * Others? */
225 }
225 }
226
226
227 impl ConfigOrigin {
227 impl DisplayBytes for ConfigOrigin {
228 /// TODO use some kind of dedicated trait?
228 fn display_bytes(
229 pub fn to_bytes(&self) -> Vec<u8> {
229 &self,
230 out: &mut dyn std::io::Write,
231 ) -> std::io::Result<()> {
230 match self {
232 match self {
231 ConfigOrigin::File(p) => get_bytes_from_path(p),
233 ConfigOrigin::File(p) => out.write_all(&get_bytes_from_path(p)),
232 ConfigOrigin::CommandLine => b"--config".to_vec(),
234 ConfigOrigin::CommandLine => out.write_all(b"--config"),
233 ConfigOrigin::Environment(e) => format_bytes!(b"${}", e),
235 ConfigOrigin::Environment(e) => write_bytes!(out, b"${}", e),
234 }
236 }
235 }
237 }
236 }
238 }
@@ -14,4 +14,4 b' derive_more = "0.99"'
14 log = "0.4.11"
14 log = "0.4.11"
15 micro-timer = "0.3.1"
15 micro-timer = "0.3.1"
16 env_logger = "0.7.1"
16 env_logger = "0.7.1"
17 format-bytes = "0.1.3"
17 format-bytes = "0.2.0"
@@ -90,7 +90,7 b' impl From<ConfigParseError> for CommandE'
90 CommandError::Abort {
90 CommandError::Abort {
91 message: format_bytes!(
91 message: format_bytes!(
92 b"config parse error in {}{}: '{}'",
92 b"config parse error in {}{}: '{}'",
93 origin.to_bytes(),
93 origin,
94 line_message,
94 line_message,
95 bytes
95 bytes
96 ),
96 ),
General Comments 0
You need to be logged in to leave comments. Login now