Mercurial > public > mercurial-scm > hg
annotate rust/hg-core/src/config/mod.rs @ 52670:72deeea26bca stable
hg-core: fix usage.resources default logic
This makes hg-core use ResourceProfileValue::Medium as the default. Before, it
used ResourceProfileValue::Default (now removed), which was not supposed to be a
real value, but rather an indirection meaning to use the default (medium).
The motivation for this is that my implementation of rhg annotate was slower on
some files than Python. This was because Python used the "Medium" profile by
default (enabling the revlog chunk cache), while Rust used the "Default" profile
(disabling the revlog chunk cache).
author | Mitchell Kember <mkember@janestreet.com> |
---|---|
date | Tue, 14 Jan 2025 17:15:54 -0500 |
parents | 6499af83735a |
children |
rev | line source |
---|---|
46187
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
1 // config.rs |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
2 // |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
3 // Copyright 2020 |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
4 // Valentin Gatien-Baron, |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
5 // Raphaël Gomès <rgomes@octobus.net> |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
6 // |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
7 // This software may be used and distributed according to the terms of the |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
8 // GNU General Public License version 2 or any later version. |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
9 |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
10 //! Mercurial config parsing and interfaces. |
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
11 |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
12 pub mod config_items; |
46187
95d6f31e88db
hg-core: add basic config module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
13 mod layer; |
49513
467d9df98c68
rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49512
diff
changeset
|
14 mod plain_info; |
46602
a687a7f27951
rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
46598
diff
changeset
|
15 mod values; |
48733
39c447e03dbc
rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents:
47404
diff
changeset
|
16 pub use layer::{ConfigError, ConfigOrigin, ConfigParseError}; |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
17 use lazy_static::lazy_static; |
49513
467d9df98c68
rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49512
diff
changeset
|
18 pub use plain_info::PlainInfo; |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
19 |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
20 use self::config_items::DefaultConfig; |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
21 use self::config_items::DefaultConfigItem; |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
22 use self::layer::ConfigLayer; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
23 use self::layer::ConfigValue; |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
24 use crate::errors::HgError; |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
25 use crate::errors::{HgResultExt, IoResultExt}; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
26 use crate::utils::files::get_bytes_from_os_str; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
27 use format_bytes::{write_bytes, DisplayBytes}; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
28 use std::collections::HashSet; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
29 use std::env; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
30 use std::fmt; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
31 use std::path::{Path, PathBuf}; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
32 use std::str; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
33 |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
34 lazy_static! { |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
35 static ref DEFAULT_CONFIG: Result<DefaultConfig, HgError> = { |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
36 DefaultConfig::from_contents(include_str!( |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
37 "../../../../mercurial/configitems.toml" |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
38 )) |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
39 }; |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
40 } |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
41 |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
42 /// Holds the config values for the current repository |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
43 /// TODO update this docstring once we support more sources |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
44 #[derive(Clone)] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
45 pub struct Config { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
46 layers: Vec<layer::ConfigLayer>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
47 plain: PlainInfo, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
48 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
49 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
50 impl DisplayBytes for Config { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
51 fn display_bytes( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
52 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
53 out: &mut dyn std::io::Write, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
54 ) -> std::io::Result<()> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
55 for (index, layer) in self.layers.iter().rev().enumerate() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
56 write_bytes!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
57 out, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
58 b"==== Layer {} (trusted: {}) ====\n{}", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
59 index, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
60 if layer.trusted { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
61 &b"yes"[..] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
62 } else { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
63 &b"no"[..] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
64 }, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
65 layer |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
66 )?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
67 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
68 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
69 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
70 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
71 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
72 pub enum ConfigSource { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
73 /// Absolute path to a config file |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
74 AbsPath(PathBuf), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
75 /// Already parsed (from the CLI, env, Python resources, etc.) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
76 Parsed(layer::ConfigLayer), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
77 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
78 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
79 #[derive(Debug)] |
50209
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49954
diff
changeset
|
80 pub struct ConfigValueParseErrorDetails { |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
81 pub origin: ConfigOrigin, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
82 pub line: Option<usize>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
83 pub section: Vec<u8>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
84 pub item: Vec<u8>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
85 pub value: Vec<u8>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
86 pub expected_type: &'static str, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
87 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
88 |
50209
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49954
diff
changeset
|
89 // boxed to avoid very large Result types |
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49954
diff
changeset
|
90 pub type ConfigValueParseError = Box<ConfigValueParseErrorDetails>; |
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49954
diff
changeset
|
91 |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
92 impl fmt::Display for ConfigValueParseError { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
94 // TODO: add origin and line number information, here and in |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
95 // corresponding python code |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
96 write!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
97 f, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
98 "config error: {}.{} is not a {} ('{}')", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
99 String::from_utf8_lossy(&self.section), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
100 String::from_utf8_lossy(&self.item), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
101 self.expected_type, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
102 String::from_utf8_lossy(&self.value) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
103 ) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
104 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
105 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
106 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
107 /// Returns true if the config item is disabled by PLAIN or PLAINEXCEPT |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
108 fn should_ignore(plain: &PlainInfo, section: &[u8], item: &[u8]) -> bool { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
109 // duplication with [_applyconfig] in [ui.py], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
110 if !plain.is_plain() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
111 return false; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
112 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
113 if section == b"alias" { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
114 return plain.plainalias(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
115 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
116 if section == b"revsetalias" { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
117 return plain.plainrevsetalias(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
118 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
119 if section == b"templatealias" { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
120 return plain.plaintemplatealias(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
121 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
122 if section == b"ui" { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
123 let to_delete: &[&[u8]] = &[ |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
124 b"debug", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
125 b"fallbackencoding", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
126 b"quiet", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
127 b"slash", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
128 b"logtemplate", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
129 b"message-output", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
130 b"statuscopies", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
131 b"style", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
132 b"traceback", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
133 b"verbose", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
134 ]; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
135 return to_delete.contains(&item); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
136 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
137 let sections_to_delete: &[&[u8]] = |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
138 &[b"defaults", b"commands", b"command-templates"]; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
139 sections_to_delete.contains(§ion) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
140 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
141 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
142 impl Config { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
143 /// The configuration to use when printing configuration-loading errors |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
144 pub fn empty() -> Self { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
145 Self { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
146 layers: Vec::new(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
147 plain: PlainInfo::empty(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
148 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
149 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
150 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
151 /// Load system and user configuration from various files. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
152 /// |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
153 /// This is also affected by some environment variables. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
154 pub fn load_non_repo() -> Result<Self, ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
155 let mut config = Self::empty(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
156 let opt_rc_path = env::var_os("HGRCPATH"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
157 // HGRCPATH replaces system config |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
158 if opt_rc_path.is_none() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
159 config.add_system_config()? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
160 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
161 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
162 config.add_for_environment_variable("EDITOR", b"ui", b"editor"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
163 config.add_for_environment_variable("VISUAL", b"ui", b"editor"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
164 config.add_for_environment_variable("PAGER", b"pager", b"pager"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
165 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
166 // These are set by `run-tests.py --rhg` to enable fallback for the |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
167 // entire test suite. Alternatives would be setting configuration |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
168 // through `$HGRCPATH` but some tests override that, or changing the |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
169 // `hg` shell alias to include `--config` but that disrupts tests that |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
170 // print command lines and check expected output. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
171 config.add_for_environment_variable( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
172 "RHG_ON_UNSUPPORTED", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
173 b"rhg", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
174 b"on-unsupported", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
175 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
176 config.add_for_environment_variable( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
177 "RHG_FALLBACK_EXECUTABLE", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
178 b"rhg", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
179 b"fallback-executable", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
180 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
181 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
182 // HGRCPATH replaces user config |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
183 if opt_rc_path.is_none() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
184 config.add_user_config()? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
185 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
186 if let Some(rc_path) = &opt_rc_path { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
187 for path in env::split_paths(rc_path) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
188 if !path.as_os_str().is_empty() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
189 if path.is_dir() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
190 config.add_trusted_dir(&path)? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
191 } else { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
192 config.add_trusted_file(&path)? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
193 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
194 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
195 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
196 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
197 Ok(config) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
198 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
199 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
200 pub fn load_cli_args( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
201 &mut self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
202 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
203 color_arg: Option<Vec<u8>>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
204 ) -> Result<(), ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
205 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
206 self.layers.push(layer) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
207 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
208 if let Some(arg) = color_arg { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
209 let mut layer = ConfigLayer::new(ConfigOrigin::CommandLineColor); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
210 layer.add(b"ui"[..].into(), b"color"[..].into(), arg, None); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
211 self.layers.push(layer) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
212 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
213 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
214 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
215 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
216 fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
217 if let Some(entries) = std::fs::read_dir(path) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
218 .when_reading_file(path) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
219 .io_not_found_as_none()? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
220 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
221 let mut file_paths = entries |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
222 .map(|result| { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
223 result.when_reading_file(path).map(|entry| entry.path()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
224 }) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
225 .collect::<Result<Vec<_>, _>>()?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
226 file_paths.sort(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
227 for file_path in &file_paths { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
228 if file_path.extension() == Some(std::ffi::OsStr::new("rc")) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
229 self.add_trusted_file(file_path)? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
230 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
231 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
232 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
233 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
234 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
235 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
236 fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
237 if let Some(data) = std::fs::read(path) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
238 .when_reading_file(path) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
239 .io_not_found_as_none()? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
240 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
241 self.layers.extend(ConfigLayer::parse(path, &data)?) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
242 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
243 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
244 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
245 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
246 fn add_for_environment_variable( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
247 &mut self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
248 var: &str, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
249 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
250 key: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
251 ) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
252 if let Some(value) = env::var_os(var) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
253 let origin = layer::ConfigOrigin::Environment(var.into()); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
254 let mut layer = ConfigLayer::new(origin); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
255 layer.add( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
256 section.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
257 key.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
258 get_bytes_from_os_str(value), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
259 None, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
260 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
261 self.layers.push(layer) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
262 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
263 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
264 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
265 #[cfg(unix)] // TODO: other platforms |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
266 fn add_system_config(&mut self) -> Result<(), ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
267 let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
268 let etc = prefix.join("etc").join("mercurial"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
269 self.add_trusted_file(&etc.join("hgrc"))?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
270 self.add_trusted_dir(&etc.join("hgrc.d")) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
271 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
272 let root = Path::new("/"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
273 // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
274 // instead? TODO: can this be a relative path? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
275 let hg = crate::utils::current_exe()?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
276 // TODO: this order (per-installation then per-system) matches |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
277 // `systemrcpath()` in `mercurial/scmposix.py`, but |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
278 // `mercurial/helptext/config.txt` suggests it should be reversed |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
279 if let Some(installation_prefix) = hg.parent().and_then(Path::parent) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
280 if installation_prefix != root { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
281 add_for_prefix(installation_prefix)? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
282 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
283 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
284 add_for_prefix(root)?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
285 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
286 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
287 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
288 #[cfg(unix)] // TODO: other plateforms |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
289 fn add_user_config(&mut self) -> Result<(), ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
290 let opt_home = home::home_dir(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
291 if let Some(home) = &opt_home { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
292 self.add_trusted_file(&home.join(".hgrc"))? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
293 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
294 let darwin = cfg!(any(target_os = "macos", target_os = "ios")); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
295 if !darwin { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
296 if let Some(config_home) = env::var_os("XDG_CONFIG_HOME") |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
297 .map(PathBuf::from) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
298 .or_else(|| opt_home.map(|home| home.join(".config"))) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
299 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
300 self.add_trusted_file(&config_home.join("hg").join("hgrc"))? |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
301 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
302 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
303 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
304 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
305 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
306 /// Loads in order, which means that the precedence is the same |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
307 /// as the order of `sources`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
308 pub fn load_from_explicit_sources( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
309 sources: Vec<ConfigSource>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
310 ) -> Result<Self, ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
311 let mut layers = vec![]; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
312 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
313 for source in sources.into_iter() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
314 match source { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
315 ConfigSource::Parsed(c) => layers.push(c), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
316 ConfigSource::AbsPath(c) => { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
317 // TODO check if it should be trusted |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
318 // mercurial/ui.py:427 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
319 let data = match std::fs::read(&c) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
320 Err(_) => continue, // same as the python code |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
321 Ok(data) => data, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
322 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
323 layers.extend(ConfigLayer::parse(&c, &data)?) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
324 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
325 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
326 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
327 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
328 Ok(Config { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
329 layers, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
330 plain: PlainInfo::empty(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
331 }) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
332 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
333 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
334 /// Loads the per-repository config into a new `Config` which is combined |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
335 /// with `self`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
336 pub(crate) fn combine_with_repo( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
337 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
338 repo_config_files: &[PathBuf], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
339 ) -> Result<Self, ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
340 let (cli_layers, other_layers) = self |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
341 .layers |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
342 .iter() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
343 .cloned() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
344 .partition(ConfigLayer::is_from_command_line); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
345 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
346 let mut repo_config = Self { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
347 layers: other_layers, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
348 plain: PlainInfo::empty(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
349 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
350 for path in repo_config_files { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
351 // TODO: check if this file should be trusted: |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
352 // `mercurial/ui.py:427` |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
353 repo_config.add_trusted_file(path)?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
354 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
355 repo_config.layers.extend(cli_layers); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
356 Ok(repo_config) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
357 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
358 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
359 pub fn apply_plain(&mut self, plain: PlainInfo) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
360 self.plain = plain; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
361 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
362 |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
363 /// Returns the default value for the given config item, if any. |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
364 pub fn get_default( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
365 &self, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
366 section: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
367 item: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
368 ) -> Result<Option<&DefaultConfigItem>, HgError> { |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
369 let default_config = DEFAULT_CONFIG.as_ref().map_err(|e| { |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
370 HgError::abort( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
371 e.to_string(), |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
372 crate::exit_codes::ABORT, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
373 Some("`mercurial/configitems.toml` is not valid".into()), |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
374 ) |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
375 })?; |
50762
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
376 let default_opt = default_config.get(section, item); |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
377 Ok(default_opt.filter(|default| { |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
378 default |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
379 .in_core_extension() |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
380 .map(|extension| { |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
381 // Only return the default for an in-core extension item |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
382 // if said extension is enabled |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
383 self.is_extension_enabled(extension.as_bytes()) |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
384 }) |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
385 .unwrap_or(true) |
7f8f6fe13fa9
configitems: move blackbox's config items to the new configitems.toml
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50761
diff
changeset
|
386 })) |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
387 } |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
388 |
50767
d64df6b35007
rust-config: add docstring to inner `get_parse` method
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50766
diff
changeset
|
389 /// Return the config item that corresponds to a section + item, a function |
d64df6b35007
rust-config: add docstring to inner `get_parse` method
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50766
diff
changeset
|
390 /// to parse from the raw bytes to the expected type (which is passed as |
d64df6b35007
rust-config: add docstring to inner `get_parse` method
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50766
diff
changeset
|
391 /// a string only to make debugging easier). |
d64df6b35007
rust-config: add docstring to inner `get_parse` method
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50766
diff
changeset
|
392 /// Used by higher-level methods like `get_bool`. |
d64df6b35007
rust-config: add docstring to inner `get_parse` method
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50766
diff
changeset
|
393 /// |
d64df6b35007
rust-config: add docstring to inner `get_parse` method
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50766
diff
changeset
|
394 /// `fallback_to_default` controls whether the default value (if any) is |
d64df6b35007
rust-config: add docstring to inner `get_parse` method
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50766
diff
changeset
|
395 /// returned if nothing is found. |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
396 fn get_parse<'config, T: 'config>( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
397 &'config self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
398 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
399 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
400 expected_type: &'static str, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
401 parse: impl Fn(&'config [u8]) -> Option<T>, |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
402 fallback_to_default: bool, |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
403 ) -> Result<Option<T>, HgError> |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
404 where |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
405 Option<T>: TryFrom<&'config DefaultConfigItem, Error = HgError>, |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
406 { |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
407 match self.get_inner(section, item) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
408 Some((layer, v)) => match parse(&v.bytes) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
409 Some(b) => Ok(Some(b)), |
50209
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49954
diff
changeset
|
410 None => Err(Box::new(ConfigValueParseErrorDetails { |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
411 origin: layer.origin.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
412 line: v.line, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
413 value: v.bytes.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
414 section: section.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
415 item: item.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
416 expected_type, |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
417 }) |
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
418 .into()), |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
419 }, |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
420 None => { |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
421 if !fallback_to_default { |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
422 return Ok(None); |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
423 } |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
424 match self.get_default(section, item)? { |
50978
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
425 Some(default) => { |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
426 // Defaults are TOML values, so they're not in the same |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
427 // shape as in the config files. |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
428 // First try to convert directly to the expected type |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
429 let as_t = default.try_into(); |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
430 match as_t { |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
431 Ok(t) => Ok(t), |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
432 Err(e) => { |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
433 // If it fails, it means that... |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
434 let as_bytes: Result<Option<&[u8]>, _> = |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
435 default.try_into(); |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
436 match as_bytes { |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
437 Ok(bytes_opt) => { |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
438 if let Some(bytes) = bytes_opt { |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
439 // ...we should be able to parse it |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
440 return Ok(parse(bytes)); |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
441 } |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
442 Err(e) |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
443 } |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
444 Err(_) => Err(e), |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
445 } |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
446 } |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
447 } |
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
448 } |
50766
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
449 None => { |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
450 self.print_devel_warning(section, item)?; |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
451 Ok(None) |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
452 } |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
453 } |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
454 } |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
455 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
456 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
457 |
50766
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
458 fn print_devel_warning( |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
459 &self, |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
460 section: &[u8], |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
461 item: &[u8], |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
462 ) -> Result<(), HgError> { |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
463 let warn_all = self.get_bool(b"devel", b"all-warnings")?; |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
464 let warn_specific = self.get_bool(b"devel", b"warn-config-unknown")?; |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
465 if !warn_all || !warn_specific { |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
466 // We technically shouldn't print anything here since it's not |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
467 // the concern of `hg-core`. |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
468 // |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
469 // We're printing directly to stderr since development warnings |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
470 // are not on by default and surfacing this to consumer crates |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
471 // (like `rhg`) would be more difficult, probably requiring |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
472 // something à la `log` crate. |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
473 // |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
474 // TODO maybe figure out a way of exposing a "warnings" channel |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
475 // that consumer crates can hook into. It would be useful for |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
476 // all other warnings that `hg-core` could expose. |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
477 eprintln!( |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
478 "devel-warn: accessing unregistered config item: '{}.{}'", |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
479 String::from_utf8_lossy(section), |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
480 String::from_utf8_lossy(item), |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
481 ); |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
482 } |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
483 Ok(()) |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
484 } |
67faf1bd8acd
rust-config: add devel warning when using undeclared config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50762
diff
changeset
|
485 |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
486 /// Returns an `Err` if the first value found is not a valid UTF-8 string. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
487 /// Otherwise, returns an `Ok(value)` if found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
488 pub fn get_str( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
489 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
490 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
491 item: &[u8], |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
492 ) -> Result<Option<&str>, HgError> { |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
493 self.get_parse( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
494 section, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
495 item, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
496 "ASCII or UTF-8 string", |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
497 |value| str::from_utf8(value).ok(), |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
498 true, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
499 ) |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
500 } |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
501 |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
502 /// Same as `get_str`, but doesn't fall back to the default `configitem` |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
503 /// if not defined in the user config. |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
504 pub fn get_str_no_default( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
505 &self, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
506 section: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
507 item: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
508 ) -> Result<Option<&str>, HgError> { |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
509 self.get_parse( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
510 section, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
511 item, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
512 "ASCII or UTF-8 string", |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
513 |value| str::from_utf8(value).ok(), |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
514 false, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
515 ) |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
516 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
517 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
518 /// Returns an `Err` if the first value found is not a valid unsigned |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
519 /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
520 pub fn get_u32( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
521 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
522 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
523 item: &[u8], |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
524 ) -> Result<Option<u32>, HgError> { |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
525 self.get_parse( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
526 section, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
527 item, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
528 "valid integer", |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
529 |value| str::from_utf8(value).ok()?.parse().ok(), |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
530 true, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
531 ) |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
532 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
533 |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
534 /// Returns an `Err` if the first value found is not a valid unsigned |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
535 /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
536 pub fn get_i64( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
537 &self, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
538 section: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
539 item: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
540 ) -> Result<Option<i64>, HgError> { |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
541 self.get_parse( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
542 section, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
543 item, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
544 "valid integer", |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
545 |value| str::from_utf8(value).ok()?.parse().ok(), |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
546 true, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
547 ) |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
548 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
549 |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
550 /// Returns an `Err` if the first value found is not a valid unsigned |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
551 /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
552 pub fn get_u64( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
553 &self, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
554 section: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
555 item: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
556 ) -> Result<Option<u64>, HgError> { |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
557 self.get_parse( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
558 section, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
559 item, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
560 "valid integer", |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
561 |value| str::from_utf8(value).ok()?.parse().ok(), |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
562 true, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
563 ) |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
564 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
565 |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
566 /// Returns an `Err` if the first value found is not a valid float |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
567 /// representation. Otherwise, returns an `Ok(value)` if found, or `None`. |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
568 pub fn get_f64( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
569 &self, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
570 section: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
571 item: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
572 ) -> Result<Option<f64>, HgError> { |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
573 self.get_parse( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
574 section, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
575 item, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
576 "valid float", |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
577 |value| str::from_utf8(value).ok()?.parse().ok(), |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
578 true, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
579 ) |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
580 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
581 |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
582 /// Returns an `Err` if the first value found is not a valid file size |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
583 /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
584 /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
585 pub fn get_byte_size( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
586 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
587 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
588 item: &[u8], |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
589 ) -> Result<Option<u64>, HgError> { |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
590 self.get_parse( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
591 section, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
592 item, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
593 "byte quantity", |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
594 values::parse_byte_size, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
595 true, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
596 ) |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
597 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
598 |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
599 /// Same as [`Self::get_byte_size`], but doesn't fall back to the default |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
600 /// `configitem` if not defined in the user config. |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
601 pub fn get_byte_size_no_default( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
602 &self, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
603 section: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
604 item: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
605 ) -> Result<Option<u64>, HgError> { |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
606 self.get_parse( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
607 section, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
608 item, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
609 "byte quantity", |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
610 values::parse_byte_size, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
611 false, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
612 ) |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
613 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
614 |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
615 /// Returns an `Err` if the first value found is not a valid boolean. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
616 /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
617 /// found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
618 pub fn get_option( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
619 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
620 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
621 item: &[u8], |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
622 ) -> Result<Option<bool>, HgError> { |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
623 self.get_parse(section, item, "boolean", values::parse_bool, true) |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
624 } |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
625 |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
626 /// Same as `get_option`, but doesn't fall back to the default `configitem` |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
627 /// if not defined in the user config. |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
628 pub fn get_option_no_default( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
629 &self, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
630 section: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
631 item: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
632 ) -> Result<Option<bool>, HgError> { |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
633 self.get_parse(section, item, "boolean", values::parse_bool, false) |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
634 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
635 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
636 /// Returns the corresponding boolean in the config. Returns `Ok(false)` |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
637 /// if the value is not found, an `Err` if it's not a valid boolean. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
638 pub fn get_bool( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
639 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
640 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
641 item: &[u8], |
50760
f8412da86d05
rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50209
diff
changeset
|
642 ) -> Result<bool, HgError> { |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
643 Ok(self.get_option(section, item)?.unwrap_or(false)) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
644 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
645 |
50761
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
646 /// Same as `get_bool`, but doesn't fall back to the default `configitem` |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
647 /// if not defined in the user config. |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
648 pub fn get_bool_no_default( |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
649 &self, |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
650 section: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
651 item: &[u8], |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
652 ) -> Result<bool, HgError> { |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
653 Ok(self.get_option_no_default(section, item)?.unwrap_or(false)) |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
654 } |
8ff187fbbfea
rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50760
diff
changeset
|
655 |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
656 /// Returns `true` if the extension is enabled, `false` otherwise |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
657 pub fn is_extension_enabled(&self, extension: &[u8]) -> bool { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
658 let value = self.get(b"extensions", extension); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
659 match value { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
660 Some(c) => !c.starts_with(b"!"), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
661 None => false, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
662 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
663 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
664 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
665 /// If there is an `item` value in `section`, parse and return a list of |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
666 /// byte strings. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
667 pub fn get_list( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
668 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
669 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
670 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
671 ) -> Option<Vec<Vec<u8>>> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
672 self.get(section, item).map(values::parse_list) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
673 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
674 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
675 /// Returns the raw value bytes of the first one found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
676 pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
677 self.get_inner(section, item) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
678 .map(|(_, value)| value.bytes.as_ref()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
679 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
680 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
681 /// Returns the raw value bytes of the first one found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
682 pub fn get_with_origin( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
683 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
684 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
685 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
686 ) -> Option<(&[u8], &ConfigOrigin)> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
687 self.get_inner(section, item) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
688 .map(|(layer, value)| (value.bytes.as_ref(), &layer.origin)) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
689 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
690 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
691 /// Returns the layer and the value of the first one found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
692 fn get_inner( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
693 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
694 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
695 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
696 ) -> Option<(&ConfigLayer, &ConfigValue)> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
697 // Filter out the config items that are hidden by [PLAIN]. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
698 // This differs from python hg where we delete them from the config. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
699 let should_ignore = should_ignore(&self.plain, section, item); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
700 for layer in self.layers.iter().rev() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
701 if !layer.trusted { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
702 continue; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
703 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
704 //The [PLAIN] config should not affect the defaults. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
705 // |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
706 // However, PLAIN should also affect the "tweaked" defaults (unless |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
707 // "tweakdefault" is part of "HGPLAINEXCEPT"). |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
708 // |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
709 // In practice the tweak-default layer is only added when it is |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
710 // relevant, so we can safely always take it into |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
711 // account here. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
712 if should_ignore && !(layer.origin == ConfigOrigin::Tweakdefaults) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
713 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
714 continue; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
715 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
716 if let Some(v) = layer.get(section, item) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
717 return Some((layer, v)); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
718 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
719 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
720 None |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
721 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
722 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
723 /// Return all keys defined for the given section |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
724 pub fn get_section_keys(&self, section: &[u8]) -> HashSet<&[u8]> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
725 self.layers |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
726 .iter() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
727 .flat_map(|layer| layer.iter_keys(section)) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
728 .collect() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
729 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
730 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
731 /// Returns whether any key is defined in the given section |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
732 pub fn has_non_empty_section(&self, section: &[u8]) -> bool { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
733 self.layers |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
734 .iter() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
735 .any(|layer| layer.has_non_empty_section(section)) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
736 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
737 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
738 /// Yields (key, value) pairs for everything in the given section |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
739 pub fn iter_section<'a>( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
740 &'a self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
741 section: &'a [u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
742 ) -> impl Iterator<Item = (&[u8], &[u8])> + 'a { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
743 // Deduplicate keys redefined in multiple layers |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
744 let mut keys_already_seen = HashSet::new(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
745 let mut key_is_new = |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
746 move |&(key, _value): &(&'a [u8], &'a [u8])| -> bool { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
747 keys_already_seen.insert(key) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
748 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
749 // This is similar to `flat_map` + `filter_map`, except with a single |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
750 // closure that owns `key_is_new` (and therefore the |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
751 // `keys_already_seen` set): |
49954
a11237723332
rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49936
diff
changeset
|
752 let mut layer_iters = self |
a11237723332
rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49936
diff
changeset
|
753 .layers |
a11237723332
rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49936
diff
changeset
|
754 .iter() |
a11237723332
rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49936
diff
changeset
|
755 .rev() |
a11237723332
rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49936
diff
changeset
|
756 .map(move |layer| layer.iter_section(section)) |
a11237723332
rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49936
diff
changeset
|
757 .peekable(); |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
758 std::iter::from_fn(move || loop { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
759 if let Some(pair) = layer_iters.peek_mut()?.find(&mut key_is_new) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
760 return Some(pair); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
761 } else { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
762 layer_iters.next(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
763 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
764 }) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
765 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
766 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
767 /// Get raw values bytes from all layers (even untrusted ones) in order |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
768 /// of precedence. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
769 #[cfg(test)] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
770 fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
771 let mut res = vec![]; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
772 for layer in self.layers.iter().rev() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
773 if let Some(v) = layer.get(section, item) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
774 res.push(v.bytes.as_ref()); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
775 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
776 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
777 res |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
778 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
779 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
780 // a config layer that's introduced by ui.tweakdefaults |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
781 fn tweakdefaults_layer() -> ConfigLayer { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
782 let mut layer = ConfigLayer::new(ConfigOrigin::Tweakdefaults); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
783 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
784 let mut add = |section: &[u8], item: &[u8], value: &[u8]| { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
785 layer.add( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
786 section[..].into(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
787 item[..].into(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
788 value[..].into(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
789 None, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
790 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
791 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
792 // duplication of [tweakrc] from [ui.py] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
793 add(b"ui", b"rollback", b"False"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
794 add(b"ui", b"statuscopies", b"yes"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
795 add(b"ui", b"interface", b"curses"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
796 add(b"ui", b"relative-paths", b"yes"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
797 add(b"commands", b"grep.all-files", b"True"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
798 add(b"commands", b"update.check", b"noconflict"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
799 add(b"commands", b"status.verbose", b"True"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
800 add(b"commands", b"resolve.explicit-re-merge", b"True"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
801 add(b"git", b"git", b"1"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
802 add(b"git", b"showfunc", b"1"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
803 add(b"git", b"word-diff", b"1"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
804 layer |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
805 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
806 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
807 // introduce the tweaked defaults as implied by ui.tweakdefaults |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
808 pub fn tweakdefaults(&mut self) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
809 self.layers.insert(0, Config::tweakdefaults_layer()); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
810 } |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
811 |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
812 /// Return the resource profile for a dimension (memory, cpu or disk). |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
813 /// |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
814 /// If no dimension is specified, the generic value is returned. |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
815 pub fn get_resource_profile( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
816 &self, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
817 dimension: Option<&str>, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
818 ) -> ResourceProfile { |
52670
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
819 let value = self.resource_profile_from_item(b"usage", b"resources"); |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
820 let sub_value = dimension.and_then(|dimension| { |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
821 self.resource_profile_from_item( |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
822 b"usage", |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
823 format!("resources.{}", dimension).as_bytes(), |
52670
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
824 ) |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
825 }); |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
826 ResourceProfile { |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
827 dimension: dimension.map(ToOwned::to_owned), |
52670
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
828 value: sub_value.or(value).unwrap_or_default(), |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
829 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
830 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
831 |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
832 fn resource_profile_from_item( |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
833 &self, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
834 section: &[u8], |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
835 item: &[u8], |
52670
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
836 ) -> Option<ResourceProfileValue> { |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
837 match self.get(section, item)? { |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
838 b"low" => Some(ResourceProfileValue::Low), |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
839 b"medium" => Some(ResourceProfileValue::Medium), |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
840 b"high" => Some(ResourceProfileValue::High), |
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
841 _ => None, |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
842 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
843 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
844 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
845 |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
846 /// Corresponds to `usage.resources[.<dimension>]`. |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
847 /// |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
848 /// See `hg help config.usage.resources`. |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
849 #[derive(Debug, Eq, PartialEq, PartialOrd, Ord)] |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
850 pub struct ResourceProfile { |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
851 pub dimension: Option<String>, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
852 pub value: ResourceProfileValue, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
853 } |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
854 |
52670
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
855 #[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Default)] |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
856 pub enum ResourceProfileValue { |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
857 Low, |
52670
72deeea26bca
hg-core: fix usage.resources default logic
Mitchell Kember <mkember@janestreet.com>
parents:
52669
diff
changeset
|
858 #[default] |
51847
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
859 Medium, |
0dbf6a5ccf5f
rust-config: add more ways of reading the config
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51117
diff
changeset
|
860 High, |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
861 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
862 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
863 #[cfg(test)] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
864 mod tests { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
865 use super::*; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
866 use pretty_assertions::assert_eq; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
867 use std::fs::File; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
868 use std::io::Write; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
869 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
870 #[test] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
871 fn test_include_layer_ordering() { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
872 let tmpdir = tempfile::tempdir().unwrap(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
873 let tmpdir_path = tmpdir.path(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
874 let mut included_file = |
51117
532e74ad3ff6
rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50980
diff
changeset
|
875 File::create(tmpdir_path.join("included.rc")).unwrap(); |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
876 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
877 included_file.write_all(b"[section]\nitem=value1").unwrap(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
878 let base_config_path = tmpdir_path.join("base.rc"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
879 let mut config_file = File::create(&base_config_path).unwrap(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
880 let data = |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
881 b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\ |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
882 [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub"; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
883 config_file.write_all(data).unwrap(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
884 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
885 let sources = vec![ConfigSource::AbsPath(base_config_path)]; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
886 let config = Config::load_from_explicit_sources(sources) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
887 .expect("expected valid config"); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
888 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
889 let (_, value) = config.get_inner(b"section", b"item").unwrap(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
890 assert_eq!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
891 value, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
892 &ConfigValue { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
893 bytes: b"value2".to_vec(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
894 line: Some(4) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
895 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
896 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
897 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
898 let value = config.get(b"section", b"item").unwrap(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
899 assert_eq!(value, b"value2",); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
900 assert_eq!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
901 config.get_all(b"section", b"item"), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
902 [b"value2", b"value1", b"value0"] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
903 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
904 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
905 assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4)); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
906 assert_eq!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
907 config.get_byte_size(b"section2", b"size").unwrap(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
908 Some(1024 + 512) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
909 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
910 assert!(config.get_u32(b"section2", b"not-count").is_err()); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
911 assert!(config.get_byte_size(b"section2", b"not-size").is_err()); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
912 } |
50977
d7b2701f17fa
rust-config: demonstrate a bug when falling back to non-trivial default values
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50767
diff
changeset
|
913 |
d7b2701f17fa
rust-config: demonstrate a bug when falling back to non-trivial default values
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50767
diff
changeset
|
914 #[test] |
d7b2701f17fa
rust-config: demonstrate a bug when falling back to non-trivial default values
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50767
diff
changeset
|
915 fn test_default_parse() { |
d7b2701f17fa
rust-config: demonstrate a bug when falling back to non-trivial default values
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50767
diff
changeset
|
916 let config = Config::load_from_explicit_sources(vec![]) |
d7b2701f17fa
rust-config: demonstrate a bug when falling back to non-trivial default values
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50767
diff
changeset
|
917 .expect("expected valid config"); |
d7b2701f17fa
rust-config: demonstrate a bug when falling back to non-trivial default values
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50767
diff
changeset
|
918 let ret = config.get_byte_size(b"cmdserver", b"max-log-size"); |
50978
58390f59826f
rust-config: fix fallback to default not parsing the default value
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50977
diff
changeset
|
919 assert!(ret.is_ok(), "{:?}", ret); |
50979
10e57e3f7276
rust-config: show default `null` is coerced incorrectly to `false`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50978
diff
changeset
|
920 |
10e57e3f7276
rust-config: show default `null` is coerced incorrectly to `false`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50978
diff
changeset
|
921 let ret = config.get_byte_size(b"ui", b"formatted"); |
50980
8343947af6a7
rust-config: fix incorrect coercion of null values to false
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50979
diff
changeset
|
922 assert!(ret.unwrap().is_none()); |
50977
d7b2701f17fa
rust-config: demonstrate a bug when falling back to non-trivial default values
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50767
diff
changeset
|
923 } |
49936
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49513
diff
changeset
|
924 } |