##// END OF EJS Templates
rust: Move config value parsing functions to a new module...
Simon Sapin -
r47344:a687a7f2 default
parent child Browse files
Show More
@@ -0,0 +1,43 b''
1 //! Parsing functions for various type of configuration values.
2 //!
3 //! Returning `None` indicates a syntax error. Using a `Result` would be more
4 //! correct but would take more boilerplate for converting between error types,
5 //! compared to using `.ok()` on inner results of various error types to
6 //! convert them all to options. The `Config::get_parse` method later converts
7 //! those options to results with `ConfigValueParseError`, which contains
8 //! details about where the value came from (but omits details of what’s
9 //! invalid inside the value).
10
11 pub(super) fn parse_bool(v: &[u8]) -> Option<bool> {
12 match v.to_ascii_lowercase().as_slice() {
13 b"1" | b"yes" | b"true" | b"on" | b"always" => Some(true),
14 b"0" | b"no" | b"false" | b"off" | b"never" => Some(false),
15 _ => None,
16 }
17 }
18
19 pub(super) fn parse_byte_size(value: &[u8]) -> Option<u64> {
20 let value = std::str::from_utf8(value).ok()?.to_ascii_lowercase();
21 const UNITS: &[(&str, u64)] = &[
22 ("g", 1 << 30),
23 ("gb", 1 << 30),
24 ("m", 1 << 20),
25 ("mb", 1 << 20),
26 ("k", 1 << 10),
27 ("kb", 1 << 10),
28 ("b", 1 << 0), // Needs to be last
29 ];
30 for &(unit, multiplier) in UNITS {
31 // TODO: use `value.strip_suffix(unit)` when we require Rust 1.45+
32 if value.ends_with(unit) {
33 let value_before_unit = &value[..value.len() - unit.len()];
34 let float: f64 = value_before_unit.trim().parse().ok()?;
35 if float >= 0.0 {
36 return Some((float * multiplier as f64).round() as u64);
37 } else {
38 return None;
39 }
40 }
41 }
42 value.parse().ok()
43 }
@@ -1,15 +1,16 b''
1 // config.rs
1 // config.rs
2 //
2 //
3 // Copyright 2020
3 // Copyright 2020
4 // Valentin Gatien-Baron,
4 // Valentin Gatien-Baron,
5 // Raphaël Gomès <rgomes@octobus.net>
5 // Raphaël Gomès <rgomes@octobus.net>
6 //
6 //
7 // This software may be used and distributed according to the terms of the
7 // This software may be used and distributed according to the terms of the
8 // GNU General Public License version 2 or any later version.
8 // GNU General Public License version 2 or any later version.
9
9
10 //! Mercurial config parsing and interfaces.
10 //! Mercurial config parsing and interfaces.
11
11
12 mod config;
12 mod config;
13 mod layer;
13 mod layer;
14 mod values;
14 pub use config::{Config, ConfigValueParseError};
15 pub use config::{Config, ConfigValueParseError};
15 pub use layer::{ConfigError, ConfigParseError};
16 pub use layer::{ConfigError, ConfigParseError};
@@ -1,438 +1,405 b''
1 // config.rs
1 // config.rs
2 //
2 //
3 // Copyright 2020
3 // Copyright 2020
4 // Valentin Gatien-Baron,
4 // Valentin Gatien-Baron,
5 // Raphaël Gomès <rgomes@octobus.net>
5 // Raphaël Gomès <rgomes@octobus.net>
6 //
6 //
7 // This software may be used and distributed according to the terms of the
7 // This software may be used and distributed according to the terms of the
8 // GNU General Public License version 2 or any later version.
8 // GNU General Public License version 2 or any later version.
9
9
10 use super::layer;
10 use super::layer;
11 use super::values;
11 use crate::config::layer::{
12 use crate::config::layer::{
12 ConfigError, ConfigLayer, ConfigOrigin, ConfigValue,
13 ConfigError, ConfigLayer, ConfigOrigin, ConfigValue,
13 };
14 };
14 use crate::utils::files::get_bytes_from_os_str;
15 use crate::utils::files::get_bytes_from_os_str;
15 use format_bytes::{write_bytes, DisplayBytes};
16 use format_bytes::{write_bytes, DisplayBytes};
16 use std::env;
17 use std::env;
17 use std::path::{Path, PathBuf};
18 use std::path::{Path, PathBuf};
18 use std::str;
19 use std::str;
19
20
20 use crate::errors::{HgResultExt, IoResultExt};
21 use crate::errors::{HgResultExt, IoResultExt};
21
22
22 /// Holds the config values for the current repository
23 /// Holds the config values for the current repository
23 /// TODO update this docstring once we support more sources
24 /// TODO update this docstring once we support more sources
24 pub struct Config {
25 pub struct Config {
25 layers: Vec<layer::ConfigLayer>,
26 layers: Vec<layer::ConfigLayer>,
26 }
27 }
27
28
28 impl DisplayBytes for Config {
29 impl DisplayBytes for Config {
29 fn display_bytes(
30 fn display_bytes(
30 &self,
31 &self,
31 out: &mut dyn std::io::Write,
32 out: &mut dyn std::io::Write,
32 ) -> std::io::Result<()> {
33 ) -> std::io::Result<()> {
33 for (index, layer) in self.layers.iter().rev().enumerate() {
34 for (index, layer) in self.layers.iter().rev().enumerate() {
34 write_bytes!(
35 write_bytes!(
35 out,
36 out,
36 b"==== Layer {} (trusted: {}) ====\n{}",
37 b"==== Layer {} (trusted: {}) ====\n{}",
37 index,
38 index,
38 if layer.trusted {
39 if layer.trusted {
39 &b"yes"[..]
40 &b"yes"[..]
40 } else {
41 } else {
41 &b"no"[..]
42 &b"no"[..]
42 },
43 },
43 layer
44 layer
44 )?;
45 )?;
45 }
46 }
46 Ok(())
47 Ok(())
47 }
48 }
48 }
49 }
49
50
50 pub enum ConfigSource {
51 pub enum ConfigSource {
51 /// Absolute path to a config file
52 /// Absolute path to a config file
52 AbsPath(PathBuf),
53 AbsPath(PathBuf),
53 /// Already parsed (from the CLI, env, Python resources, etc.)
54 /// Already parsed (from the CLI, env, Python resources, etc.)
54 Parsed(layer::ConfigLayer),
55 Parsed(layer::ConfigLayer),
55 }
56 }
56
57
57 #[derive(Debug)]
58 #[derive(Debug)]
58 pub struct ConfigValueParseError {
59 pub struct ConfigValueParseError {
59 pub origin: ConfigOrigin,
60 pub origin: ConfigOrigin,
60 pub line: Option<usize>,
61 pub line: Option<usize>,
61 pub section: Vec<u8>,
62 pub section: Vec<u8>,
62 pub item: Vec<u8>,
63 pub item: Vec<u8>,
63 pub value: Vec<u8>,
64 pub value: Vec<u8>,
64 pub expected_type: &'static str,
65 pub expected_type: &'static str,
65 }
66 }
66
67
67 pub fn parse_bool(v: &[u8]) -> Option<bool> {
68 match v.to_ascii_lowercase().as_slice() {
69 b"1" | b"yes" | b"true" | b"on" | b"always" => Some(true),
70 b"0" | b"no" | b"false" | b"off" | b"never" => Some(false),
71 _ => None,
72 }
73 }
74
75 pub fn parse_byte_size(value: &[u8]) -> Option<u64> {
76 let value = str::from_utf8(value).ok()?.to_ascii_lowercase();
77 const UNITS: &[(&str, u64)] = &[
78 ("g", 1 << 30),
79 ("gb", 1 << 30),
80 ("m", 1 << 20),
81 ("mb", 1 << 20),
82 ("k", 1 << 10),
83 ("kb", 1 << 10),
84 ("b", 1 << 0), // Needs to be last
85 ];
86 for &(unit, multiplier) in UNITS {
87 // TODO: use `value.strip_suffix(unit)` when we require Rust 1.45+
88 if value.ends_with(unit) {
89 let value_before_unit = &value[..value.len() - unit.len()];
90 let float: f64 = value_before_unit.trim().parse().ok()?;
91 if float >= 0.0 {
92 return Some((float * multiplier as f64).round() as u64);
93 } else {
94 return None;
95 }
96 }
97 }
98 value.parse().ok()
99 }
100
101 impl Config {
68 impl Config {
102 /// Load system and user configuration from various files.
69 /// Load system and user configuration from various files.
103 ///
70 ///
104 /// This is also affected by some environment variables.
71 /// This is also affected by some environment variables.
105 pub fn load(
72 pub fn load(
106 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
73 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
107 ) -> Result<Self, ConfigError> {
74 ) -> Result<Self, ConfigError> {
108 let mut config = Self { layers: Vec::new() };
75 let mut config = Self { layers: Vec::new() };
109 let opt_rc_path = env::var_os("HGRCPATH");
76 let opt_rc_path = env::var_os("HGRCPATH");
110 // HGRCPATH replaces system config
77 // HGRCPATH replaces system config
111 if opt_rc_path.is_none() {
78 if opt_rc_path.is_none() {
112 config.add_system_config()?
79 config.add_system_config()?
113 }
80 }
114 config.add_for_environment_variable("EDITOR", b"ui", b"editor");
81 config.add_for_environment_variable("EDITOR", b"ui", b"editor");
115 config.add_for_environment_variable("VISUAL", b"ui", b"editor");
82 config.add_for_environment_variable("VISUAL", b"ui", b"editor");
116 config.add_for_environment_variable("PAGER", b"pager", b"pager");
83 config.add_for_environment_variable("PAGER", b"pager", b"pager");
117 // HGRCPATH replaces user config
84 // HGRCPATH replaces user config
118 if opt_rc_path.is_none() {
85 if opt_rc_path.is_none() {
119 config.add_user_config()?
86 config.add_user_config()?
120 }
87 }
121 if let Some(rc_path) = &opt_rc_path {
88 if let Some(rc_path) = &opt_rc_path {
122 for path in env::split_paths(rc_path) {
89 for path in env::split_paths(rc_path) {
123 if !path.as_os_str().is_empty() {
90 if !path.as_os_str().is_empty() {
124 if path.is_dir() {
91 if path.is_dir() {
125 config.add_trusted_dir(&path)?
92 config.add_trusted_dir(&path)?
126 } else {
93 } else {
127 config.add_trusted_file(&path)?
94 config.add_trusted_file(&path)?
128 }
95 }
129 }
96 }
130 }
97 }
131 }
98 }
132 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
99 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
133 config.layers.push(layer)
100 config.layers.push(layer)
134 }
101 }
135 Ok(config)
102 Ok(config)
136 }
103 }
137
104
138 fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> {
105 fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> {
139 if let Some(entries) = std::fs::read_dir(path)
106 if let Some(entries) = std::fs::read_dir(path)
140 .when_reading_file(path)
107 .when_reading_file(path)
141 .io_not_found_as_none()?
108 .io_not_found_as_none()?
142 {
109 {
143 for entry in entries {
110 for entry in entries {
144 let file_path = entry.when_reading_file(path)?.path();
111 let file_path = entry.when_reading_file(path)?.path();
145 if file_path.extension() == Some(std::ffi::OsStr::new("rc")) {
112 if file_path.extension() == Some(std::ffi::OsStr::new("rc")) {
146 self.add_trusted_file(&file_path)?
113 self.add_trusted_file(&file_path)?
147 }
114 }
148 }
115 }
149 }
116 }
150 Ok(())
117 Ok(())
151 }
118 }
152
119
153 fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> {
120 fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> {
154 if let Some(data) = std::fs::read(path)
121 if let Some(data) = std::fs::read(path)
155 .when_reading_file(path)
122 .when_reading_file(path)
156 .io_not_found_as_none()?
123 .io_not_found_as_none()?
157 {
124 {
158 self.layers.extend(ConfigLayer::parse(path, &data)?)
125 self.layers.extend(ConfigLayer::parse(path, &data)?)
159 }
126 }
160 Ok(())
127 Ok(())
161 }
128 }
162
129
163 fn add_for_environment_variable(
130 fn add_for_environment_variable(
164 &mut self,
131 &mut self,
165 var: &str,
132 var: &str,
166 section: &[u8],
133 section: &[u8],
167 key: &[u8],
134 key: &[u8],
168 ) {
135 ) {
169 if let Some(value) = env::var_os(var) {
136 if let Some(value) = env::var_os(var) {
170 let origin = layer::ConfigOrigin::Environment(var.into());
137 let origin = layer::ConfigOrigin::Environment(var.into());
171 let mut layer = ConfigLayer::new(origin);
138 let mut layer = ConfigLayer::new(origin);
172 layer.add(
139 layer.add(
173 section.to_owned(),
140 section.to_owned(),
174 key.to_owned(),
141 key.to_owned(),
175 get_bytes_from_os_str(value),
142 get_bytes_from_os_str(value),
176 None,
143 None,
177 );
144 );
178 self.layers.push(layer)
145 self.layers.push(layer)
179 }
146 }
180 }
147 }
181
148
182 #[cfg(unix)] // TODO: other platforms
149 #[cfg(unix)] // TODO: other platforms
183 fn add_system_config(&mut self) -> Result<(), ConfigError> {
150 fn add_system_config(&mut self) -> Result<(), ConfigError> {
184 let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> {
151 let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> {
185 let etc = prefix.join("etc").join("mercurial");
152 let etc = prefix.join("etc").join("mercurial");
186 self.add_trusted_file(&etc.join("hgrc"))?;
153 self.add_trusted_file(&etc.join("hgrc"))?;
187 self.add_trusted_dir(&etc.join("hgrc.d"))
154 self.add_trusted_dir(&etc.join("hgrc.d"))
188 };
155 };
189 let root = Path::new("/");
156 let root = Path::new("/");
190 // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0]
157 // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0]
191 // instead? TODO: can this be a relative path?
158 // instead? TODO: can this be a relative path?
192 let hg = crate::utils::current_exe()?;
159 let hg = crate::utils::current_exe()?;
193 // TODO: this order (per-installation then per-system) matches
160 // TODO: this order (per-installation then per-system) matches
194 // `systemrcpath()` in `mercurial/scmposix.py`, but
161 // `systemrcpath()` in `mercurial/scmposix.py`, but
195 // `mercurial/helptext/config.txt` suggests it should be reversed
162 // `mercurial/helptext/config.txt` suggests it should be reversed
196 if let Some(installation_prefix) = hg.parent().and_then(Path::parent) {
163 if let Some(installation_prefix) = hg.parent().and_then(Path::parent) {
197 if installation_prefix != root {
164 if installation_prefix != root {
198 add_for_prefix(&installation_prefix)?
165 add_for_prefix(&installation_prefix)?
199 }
166 }
200 }
167 }
201 add_for_prefix(root)?;
168 add_for_prefix(root)?;
202 Ok(())
169 Ok(())
203 }
170 }
204
171
205 #[cfg(unix)] // TODO: other plateforms
172 #[cfg(unix)] // TODO: other plateforms
206 fn add_user_config(&mut self) -> Result<(), ConfigError> {
173 fn add_user_config(&mut self) -> Result<(), ConfigError> {
207 let opt_home = home::home_dir();
174 let opt_home = home::home_dir();
208 if let Some(home) = &opt_home {
175 if let Some(home) = &opt_home {
209 self.add_trusted_file(&home.join(".hgrc"))?
176 self.add_trusted_file(&home.join(".hgrc"))?
210 }
177 }
211 let darwin = cfg!(any(target_os = "macos", target_os = "ios"));
178 let darwin = cfg!(any(target_os = "macos", target_os = "ios"));
212 if !darwin {
179 if !darwin {
213 if let Some(config_home) = env::var_os("XDG_CONFIG_HOME")
180 if let Some(config_home) = env::var_os("XDG_CONFIG_HOME")
214 .map(PathBuf::from)
181 .map(PathBuf::from)
215 .or_else(|| opt_home.map(|home| home.join(".config")))
182 .or_else(|| opt_home.map(|home| home.join(".config")))
216 {
183 {
217 self.add_trusted_file(&config_home.join("hg").join("hgrc"))?
184 self.add_trusted_file(&config_home.join("hg").join("hgrc"))?
218 }
185 }
219 }
186 }
220 Ok(())
187 Ok(())
221 }
188 }
222
189
223 /// Loads in order, which means that the precedence is the same
190 /// Loads in order, which means that the precedence is the same
224 /// as the order of `sources`.
191 /// as the order of `sources`.
225 pub fn load_from_explicit_sources(
192 pub fn load_from_explicit_sources(
226 sources: Vec<ConfigSource>,
193 sources: Vec<ConfigSource>,
227 ) -> Result<Self, ConfigError> {
194 ) -> Result<Self, ConfigError> {
228 let mut layers = vec![];
195 let mut layers = vec![];
229
196
230 for source in sources.into_iter() {
197 for source in sources.into_iter() {
231 match source {
198 match source {
232 ConfigSource::Parsed(c) => layers.push(c),
199 ConfigSource::Parsed(c) => layers.push(c),
233 ConfigSource::AbsPath(c) => {
200 ConfigSource::AbsPath(c) => {
234 // TODO check if it should be trusted
201 // TODO check if it should be trusted
235 // mercurial/ui.py:427
202 // mercurial/ui.py:427
236 let data = match std::fs::read(&c) {
203 let data = match std::fs::read(&c) {
237 Err(_) => continue, // same as the python code
204 Err(_) => continue, // same as the python code
238 Ok(data) => data,
205 Ok(data) => data,
239 };
206 };
240 layers.extend(ConfigLayer::parse(&c, &data)?)
207 layers.extend(ConfigLayer::parse(&c, &data)?)
241 }
208 }
242 }
209 }
243 }
210 }
244
211
245 Ok(Config { layers })
212 Ok(Config { layers })
246 }
213 }
247
214
248 /// Loads the per-repository config into a new `Config` which is combined
215 /// Loads the per-repository config into a new `Config` which is combined
249 /// with `self`.
216 /// with `self`.
250 pub(crate) fn combine_with_repo(
217 pub(crate) fn combine_with_repo(
251 &self,
218 &self,
252 repo_config_files: &[PathBuf],
219 repo_config_files: &[PathBuf],
253 ) -> Result<Self, ConfigError> {
220 ) -> Result<Self, ConfigError> {
254 let (cli_layers, other_layers) = self
221 let (cli_layers, other_layers) = self
255 .layers
222 .layers
256 .iter()
223 .iter()
257 .cloned()
224 .cloned()
258 .partition(ConfigLayer::is_from_command_line);
225 .partition(ConfigLayer::is_from_command_line);
259
226
260 let mut repo_config = Self {
227 let mut repo_config = Self {
261 layers: other_layers,
228 layers: other_layers,
262 };
229 };
263 for path in repo_config_files {
230 for path in repo_config_files {
264 // TODO: check if this file should be trusted:
231 // TODO: check if this file should be trusted:
265 // `mercurial/ui.py:427`
232 // `mercurial/ui.py:427`
266 repo_config.add_trusted_file(path)?;
233 repo_config.add_trusted_file(path)?;
267 }
234 }
268 repo_config.layers.extend(cli_layers);
235 repo_config.layers.extend(cli_layers);
269 Ok(repo_config)
236 Ok(repo_config)
270 }
237 }
271
238
272 fn get_parse<'config, T: 'config>(
239 fn get_parse<'config, T: 'config>(
273 &'config self,
240 &'config self,
274 section: &[u8],
241 section: &[u8],
275 item: &[u8],
242 item: &[u8],
276 expected_type: &'static str,
243 expected_type: &'static str,
277 parse: impl Fn(&'config [u8]) -> Option<T>,
244 parse: impl Fn(&'config [u8]) -> Option<T>,
278 ) -> Result<Option<T>, ConfigValueParseError> {
245 ) -> Result<Option<T>, ConfigValueParseError> {
279 match self.get_inner(&section, &item) {
246 match self.get_inner(&section, &item) {
280 Some((layer, v)) => match parse(&v.bytes) {
247 Some((layer, v)) => match parse(&v.bytes) {
281 Some(b) => Ok(Some(b)),
248 Some(b) => Ok(Some(b)),
282 None => Err(ConfigValueParseError {
249 None => Err(ConfigValueParseError {
283 origin: layer.origin.to_owned(),
250 origin: layer.origin.to_owned(),
284 line: v.line,
251 line: v.line,
285 value: v.bytes.to_owned(),
252 value: v.bytes.to_owned(),
286 section: section.to_owned(),
253 section: section.to_owned(),
287 item: item.to_owned(),
254 item: item.to_owned(),
288 expected_type,
255 expected_type,
289 }),
256 }),
290 },
257 },
291 None => Ok(None),
258 None => Ok(None),
292 }
259 }
293 }
260 }
294
261
295 /// Returns an `Err` if the first value found is not a valid UTF-8 string.
262 /// Returns an `Err` if the first value found is not a valid UTF-8 string.
296 /// Otherwise, returns an `Ok(value)` if found, or `None`.
263 /// Otherwise, returns an `Ok(value)` if found, or `None`.
297 pub fn get_str(
264 pub fn get_str(
298 &self,
265 &self,
299 section: &[u8],
266 section: &[u8],
300 item: &[u8],
267 item: &[u8],
301 ) -> Result<Option<&str>, ConfigValueParseError> {
268 ) -> Result<Option<&str>, ConfigValueParseError> {
302 self.get_parse(section, item, "ASCII or UTF-8 string", |value| {
269 self.get_parse(section, item, "ASCII or UTF-8 string", |value| {
303 str::from_utf8(value).ok()
270 str::from_utf8(value).ok()
304 })
271 })
305 }
272 }
306
273
307 /// Returns an `Err` if the first value found is not a valid unsigned
274 /// Returns an `Err` if the first value found is not a valid unsigned
308 /// integer. Otherwise, returns an `Ok(value)` if found, or `None`.
275 /// integer. Otherwise, returns an `Ok(value)` if found, or `None`.
309 pub fn get_u32(
276 pub fn get_u32(
310 &self,
277 &self,
311 section: &[u8],
278 section: &[u8],
312 item: &[u8],
279 item: &[u8],
313 ) -> Result<Option<u32>, ConfigValueParseError> {
280 ) -> Result<Option<u32>, ConfigValueParseError> {
314 self.get_parse(section, item, "valid integer", |value| {
281 self.get_parse(section, item, "valid integer", |value| {
315 str::from_utf8(value).ok()?.parse().ok()
282 str::from_utf8(value).ok()?.parse().ok()
316 })
283 })
317 }
284 }
318
285
319 /// Returns an `Err` if the first value found is not a valid file size
286 /// Returns an `Err` if the first value found is not a valid file size
320 /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`.
287 /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`.
321 /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`.
288 /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`.
322 pub fn get_byte_size(
289 pub fn get_byte_size(
323 &self,
290 &self,
324 section: &[u8],
291 section: &[u8],
325 item: &[u8],
292 item: &[u8],
326 ) -> Result<Option<u64>, ConfigValueParseError> {
293 ) -> Result<Option<u64>, ConfigValueParseError> {
327 self.get_parse(section, item, "byte quantity", parse_byte_size)
294 self.get_parse(section, item, "byte quantity", values::parse_byte_size)
328 }
295 }
329
296
330 /// Returns an `Err` if the first value found is not a valid boolean.
297 /// Returns an `Err` if the first value found is not a valid boolean.
331 /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if
298 /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if
332 /// found, or `None`.
299 /// found, or `None`.
333 pub fn get_option(
300 pub fn get_option(
334 &self,
301 &self,
335 section: &[u8],
302 section: &[u8],
336 item: &[u8],
303 item: &[u8],
337 ) -> Result<Option<bool>, ConfigValueParseError> {
304 ) -> Result<Option<bool>, ConfigValueParseError> {
338 self.get_parse(section, item, "boolean", parse_bool)
305 self.get_parse(section, item, "boolean", values::parse_bool)
339 }
306 }
340
307
341 /// Returns the corresponding boolean in the config. Returns `Ok(false)`
308 /// Returns the corresponding boolean in the config. Returns `Ok(false)`
342 /// if the value is not found, an `Err` if it's not a valid boolean.
309 /// if the value is not found, an `Err` if it's not a valid boolean.
343 pub fn get_bool(
310 pub fn get_bool(
344 &self,
311 &self,
345 section: &[u8],
312 section: &[u8],
346 item: &[u8],
313 item: &[u8],
347 ) -> Result<bool, ConfigValueParseError> {
314 ) -> Result<bool, ConfigValueParseError> {
348 Ok(self.get_option(section, item)?.unwrap_or(false))
315 Ok(self.get_option(section, item)?.unwrap_or(false))
349 }
316 }
350
317
351 /// Returns the raw value bytes of the first one found, or `None`.
318 /// Returns the raw value bytes of the first one found, or `None`.
352 pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> {
319 pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> {
353 self.get_inner(section, item)
320 self.get_inner(section, item)
354 .map(|(_, value)| value.bytes.as_ref())
321 .map(|(_, value)| value.bytes.as_ref())
355 }
322 }
356
323
357 /// Returns the layer and the value of the first one found, or `None`.
324 /// Returns the layer and the value of the first one found, or `None`.
358 fn get_inner(
325 fn get_inner(
359 &self,
326 &self,
360 section: &[u8],
327 section: &[u8],
361 item: &[u8],
328 item: &[u8],
362 ) -> Option<(&ConfigLayer, &ConfigValue)> {
329 ) -> Option<(&ConfigLayer, &ConfigValue)> {
363 for layer in self.layers.iter().rev() {
330 for layer in self.layers.iter().rev() {
364 if !layer.trusted {
331 if !layer.trusted {
365 continue;
332 continue;
366 }
333 }
367 if let Some(v) = layer.get(&section, &item) {
334 if let Some(v) = layer.get(&section, &item) {
368 return Some((&layer, v));
335 return Some((&layer, v));
369 }
336 }
370 }
337 }
371 None
338 None
372 }
339 }
373
340
374 /// Get raw values bytes from all layers (even untrusted ones) in order
341 /// Get raw values bytes from all layers (even untrusted ones) in order
375 /// of precedence.
342 /// of precedence.
376 #[cfg(test)]
343 #[cfg(test)]
377 fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> {
344 fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> {
378 let mut res = vec![];
345 let mut res = vec![];
379 for layer in self.layers.iter().rev() {
346 for layer in self.layers.iter().rev() {
380 if let Some(v) = layer.get(&section, &item) {
347 if let Some(v) = layer.get(&section, &item) {
381 res.push(v.bytes.as_ref());
348 res.push(v.bytes.as_ref());
382 }
349 }
383 }
350 }
384 res
351 res
385 }
352 }
386 }
353 }
387
354
388 #[cfg(test)]
355 #[cfg(test)]
389 mod tests {
356 mod tests {
390 use super::*;
357 use super::*;
391 use pretty_assertions::assert_eq;
358 use pretty_assertions::assert_eq;
392 use std::fs::File;
359 use std::fs::File;
393 use std::io::Write;
360 use std::io::Write;
394
361
395 #[test]
362 #[test]
396 fn test_include_layer_ordering() {
363 fn test_include_layer_ordering() {
397 let tmpdir = tempfile::tempdir().unwrap();
364 let tmpdir = tempfile::tempdir().unwrap();
398 let tmpdir_path = tmpdir.path();
365 let tmpdir_path = tmpdir.path();
399 let mut included_file =
366 let mut included_file =
400 File::create(&tmpdir_path.join("included.rc")).unwrap();
367 File::create(&tmpdir_path.join("included.rc")).unwrap();
401
368
402 included_file.write_all(b"[section]\nitem=value1").unwrap();
369 included_file.write_all(b"[section]\nitem=value1").unwrap();
403 let base_config_path = tmpdir_path.join("base.rc");
370 let base_config_path = tmpdir_path.join("base.rc");
404 let mut config_file = File::create(&base_config_path).unwrap();
371 let mut config_file = File::create(&base_config_path).unwrap();
405 let data =
372 let data =
406 b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\
373 b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\
407 [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub";
374 [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub";
408 config_file.write_all(data).unwrap();
375 config_file.write_all(data).unwrap();
409
376
410 let sources = vec![ConfigSource::AbsPath(base_config_path)];
377 let sources = vec![ConfigSource::AbsPath(base_config_path)];
411 let config = Config::load_from_explicit_sources(sources)
378 let config = Config::load_from_explicit_sources(sources)
412 .expect("expected valid config");
379 .expect("expected valid config");
413
380
414 let (_, value) = config.get_inner(b"section", b"item").unwrap();
381 let (_, value) = config.get_inner(b"section", b"item").unwrap();
415 assert_eq!(
382 assert_eq!(
416 value,
383 value,
417 &ConfigValue {
384 &ConfigValue {
418 bytes: b"value2".to_vec(),
385 bytes: b"value2".to_vec(),
419 line: Some(4)
386 line: Some(4)
420 }
387 }
421 );
388 );
422
389
423 let value = config.get(b"section", b"item").unwrap();
390 let value = config.get(b"section", b"item").unwrap();
424 assert_eq!(value, b"value2",);
391 assert_eq!(value, b"value2",);
425 assert_eq!(
392 assert_eq!(
426 config.get_all(b"section", b"item"),
393 config.get_all(b"section", b"item"),
427 [b"value2", b"value1", b"value0"]
394 [b"value2", b"value1", b"value0"]
428 );
395 );
429
396
430 assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4));
397 assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4));
431 assert_eq!(
398 assert_eq!(
432 config.get_byte_size(b"section2", b"size").unwrap(),
399 config.get_byte_size(b"section2", b"size").unwrap(),
433 Some(1024 + 512)
400 Some(1024 + 512)
434 );
401 );
435 assert!(config.get_u32(b"section2", b"not-count").is_err());
402 assert!(config.get_u32(b"section2", b"not-count").is_err());
436 assert!(config.get_byte_size(b"section2", b"not-size").is_err());
403 assert!(config.get_byte_size(b"section2", b"not-size").is_err());
437 }
404 }
438 }
405 }
General Comments 0
You need to be logged in to leave comments. Login now