History

feat: `GitConfig::path()` for direct access to paths. (#301)

Very similar to `string()`, but as path, whose query can never fail.

Sebastian Thiel committed 4 years ago

Commit 7c75eac1 parent ed7f2232 files · +18 −2Browse files

Unified · Split

2 files
  1. git-config/src/file/git_config.rs +14 −0
  2. git-config/tests/value/mod.rs +4 −2
git-config/src/file/git_config.rs+14−0
@@ -466,6 +466,20 @@ impl<'event> GitConfig<'event> { · 465 unmodified lines
466466 .map(|v| values::String::from(v).value)
467467 }
468468
469+ /// Like [`value()`][GitConfig::value()], but returning an `Option` if the paty wasn't found.
470+ ///
471+ /// As strings perform no conversions, this will never fail.
472+ pub fn path(
473+ &'event self,
474+ section_name: &str,
475+ subsection_name: Option<&str>,
476+ key: &str,
477+ ) -> Option<values::Path<'event>> {
478+ self.raw_value(section_name, subsection_name, key)
479+ .ok()
480+ .map(|v| values::Path::from(v))
481+ }
482+
469483 /// Like [`value()`][GitConfig::value()], but returning an `Option` if the boolean wasn't found.
470484 pub fn boolean(
471485 &'event self,
git-config/tests/value/mod.rs+4−2
@@ -85,13 +85,15 @@ fn get_value_for_all_provided_values() -> crate::Result { · 84 unmodified lines
8585
8686 let actual = file.value::<git_config::values::Path>("core", None, "location")?;
8787 assert_eq!(
88− &*actual,
89− "~/tmp".as_bytes(),
88+ &*actual, "~/tmp",
9089 "no interpolation occurs when querying a path due to lack of context"
9190 );
9291 let expected = PathBuf::from(format!("{}/tmp", dirs::home_dir().expect("empty home dir").display()));
9392 assert_eq!(actual.interpolate(None).unwrap(), expected);
9493
94+ let actual = file.path("core", None, "location").expect("present");
95+ assert_eq!(&*actual, "~/tmp",);
96+
9597 Ok(())
9698 }
9799