annotate rust/hg-core/src/config/config.rs @ 46504:2e5dd18d6dc3

rhg: Add support for --config CLI arguments Differential Revision: https://phab.mercurial-scm.org/D9971
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 08 Feb 2021 23:08:44 +0100
parents eace48b4a786
children d2e61f00ee9d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1 // config.rs
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
2 //
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
3 // Copyright 2020
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
4 // Valentin Gatien-Baron,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
5 // Raphaël Gomès <rgomes@octobus.net>
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
6 //
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
7 // This software may be used and distributed according to the terms of the
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
8 // GNU General Public License version 2 or any later version.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
9
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
10 use super::layer;
46447
0cb1b02228a6 rust: use HgError in ConfigError
Simon Sapin <simon.sapin@octobus.net>
parents: 46446
diff changeset
11 use crate::config::layer::{
0cb1b02228a6 rust: use HgError in ConfigError
Simon Sapin <simon.sapin@octobus.net>
parents: 46446
diff changeset
12 ConfigError, ConfigLayer, ConfigParseError, ConfigValue,
0cb1b02228a6 rust: use HgError in ConfigError
Simon Sapin <simon.sapin@octobus.net>
parents: 46446
diff changeset
13 };
46483
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
14 use crate::utils::files::get_bytes_from_path;
46499
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
15 use format_bytes::{write_bytes, DisplayBytes};
46483
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
16 use std::env;
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
17 use std::path::{Path, PathBuf};
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
18
46483
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
19 use crate::errors::{HgResultExt, IoResultExt};
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
20
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
21 /// Holds the config values for the current repository
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
22 /// TODO update this docstring once we support more sources
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
23 pub struct Config {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
24 layers: Vec<layer::ConfigLayer>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
25 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
26
46499
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
27 impl DisplayBytes for Config {
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
28 fn display_bytes(
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
29 &self,
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
30 out: &mut dyn std::io::Write,
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
31 ) -> std::io::Result<()> {
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
32 for (index, layer) in self.layers.iter().rev().enumerate() {
46499
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
33 write_bytes!(
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
34 out,
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
35 b"==== Layer {} (trusted: {}) ====\n{}",
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
36 index,
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
37 if layer.trusted {
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
38 &b"yes"[..]
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
39 } else {
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
40 &b"no"[..]
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
41 },
eace48b4a786 rust: Use the DisplayBytes trait in config printing
Simon Sapin <simon.sapin@octobus.net>
parents: 46486
diff changeset
42 layer
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
43 )?;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
44 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
45 Ok(())
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
46 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
47 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
48
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
49 pub enum ConfigSource {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
50 /// Absolute path to a config file
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
51 AbsPath(PathBuf),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
52 /// Already parsed (from the CLI, env, Python resources, etc.)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
53 Parsed(layer::ConfigLayer),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
54 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
55
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
56 pub fn parse_bool(v: &[u8]) -> Option<bool> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
57 match v.to_ascii_lowercase().as_slice() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
58 b"1" | b"yes" | b"true" | b"on" | b"always" => Some(true),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
59 b"0" | b"no" | b"false" | b"off" | b"never" => Some(false),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
60 _ => None,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
61 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
62 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
63
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
64 impl Config {
46483
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
65 /// Load system and user configuration from various files.
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
66 ///
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
67 /// This is also affected by some environment variables.
46504
2e5dd18d6dc3 rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
68 pub fn load(
2e5dd18d6dc3 rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
69 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
2e5dd18d6dc3 rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
70 ) -> Result<Self, ConfigError> {
46483
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
71 let mut config = Self { layers: Vec::new() };
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
72 let opt_rc_path = env::var_os("HGRCPATH");
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
73 // HGRCPATH replaces system config
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
74 if opt_rc_path.is_none() {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
75 config.add_system_config()?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
76 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
77 config.add_for_environment_variable("EDITOR", b"ui", b"editor");
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
78 config.add_for_environment_variable("VISUAL", b"ui", b"editor");
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
79 config.add_for_environment_variable("PAGER", b"pager", b"pager");
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
80 // HGRCPATH replaces user config
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
81 if opt_rc_path.is_none() {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
82 config.add_user_config()?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
83 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
84 if let Some(rc_path) = &opt_rc_path {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
85 for path in env::split_paths(rc_path) {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
86 if !path.as_os_str().is_empty() {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
87 if path.is_dir() {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
88 config.add_trusted_dir(&path)?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
89 } else {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
90 config.add_trusted_file(&path)?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
91 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
92 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
93 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
94 }
46504
2e5dd18d6dc3 rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
95 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
2e5dd18d6dc3 rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
96 config.layers.push(layer)
2e5dd18d6dc3 rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
97 }
46483
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
98 Ok(config)
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
99 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
100
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
101 fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
102 if let Some(entries) = std::fs::read_dir(path)
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
103 .for_file(path)
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
104 .io_not_found_as_none()?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
105 {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
106 for entry in entries {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
107 let file_path = entry.for_file(path)?.path();
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
108 if file_path.extension() == Some(std::ffi::OsStr::new("rc")) {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
109 self.add_trusted_file(&file_path)?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
110 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
111 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
112 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
113 Ok(())
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
114 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
115
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
116 fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
117 if let Some(data) =
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
118 std::fs::read(path).for_file(path).io_not_found_as_none()?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
119 {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
120 self.layers.extend(ConfigLayer::parse(path, &data)?)
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
121 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
122 Ok(())
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
123 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
124
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
125 fn add_for_environment_variable(
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
126 &mut self,
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
127 var: &str,
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
128 section: &[u8],
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
129 key: &[u8],
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
130 ) {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
131 if let Some(value) = env::var_os(var) {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
132 let origin = layer::ConfigOrigin::Environment(var.into());
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
133 let mut layer = ConfigLayer::new(origin);
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
134 layer.add(
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
135 section.to_owned(),
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
136 key.to_owned(),
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
137 // `value` is not a path but this works for any `OsStr`:
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
138 get_bytes_from_path(value),
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
139 None,
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
140 );
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
141 self.layers.push(layer)
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
142 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
143 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
144
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
145 #[cfg(unix)] // TODO: other platforms
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
146 fn add_system_config(&mut self) -> Result<(), ConfigError> {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
147 let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
148 let etc = prefix.join("etc").join("mercurial");
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
149 self.add_trusted_file(&etc.join("hgrc"))?;
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
150 self.add_trusted_dir(&etc.join("hgrc.d"))
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
151 };
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
152 let root = Path::new("/");
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
153 // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0]
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
154 // instead? TODO: can this be a relative path?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
155 let hg = crate::utils::current_exe()?;
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
156 // TODO: this order (per-installation then per-system) matches
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
157 // `systemrcpath()` in `mercurial/scmposix.py`, but
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
158 // `mercurial/helptext/config.txt` suggests it should be reversed
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
159 if let Some(installation_prefix) = hg.parent().and_then(Path::parent) {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
160 if installation_prefix != root {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
161 add_for_prefix(&installation_prefix)?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
162 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
163 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
164 add_for_prefix(root)?;
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
165 Ok(())
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
166 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
167
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
168 #[cfg(unix)] // TODO: other plateforms
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
169 fn add_user_config(&mut self) -> Result<(), ConfigError> {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
170 let opt_home = home::home_dir();
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
171 if let Some(home) = &opt_home {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
172 self.add_trusted_file(&home.join(".hgrc"))?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
173 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
174 let darwin = cfg!(any(target_os = "macos", target_os = "ios"));
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
175 if !darwin {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
176 if let Some(config_home) = env::var_os("XDG_CONFIG_HOME")
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
177 .map(PathBuf::from)
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
178 .or_else(|| opt_home.map(|home| home.join(".config")))
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
179 {
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
180 self.add_trusted_file(&config_home.join("hg").join("hgrc"))?
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
181 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
182 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
183 Ok(())
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
184 }
2845892dd489 rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46481
diff changeset
185
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
186 /// Loads in order, which means that the precedence is the same
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
187 /// as the order of `sources`.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
188 pub fn load_from_explicit_sources(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
189 sources: Vec<ConfigSource>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
190 ) -> Result<Self, ConfigError> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
191 let mut layers = vec![];
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
192
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
193 for source in sources.into_iter() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
194 match source {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
195 ConfigSource::Parsed(c) => layers.push(c),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
196 ConfigSource::AbsPath(c) => {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
197 // TODO check if it should be trusted
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
198 // mercurial/ui.py:427
46481
0d734c0ae1cf rust: replace read_whole_file with std::fs::read
Simon Sapin <simon.sapin@octobus.net>
parents: 46447
diff changeset
199 let data = match std::fs::read(&c) {
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
200 Err(_) => continue, // same as the python code
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
201 Ok(data) => data,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
202 };
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
203 layers.extend(ConfigLayer::parse(&c, &data)?)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
204 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
205 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
206 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
207
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
208 Ok(Config { layers })
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
209 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
210
46486
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
211 /// Loads the per-repository config into a new `Config` which is combined
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
212 /// with `self`.
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
213 pub(crate) fn combine_with_repo(
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
214 &self,
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
215 repo_config_files: &[PathBuf],
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
216 ) -> Result<Self, ConfigError> {
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
217 let (cli_layers, other_layers) = self
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
218 .layers
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
219 .iter()
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
220 .cloned()
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
221 .partition(ConfigLayer::is_from_command_line);
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
222
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
223 let mut repo_config = Self {
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
224 layers: other_layers,
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
225 };
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
226 for path in repo_config_files {
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
227 // TODO: check if this file should be trusted:
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
228 // `mercurial/ui.py:427`
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
229 repo_config.add_trusted_file(path)?;
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
230 }
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
231 repo_config.layers.extend(cli_layers);
d7685105e504 rhg: Parse per-repository configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46483
diff changeset
232 Ok(repo_config)
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
233 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
234
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
235 /// Returns an `Err` if the first value found is not a valid boolean.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
236 /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
237 /// found, or `None`.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
238 pub fn get_option(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
239 &self,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
240 section: &[u8],
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
241 item: &[u8],
46447
0cb1b02228a6 rust: use HgError in ConfigError
Simon Sapin <simon.sapin@octobus.net>
parents: 46446
diff changeset
242 ) -> Result<Option<bool>, ConfigParseError> {
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
243 match self.get_inner(&section, &item) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
244 Some((layer, v)) => match parse_bool(&v.bytes) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
245 Some(b) => Ok(Some(b)),
46447
0cb1b02228a6 rust: use HgError in ConfigError
Simon Sapin <simon.sapin@octobus.net>
parents: 46446
diff changeset
246 None => Err(ConfigParseError {
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
247 origin: layer.origin.to_owned(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
248 line: v.line,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
249 bytes: v.bytes.to_owned(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
250 }),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
251 },
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
252 None => Ok(None),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
253 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
254 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
255
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
256 /// Returns the corresponding boolean in the config. Returns `Ok(false)`
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
257 /// if the value is not found, an `Err` if it's not a valid boolean.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
258 pub fn get_bool(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
259 &self,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
260 section: &[u8],
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
261 item: &[u8],
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
262 ) -> Result<bool, ConfigError> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
263 Ok(self.get_option(section, item)?.unwrap_or(false))
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
264 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
265
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
266 /// Returns the raw value bytes of the first one found, or `None`.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
267 pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
268 self.get_inner(section, item)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
269 .map(|(_, value)| value.bytes.as_ref())
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
270 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
271
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
272 /// Returns the layer and the value of the first one found, or `None`.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
273 fn get_inner(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
274 &self,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
275 section: &[u8],
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
276 item: &[u8],
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
277 ) -> Option<(&ConfigLayer, &ConfigValue)> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
278 for layer in self.layers.iter().rev() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
279 if !layer.trusted {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
280 continue;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
281 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
282 if let Some(v) = layer.get(&section, &item) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
283 return Some((&layer, v));
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
284 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
285 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
286 None
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
287 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
288
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
289 /// Get raw values bytes from all layers (even untrusted ones) in order
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
290 /// of precedence.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
291 #[cfg(test)]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
292 fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
293 let mut res = vec![];
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
294 for layer in self.layers.iter().rev() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
295 if let Some(v) = layer.get(&section, &item) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
296 res.push(v.bytes.as_ref());
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
297 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
298 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
299 res
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
300 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
301 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
302
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
303 #[cfg(test)]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
304 mod tests {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
305 use super::*;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
306 use pretty_assertions::assert_eq;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
307 use std::fs::File;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
308 use std::io::Write;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
309
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
310 #[test]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
311 fn test_include_layer_ordering() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
312 let tmpdir = tempfile::tempdir().unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
313 let tmpdir_path = tmpdir.path();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
314 let mut included_file =
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
315 File::create(&tmpdir_path.join("included.rc")).unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
316
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
317 included_file.write_all(b"[section]\nitem=value1").unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
318 let base_config_path = tmpdir_path.join("base.rc");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
319 let mut config_file = File::create(&base_config_path).unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
320 let data =
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
321 b"[section]\nitem=value0\n%include included.rc\nitem=value2";
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
322 config_file.write_all(data).unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
323
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
324 let sources = vec![ConfigSource::AbsPath(base_config_path)];
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
325 let config = Config::load_from_explicit_sources(sources)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
326 .expect("expected valid config");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
327
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
328 let (_, value) = config.get_inner(b"section", b"item").unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
329 assert_eq!(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
330 value,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
331 &ConfigValue {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
332 bytes: b"value2".to_vec(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
333 line: Some(4)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
334 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
335 );
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
336
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
337 let value = config.get(b"section", b"item").unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
338 assert_eq!(value, b"value2",);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
339 assert_eq!(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
340 config.get_all(b"section", b"item"),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
341 [b"value2", b"value1", b"value0"]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
342 );
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
343 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
344 }