Show More
@@ -360,21 +360,21 b' impl Config {' | |||
|
360 | 360 | self.plain = plain; |
|
361 | 361 | } |
|
362 | 362 | |
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
|
|
|
377 |
|
|
|
363 | /// Returns the default value for the given config item, if any. | |
|
364 | pub fn get_default( | |
|
365 | &self, | |
|
366 | section: &[u8], | |
|
367 | item: &[u8], | |
|
368 | ) -> Result<Option<&DefaultConfigItem>, HgError> { | |
|
369 | let default_config = DEFAULT_CONFIG.as_ref().map_err(|e| { | |
|
370 | HgError::abort( | |
|
371 | e.to_string(), | |
|
372 | crate::exit_codes::ABORT, | |
|
373 | Some("`mercurial/configitems.toml` is not valid".into()), | |
|
374 | ) | |
|
375 | })?; | |
|
376 | Ok(default_config.get(section, item)) | |
|
377 | } | |
|
378 | 378 | |
|
379 | 379 | fn get_parse<'config, T: 'config>( |
|
380 | 380 | &'config self, |
@@ -382,6 +382,7 b' impl Config {' | |||
|
382 | 382 | item: &[u8], |
|
383 | 383 | expected_type: &'static str, |
|
384 | 384 | parse: impl Fn(&'config [u8]) -> Option<T>, |
|
385 | fallback_to_default: bool, | |
|
385 | 386 | ) -> Result<Option<T>, HgError> |
|
386 | 387 | where |
|
387 | 388 | Option<T>: TryFrom<&'config DefaultConfigItem, Error = HgError>, |
@@ -399,12 +400,15 b' impl Config {' | |||
|
399 | 400 | }) |
|
400 | 401 | .into()), |
|
401 | 402 | }, |
|
402 | None => match self.get_default(section, item)? { | |
|
403 | Some(default) => Ok(default.try_into()?), | |
|
404 |
None |
|
|
405 | Ok(None) | |
|
403 | None => { | |
|
404 | if !fallback_to_default { | |
|
405 | return Ok(None); | |
|
406 | 406 | } |
|
407 | }, | |
|
407 | match self.get_default(section, item)? { | |
|
408 | Some(default) => Ok(default.try_into()?), | |
|
409 | None => Ok(None), | |
|
410 | } | |
|
411 | } | |
|
408 | 412 | } |
|
409 | 413 | } |
|
410 | 414 | |
@@ -415,9 +419,29 b' impl Config {' | |||
|
415 | 419 | section: &[u8], |
|
416 | 420 | item: &[u8], |
|
417 | 421 | ) -> Result<Option<&str>, HgError> { |
|
418 | self.get_parse(section, item, "ASCII or UTF-8 string", |value| { | |
|
419 | str::from_utf8(value).ok() | |
|
420 | }) | |
|
422 | self.get_parse( | |
|
423 | section, | |
|
424 | item, | |
|
425 | "ASCII or UTF-8 string", | |
|
426 | |value| str::from_utf8(value).ok(), | |
|
427 | true, | |
|
428 | ) | |
|
429 | } | |
|
430 | ||
|
431 | /// Same as `get_str`, but doesn't fall back to the default `configitem` | |
|
432 | /// if not defined in the user config. | |
|
433 | pub fn get_str_no_default( | |
|
434 | &self, | |
|
435 | section: &[u8], | |
|
436 | item: &[u8], | |
|
437 | ) -> Result<Option<&str>, HgError> { | |
|
438 | self.get_parse( | |
|
439 | section, | |
|
440 | item, | |
|
441 | "ASCII or UTF-8 string", | |
|
442 | |value| str::from_utf8(value).ok(), | |
|
443 | false, | |
|
444 | ) | |
|
421 | 445 | } |
|
422 | 446 | |
|
423 | 447 | /// Returns an `Err` if the first value found is not a valid unsigned |
@@ -427,9 +451,13 b' impl Config {' | |||
|
427 | 451 | section: &[u8], |
|
428 | 452 | item: &[u8], |
|
429 | 453 | ) -> Result<Option<u32>, HgError> { |
|
430 | self.get_parse(section, item, "valid integer", |value| { | |
|
431 | str::from_utf8(value).ok()?.parse().ok() | |
|
432 | }) | |
|
454 | self.get_parse( | |
|
455 | section, | |
|
456 | item, | |
|
457 | "valid integer", | |
|
458 | |value| str::from_utf8(value).ok()?.parse().ok(), | |
|
459 | true, | |
|
460 | ) | |
|
433 | 461 | } |
|
434 | 462 | |
|
435 | 463 | /// Returns an `Err` if the first value found is not a valid file size |
@@ -440,7 +468,13 b' impl Config {' | |||
|
440 | 468 | section: &[u8], |
|
441 | 469 | item: &[u8], |
|
442 | 470 | ) -> Result<Option<u64>, HgError> { |
|
443 | self.get_parse(section, item, "byte quantity", values::parse_byte_size) | |
|
471 | self.get_parse( | |
|
472 | section, | |
|
473 | item, | |
|
474 | "byte quantity", | |
|
475 | values::parse_byte_size, | |
|
476 | true, | |
|
477 | ) | |
|
444 | 478 | } |
|
445 | 479 | |
|
446 | 480 | /// Returns an `Err` if the first value found is not a valid boolean. |
@@ -451,7 +485,17 b' impl Config {' | |||
|
451 | 485 | section: &[u8], |
|
452 | 486 | item: &[u8], |
|
453 | 487 | ) -> Result<Option<bool>, HgError> { |
|
454 | self.get_parse(section, item, "boolean", values::parse_bool) | |
|
488 | self.get_parse(section, item, "boolean", values::parse_bool, true) | |
|
489 | } | |
|
490 | ||
|
491 | /// Same as `get_option`, but doesn't fall back to the default `configitem` | |
|
492 | /// if not defined in the user config. | |
|
493 | pub fn get_option_no_default( | |
|
494 | &self, | |
|
495 | section: &[u8], | |
|
496 | item: &[u8], | |
|
497 | ) -> Result<Option<bool>, HgError> { | |
|
498 | self.get_parse(section, item, "boolean", values::parse_bool, false) | |
|
455 | 499 | } |
|
456 | 500 | |
|
457 | 501 | /// Returns the corresponding boolean in the config. Returns `Ok(false)` |
@@ -464,6 +508,16 b' impl Config {' | |||
|
464 | 508 | Ok(self.get_option(section, item)?.unwrap_or(false)) |
|
465 | 509 | } |
|
466 | 510 | |
|
511 | /// Same as `get_bool`, but doesn't fall back to the default `configitem` | |
|
512 | /// if not defined in the user config. | |
|
513 | pub fn get_bool_no_default( | |
|
514 | &self, | |
|
515 | section: &[u8], | |
|
516 | item: &[u8], | |
|
517 | ) -> Result<bool, HgError> { | |
|
518 | Ok(self.get_option_no_default(section, item)?.unwrap_or(false)) | |
|
519 | } | |
|
520 | ||
|
467 | 521 | /// Returns `true` if the extension is enabled, `false` otherwise |
|
468 | 522 | pub fn is_extension_enabled(&self, extension: &[u8]) -> bool { |
|
469 | 523 | let value = self.get(b"extensions", extension); |
@@ -86,7 +86,8 b' fn main_with_result(' | |||
|
86 | 86 | // Mercurial allows users to define generic hooks for commands, |
|
87 | 87 | // fallback if any are detected |
|
88 | 88 | let item = format!("{}-{}", prefix, subcommand_name); |
|
89 | let hook_for_command = config.get_str(b"hooks", item.as_bytes())?; | |
|
89 | let hook_for_command = | |
|
90 | config.get_str_no_default(b"hooks", item.as_bytes())?; | |
|
90 | 91 | if hook_for_command.is_some() { |
|
91 | 92 | let msg = format!("{}-{} hook defined", prefix, subcommand_name); |
|
92 | 93 | return Err(CommandError::unsupported(msg)); |
General Comments 0
You need to be logged in to leave comments.
Login now