Mercurial > hg-stable
annotate rust/hg-core/src/config/mod.rs @ 50248:af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
clippy emits a warning that all the Result types are way too large
because of HgError includes ConfigValueParseError as one of the variants,
so its size is 136 bytes.
By boxing ConfigValueParseError we're hopefully making everything faster
"for free".
author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
---|---|
date | Mon, 27 Feb 2023 18:24:29 +0000 |
parents | a11237723332 |
children | f8412da86d05 |
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 |
95d6f31e88db
hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
12 mod layer; |
49518
467d9df98c68
rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49517
diff
changeset
|
13 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
|
14 mod values; |
48743
39c447e03dbc
rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents:
47410
diff
changeset
|
15 pub use layer::{ConfigError, ConfigOrigin, ConfigParseError}; |
49518
467d9df98c68
rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49517
diff
changeset
|
16 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
|
17 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
18 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
|
19 use self::layer::ConfigValue; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 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
|
24 use std::env; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
25 use std::fmt; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
26 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
|
27 use std::str; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
28 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
29 /// 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
|
30 /// 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
|
31 #[derive(Clone)] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
32 pub struct Config { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
33 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
|
34 plain: PlainInfo, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
35 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
36 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
37 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
|
38 fn display_bytes( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
39 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
40 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
|
41 ) -> std::io::Result<()> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
42 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
|
43 write_bytes!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
44 out, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
45 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
|
46 index, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
47 if layer.trusted { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
48 &b"yes"[..] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
49 } else { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
50 &b"no"[..] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
51 }, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
52 layer |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
53 )?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
54 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
55 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
56 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
57 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
58 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
59 pub enum ConfigSource { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
60 /// 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
|
61 AbsPath(PathBuf), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
62 /// 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
|
63 Parsed(layer::ConfigLayer), |
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 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
66 #[derive(Debug)] |
50248
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50017
diff
changeset
|
67 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
|
68 pub origin: ConfigOrigin, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
75 |
50248
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50017
diff
changeset
|
76 // 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
|
77 pub type ConfigValueParseError = Box<ConfigValueParseErrorDetails>; |
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50017
diff
changeset
|
78 |
50009
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
79 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
|
80 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
|
81 // 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
|
82 // corresponding python code |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
83 write!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
84 f, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
85 "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
|
86 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
|
87 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
|
88 self.expected_type, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
89 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
|
90 ) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
91 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
92 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
93 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
94 /// 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
|
95 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
|
96 // 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
|
97 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
|
98 return false; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
99 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
100 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
|
101 return plain.plainalias(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
102 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
103 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
|
104 return plain.plainrevsetalias(); |
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 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
|
107 return plain.plaintemplatealias(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
108 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
109 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
|
110 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
|
111 b"debug", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
112 b"fallbackencoding", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
113 b"quiet", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
114 b"slash", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
115 b"logtemplate", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
116 b"message-output", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
117 b"statuscopies", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
118 b"style", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
119 b"traceback", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
120 b"verbose", |
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 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
|
123 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
124 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
|
125 &[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
|
126 sections_to_delete.contains(§ion) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
127 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
128 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
129 impl Config { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
130 /// 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
|
131 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
|
132 Self { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
133 layers: Vec::new(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
134 plain: PlainInfo::empty(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
135 } |
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 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
138 /// 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
|
139 /// |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
140 /// 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
|
141 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
|
142 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
|
143 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
|
144 // 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
|
145 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
|
146 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
|
147 } |
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 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
|
150 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
|
151 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
|
152 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
153 // 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
|
154 // 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
|
155 // 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
|
156 // `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
|
157 // 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
|
158 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
|
159 "RHG_ON_UNSUPPORTED", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
160 b"rhg", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
161 b"on-unsupported", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
162 ); |
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( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
164 "RHG_FALLBACK_EXECUTABLE", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
165 b"rhg", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
166 b"fallback-executable", |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
167 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
168 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
169 // 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
|
170 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
|
171 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
|
172 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 } else { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
179 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
|
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 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
183 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
184 Ok(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 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
187 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
|
188 &mut self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
189 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
|
190 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
|
191 ) -> Result<(), ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
192 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
|
193 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
|
194 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
195 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
|
196 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
|
197 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
|
198 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
|
199 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
200 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
201 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
202 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
203 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
|
204 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
|
205 .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
|
206 .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
|
207 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
208 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
|
209 .map(|result| { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
210 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
|
211 }) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
212 .collect::<Result<Vec<_>, _>>()?; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
213 file_paths.sort(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
214 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
|
215 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
|
216 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
|
217 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
218 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
219 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
220 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
221 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
222 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
223 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
|
224 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
|
225 .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
|
226 .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
|
227 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
228 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
|
229 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
230 Ok(()) |
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 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
|
234 &mut self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
235 var: &str, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
236 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
237 key: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
238 ) { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 layer.add( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
243 section.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
244 key.to_owned(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
245 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
|
246 None, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
247 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
248 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
|
249 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
250 } |
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 #[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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 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
|
258 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
259 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
|
260 // 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
|
261 // 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
|
262 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
|
263 // 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
|
264 // `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
|
265 // `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
|
266 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
|
267 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
|
268 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
|
269 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
270 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
271 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
|
272 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
273 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
274 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
275 #[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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
281 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
|
282 if !darwin { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
283 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
|
284 .map(PathBuf::from) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
285 .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
|
286 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
287 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
|
288 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
289 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
290 Ok(()) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
291 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
292 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
293 /// 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
|
294 /// 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
|
295 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
|
296 sources: Vec<ConfigSource>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
297 ) -> Result<Self, ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
298 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
|
299 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
300 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
|
301 match source { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
302 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
|
303 ConfigSource::AbsPath(c) => { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
304 // 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
|
305 // 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
|
306 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
|
307 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
|
308 Ok(data) => data, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
309 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
310 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
|
311 } |
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 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
314 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
315 Ok(Config { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
316 layers, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
317 plain: PlainInfo::empty(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
318 }) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
319 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
320 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
321 /// 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
|
322 /// with `self`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
323 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
|
324 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
325 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
|
326 ) -> Result<Self, ConfigError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
327 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
|
328 .layers |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
329 .iter() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
330 .cloned() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
331 .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
|
332 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
333 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
|
334 layers: other_layers, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
335 plain: PlainInfo::empty(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
336 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
337 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
|
338 // 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
|
339 // `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
|
340 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
|
341 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
342 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
|
343 Ok(repo_config) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
344 } |
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 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
|
347 self.plain = plain; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
348 } |
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 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
|
351 &'config self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
352 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
353 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
354 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
|
355 parse: impl Fn(&'config [u8]) -> Option<T>, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
356 ) -> Result<Option<T>, ConfigValueParseError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
357 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
|
358 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
|
359 Some(b) => Ok(Some(b)), |
50248
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50017
diff
changeset
|
360 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
|
361 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
|
362 line: v.line, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
363 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
|
364 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
|
365 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
|
366 expected_type, |
50248
af9d050f2bb8
rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50017
diff
changeset
|
367 })), |
50009
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
368 }, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
369 None => Ok(None), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
370 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
371 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
372 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
373 /// 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
|
374 /// 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
|
375 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
|
376 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
377 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
378 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
379 ) -> Result<Option<&str>, ConfigValueParseError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
380 self.get_parse(section, item, "ASCII or UTF-8 string", |value| { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
381 str::from_utf8(value).ok() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
382 }) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
383 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
384 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
385 /// 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
|
386 /// 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
|
387 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
|
388 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
389 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
390 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
391 ) -> Result<Option<u32>, ConfigValueParseError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
392 self.get_parse(section, item, "valid integer", |value| { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
393 str::from_utf8(value).ok()?.parse().ok() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
394 }) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
395 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
396 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
397 /// 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
|
398 /// 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
|
399 /// 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
|
400 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
|
401 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
402 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
403 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
404 ) -> Result<Option<u64>, ConfigValueParseError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
405 self.get_parse(section, item, "byte quantity", values::parse_byte_size) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
406 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
407 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
408 /// 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
|
409 /// 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
|
410 /// found, or `None`. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
411 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
|
412 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
413 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
414 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
415 ) -> Result<Option<bool>, ConfigValueParseError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
416 self.get_parse(section, item, "boolean", values::parse_bool) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
417 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
418 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
419 /// 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
|
420 /// 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
|
421 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
|
422 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
423 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
424 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
425 ) -> Result<bool, ConfigValueParseError> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
426 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
|
427 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
428 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
429 /// 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
|
430 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
|
431 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
|
432 match value { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
433 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
|
434 None => false, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
435 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
436 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
437 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
438 /// 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
|
439 /// byte strings. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
440 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
|
441 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
442 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
443 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
444 ) -> 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
|
445 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
|
446 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
447 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
448 /// 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
|
449 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
|
450 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
|
451 .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
|
452 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
453 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
454 /// 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
|
455 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
|
456 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
457 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
458 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
459 ) -> Option<(&[u8], &ConfigOrigin)> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
460 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
|
461 .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
|
462 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
463 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
464 /// 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
|
465 fn get_inner( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
466 &self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
467 section: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
468 item: &[u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
469 ) -> Option<(&ConfigLayer, &ConfigValue)> { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
470 // 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
|
471 // 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
|
472 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
|
473 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
|
474 if !layer.trusted { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
475 continue; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
476 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
477 //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
|
478 // |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
479 // 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
|
480 // "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
|
481 // |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
482 // 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
|
483 // 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
|
484 // account here. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
485 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
|
486 { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
487 continue; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
488 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
489 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
|
490 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
|
491 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
492 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
493 None |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
494 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
495 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
496 /// 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
|
497 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
|
498 self.layers |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
499 .iter() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
500 .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
|
501 .collect() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
502 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
503 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
504 /// 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
|
505 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
|
506 self.layers |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
507 .iter() |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
508 .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
|
509 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
510 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
511 /// 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
|
512 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
|
513 &'a self, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
514 section: &'a [u8], |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
515 ) -> 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
|
516 // 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
|
517 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
|
518 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
|
519 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
|
520 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
|
521 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
522 // 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
|
523 // 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
|
524 // `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
|
525 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
|
526 .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
|
527 .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
|
528 .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
|
529 .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
|
530 .peekable(); |
50009
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
531 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
|
532 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
|
533 return Some(pair); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
534 } else { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
535 layer_iters.next(); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
536 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
537 }) |
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 /// 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
|
541 /// of precedence. |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
542 #[cfg(test)] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
543 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
549 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
550 res |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
551 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
552 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
553 // 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
|
554 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
|
555 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
|
556 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
557 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
|
558 layer.add( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
559 section[..].into(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
560 item[..].into(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
561 value[..].into(), |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
562 None, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
563 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
564 }; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
565 // 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
|
566 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
|
567 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
|
568 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
|
569 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
|
570 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
|
571 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
|
572 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
|
573 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
|
574 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
|
575 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
|
576 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
|
577 layer |
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 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
580 // 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
|
581 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
|
582 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
|
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 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
586 #[cfg(test)] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
587 mod tests { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
588 use super::*; |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
593 #[test] |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
594 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
|
595 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
|
596 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
|
597 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
|
598 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
|
599 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
600 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
|
601 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
|
602 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
|
603 let data = |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
604 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
|
605 [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
|
606 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
|
607 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
608 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
|
609 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
|
610 .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
|
611 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
612 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
|
613 assert_eq!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
614 value, |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
615 &ConfigValue { |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
616 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
|
617 line: Some(4) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
618 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
619 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
620 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
621 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
|
622 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
|
623 assert_eq!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
624 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
|
625 [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
|
626 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
627 |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
628 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
|
629 assert_eq!( |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
630 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
|
631 Some(1024 + 512) |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
632 ); |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
633 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
|
634 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
|
635 } |
2cd8352f7e11
rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
49518
diff
changeset
|
636 } |