annotate rust/hg-core/src/config/mod.rs @ 50803:8ff187fbbfea

rust-config: add config getters that don't fall back to defaults This is useful in cases where we access config items that are more... lenient with their types than a fresh new system would allow. For now there is only a single use of this, but we might get more later.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 13 Feb 2023 18:11:48 +0100
parents f8412da86d05
children 7f8f6fe13fa9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
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;
49518
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49517
diff changeset
14 mod plain_info;
46641
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents: 46637
diff changeset
15 mod values;
48743
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 47410
diff changeset
16 pub use layer::{ConfigError, ConfigOrigin, ConfigParseError};
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
17 use lazy_static::lazy_static;
49518
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49517
diff changeset
18 pub use plain_info::PlainInfo;
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
19
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
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: 50248
diff changeset
21 use self::config_items::DefaultConfigItem;
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
23 use self::layer::ConfigValue;
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
24 use crate::errors::HgError;
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
29 use std::env;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
30 use std::fmt;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
32 use std::str;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
33
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
34 lazy_static! {
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
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: 50248
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: 50248
diff changeset
37 "../../../../mercurial/configitems.toml"
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
38 ))
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
39 };
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
40 }
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
41
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
diff changeset
44 #[derive(Clone)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
45 pub struct Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
47 plain: PlainInfo,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
48 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
49
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
51 fn display_bytes(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
52 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
54 ) -> std::io::Result<()> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
56 write_bytes!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
57 out,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
59 index,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
60 if layer.trusted {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
61 &b"yes"[..]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
62 } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
63 &b"no"[..]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
64 },
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
65 layer
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
66 )?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
67 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
68 Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
69 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
70 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
71
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
72 pub enum ConfigSource {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
74 AbsPath(PathBuf),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
76 Parsed(layer::ConfigLayer),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
77 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
78
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
79 #[derive(Debug)]
50248
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50017
diff changeset
80 pub struct ConfigValueParseErrorDetails {
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
81 pub origin: ConfigOrigin,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
87 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
88
50248
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50017
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: 50017
diff changeset
90 pub type ConfigValueParseError = Box<ConfigValueParseErrorDetails>;
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50017
diff changeset
91
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
diff changeset
95 // corresponding python code
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
96 write!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
97 f,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
diff changeset
101 self.expected_type,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
103 )
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
104 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
105 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
106
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
111 return false;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
112 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
114 return plain.plainalias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
115 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
117 return plain.plainrevsetalias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
118 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
120 return plain.plaintemplatealias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
121 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
diff changeset
124 b"debug",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
125 b"fallbackencoding",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
126 b"quiet",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
127 b"slash",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
128 b"logtemplate",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
129 b"message-output",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
130 b"statuscopies",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
131 b"style",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
132 b"traceback",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
133 b"verbose",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
134 ];
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
136 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
diff changeset
139 sections_to_delete.contains(&section)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
140 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
141
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
142 impl Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
diff changeset
145 Self {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
146 layers: Vec::new(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
147 plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
148 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
149 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
150
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
152 ///
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
160 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
161
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
diff changeset
165
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
172 "RHG_ON_UNSUPPORTED",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
173 b"rhg",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
174 b"on-unsupported",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
175 );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
177 "RHG_FALLBACK_EXECUTABLE",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
178 b"rhg",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
179 b"fallback-executable",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
180 );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
181
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
diff changeset
185 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
191 } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
193 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
194 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
195 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
196 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
197 Ok(config)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
198 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
199
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
201 &mut self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
diff changeset
204 ) -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
diff changeset
207 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
212 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
213 Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
214 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
215
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
220 {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
222 .map(|result| {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
224 })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
225 .collect::<Result<Vec<_>, _>>()?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
226 file_paths.sort();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
diff changeset
230 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
231 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
232 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
233 Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
234 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
235
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
240 {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
242 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
243 Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
244 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
245
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
247 &mut self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
248 var: &str,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
249 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
250 key: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
251 ) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
diff changeset
255 layer.add(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
256 section.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
257 key.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
259 None,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
260 );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
262 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
263 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
264
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
271 };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
282 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
283 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
285 Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
286 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
287
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
293 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
295 if !darwin {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
297 .map(PathBuf::from)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
299 {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
301 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
302 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
303 Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
304 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
305
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
diff changeset
309 sources: Vec<ConfigSource>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
310 ) -> Result<Self, ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
312
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
314 match source {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
316 ConfigSource::AbsPath(c) => {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
321 Ok(data) => data,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
322 };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
324 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
325 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
326 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
327
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
328 Ok(Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
329 layers,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
330 plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
331 })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
332 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
333
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
335 /// with `self`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
337 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
339 ) -> Result<Self, ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
341 .layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
342 .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
343 .cloned()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
345
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
347 layers: other_layers,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
348 plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
349 };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
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: 49518
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: 49518
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: 49518
diff changeset
354 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
356 Ok(repo_config)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
357 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
358
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
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: 49518
diff changeset
360 self.plain = plain;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
361 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
362
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
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: 50802
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: 50802
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: 50802
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: 50802
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: 50802
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: 50802
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: 50802
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: 50802
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: 50802
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: 50802
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: 50802
diff changeset
374 )
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
375 })?;
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
376 Ok(default_config.get(section, item))
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
377 }
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
378
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
379 fn get_parse<'config, T: 'config>(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
380 &'config self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
381 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
382 item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
383 expected_type: &'static str,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
384 parse: impl Fn(&'config [u8]) -> Option<T>,
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
385 fallback_to_default: bool,
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
386 ) -> Result<Option<T>, HgError>
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
387 where
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
388 Option<T>: TryFrom<&'config DefaultConfigItem, Error = HgError>,
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
389 {
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
390 match self.get_inner(section, item) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
391 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: 49518
diff changeset
392 Some(b) => Ok(Some(b)),
50248
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50017
diff changeset
393 None => Err(Box::new(ConfigValueParseErrorDetails {
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
394 origin: layer.origin.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
395 line: v.line,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
396 value: v.bytes.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
397 section: section.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
398 item: item.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
399 expected_type,
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
400 })
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
401 .into()),
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
402 },
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
403 None => {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
404 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: 50802
diff changeset
405 return Ok(None);
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
406 }
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
407 match self.get_default(section, item)? {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
408 Some(default) => Ok(default.try_into()?),
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
409 None => Ok(None),
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
410 }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
411 }
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
412 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
413 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
414
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
415 /// 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: 49518
diff changeset
416 /// 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: 49518
diff changeset
417 pub fn get_str(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
418 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
419 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
420 item: &[u8],
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
421 ) -> Result<Option<&str>, HgError> {
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
422 self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
423 section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
424 item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
425 "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: 50802
diff changeset
426 |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: 50802
diff changeset
427 true,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
428 )
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
429 }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
430
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
431 /// 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: 50802
diff changeset
432 /// 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: 50802
diff changeset
433 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: 50802
diff changeset
434 &self,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
435 section: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
436 item: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
437 ) -> 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: 50802
diff changeset
438 self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
439 section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
440 item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
441 "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: 50802
diff changeset
442 |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: 50802
diff changeset
443 false,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
444 )
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
445 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
446
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
447 /// 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: 49518
diff changeset
448 /// 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: 49518
diff changeset
449 pub fn get_u32(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
450 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
451 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
452 item: &[u8],
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
453 ) -> Result<Option<u32>, HgError> {
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
454 self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
455 section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
456 item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
457 "valid integer",
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
458 |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: 50802
diff changeset
459 true,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
460 )
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
461 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
462
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
463 /// 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: 49518
diff changeset
464 /// 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: 49518
diff changeset
465 /// 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: 49518
diff changeset
466 pub fn get_byte_size(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
467 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
468 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
469 item: &[u8],
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
470 ) -> Result<Option<u64>, HgError> {
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
471 self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
472 section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
473 item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
474 "byte quantity",
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
475 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: 50802
diff changeset
476 true,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
477 )
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
478 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
479
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
480 /// 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: 49518
diff changeset
481 /// 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: 49518
diff changeset
482 /// found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
483 pub fn get_option(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
484 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
485 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
486 item: &[u8],
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
487 ) -> Result<Option<bool>, HgError> {
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
488 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: 50802
diff changeset
489 }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
490
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
491 /// 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: 50802
diff changeset
492 /// 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: 50802
diff changeset
493 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: 50802
diff changeset
494 &self,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
495 section: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
496 item: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
497 ) -> 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: 50802
diff changeset
498 self.get_parse(section, item, "boolean", values::parse_bool, false)
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
499 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
500
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
501 /// 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: 49518
diff changeset
502 /// 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: 49518
diff changeset
503 pub fn get_bool(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
504 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
505 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
506 item: &[u8],
50802
f8412da86d05 rust-config: add support for default config items
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50248
diff changeset
507 ) -> Result<bool, HgError> {
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
508 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: 49518
diff changeset
509 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
510
50803
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
511 /// 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: 50802
diff changeset
512 /// 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: 50802
diff changeset
513 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: 50802
diff changeset
514 &self,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
515 section: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
516 item: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
517 ) -> Result<bool, HgError> {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
518 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: 50802
diff changeset
519 }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50802
diff changeset
520
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
521 /// 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: 49518
diff changeset
522 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: 49518
diff changeset
523 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: 49518
diff changeset
524 match value {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
525 Some(c) => !c.starts_with(b"!"),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
526 None => false,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
527 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
528 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
529
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
530 /// 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: 49518
diff changeset
531 /// byte strings.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
532 pub fn get_list(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
533 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
534 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
535 item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
536 ) -> Option<Vec<Vec<u8>>> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
537 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: 49518
diff changeset
538 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
539
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
540 /// 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: 49518
diff changeset
541 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: 49518
diff changeset
542 self.get_inner(section, item)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
543 .map(|(_, value)| value.bytes.as_ref())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
544 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
545
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
546 /// 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: 49518
diff changeset
547 pub fn get_with_origin(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
548 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
549 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
550 item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
551 ) -> Option<(&[u8], &ConfigOrigin)> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
552 self.get_inner(section, item)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
553 .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: 49518
diff changeset
554 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
555
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
556 /// 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: 49518
diff changeset
557 fn get_inner(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
558 &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
559 section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
560 item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
561 ) -> Option<(&ConfigLayer, &ConfigValue)> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
562 // 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: 49518
diff changeset
563 // 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: 49518
diff changeset
564 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: 49518
diff changeset
565 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: 49518
diff changeset
566 if !layer.trusted {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
567 continue;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
568 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
569 //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: 49518
diff changeset
570 //
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
571 // 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: 49518
diff changeset
572 // "tweakdefault" is part of "HGPLAINEXCEPT").
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
573 //
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
574 // 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: 49518
diff changeset
575 // 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: 49518
diff changeset
576 // account here.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
577 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: 49518
diff changeset
578 {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
579 continue;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
580 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
581 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: 49518
diff changeset
582 return Some((layer, v));
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
583 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
584 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
585 None
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
586 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
587
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
588 /// 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: 49518
diff changeset
589 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: 49518
diff changeset
590 self.layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
591 .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
592 .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: 49518
diff changeset
593 .collect()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
594 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
595
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
596 /// 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: 49518
diff changeset
597 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: 49518
diff changeset
598 self.layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
599 .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
600 .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: 49518
diff changeset
601 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
602
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
603 /// 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: 49518
diff changeset
604 pub fn iter_section<'a>(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
605 &'a self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
606 section: &'a [u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
607 ) -> impl Iterator<Item = (&[u8], &[u8])> + 'a {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
608 // Deduplicate keys redefined in multiple layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
609 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: 49518
diff changeset
610 let mut key_is_new =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
611 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: 49518
diff changeset
612 keys_already_seen.insert(key)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
613 };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
614 // 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: 49518
diff changeset
615 // 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: 49518
diff changeset
616 // `keys_already_seen` set):
50017
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50009
diff changeset
617 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: 50009
diff changeset
618 .layers
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50009
diff changeset
619 .iter()
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50009
diff changeset
620 .rev()
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50009
diff changeset
621 .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: 50009
diff changeset
622 .peekable();
50009
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
623 std::iter::from_fn(move || loop {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
624 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: 49518
diff changeset
625 return Some(pair);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
626 } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
627 layer_iters.next();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
628 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
629 })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
630 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
631
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
632 /// 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: 49518
diff changeset
633 /// of precedence.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
634 #[cfg(test)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
635 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: 49518
diff changeset
636 let mut res = vec![];
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
637 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: 49518
diff changeset
638 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: 49518
diff changeset
639 res.push(v.bytes.as_ref());
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
640 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
641 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
642 res
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
643 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
644
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
645 // 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: 49518
diff changeset
646 fn tweakdefaults_layer() -> ConfigLayer {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
647 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: 49518
diff changeset
648
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
649 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: 49518
diff changeset
650 layer.add(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
651 section[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
652 item[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
653 value[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
654 None,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
655 );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
656 };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
657 // duplication of [tweakrc] from [ui.py]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
658 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: 49518
diff changeset
659 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: 49518
diff changeset
660 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: 49518
diff changeset
661 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: 49518
diff changeset
662 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: 49518
diff changeset
663 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: 49518
diff changeset
664 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: 49518
diff changeset
665 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: 49518
diff changeset
666 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: 49518
diff changeset
667 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: 49518
diff changeset
668 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: 49518
diff changeset
669 layer
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
670 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
671
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
672 // 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: 49518
diff changeset
673 pub fn tweakdefaults(&mut self) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
674 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: 49518
diff changeset
675 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
676 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
677
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
678 #[cfg(test)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
679 mod tests {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
680 use super::*;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
681 use pretty_assertions::assert_eq;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
682 use std::fs::File;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
683 use std::io::Write;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
684
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
685 #[test]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
686 fn test_include_layer_ordering() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
687 let tmpdir = tempfile::tempdir().unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
688 let tmpdir_path = tmpdir.path();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
689 let mut included_file =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
690 File::create(&tmpdir_path.join("included.rc")).unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
691
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
692 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: 49518
diff changeset
693 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: 49518
diff changeset
694 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: 49518
diff changeset
695 let data =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
696 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: 49518
diff changeset
697 [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: 49518
diff changeset
698 config_file.write_all(data).unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
699
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
700 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: 49518
diff changeset
701 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: 49518
diff changeset
702 .expect("expected valid config");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
703
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
704 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: 49518
diff changeset
705 assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
706 value,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
707 &ConfigValue {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
708 bytes: b"value2".to_vec(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
709 line: Some(4)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
710 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
711 );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
712
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
713 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: 49518
diff changeset
714 assert_eq!(value, b"value2",);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
715 assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
716 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: 49518
diff changeset
717 [b"value2", b"value1", b"value0"]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
718 );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
719
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
720 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: 49518
diff changeset
721 assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
722 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: 49518
diff changeset
723 Some(1024 + 512)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
724 );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
725 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: 49518
diff changeset
726 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: 49518
diff changeset
727 }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49518
diff changeset
728 }