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