Skip to content

Commit 6281e1a

Browse files
committed
gix-config now uses a Key trait rather than Into<&BStr>
1 parent 35592c9 commit 6281e1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+636
-440
lines changed

gitoxide-core/src/organize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn find_origin_remote(repo: &Path) -> anyhow::Result<Option<gix_url::Url>> {
9696
let config = gix::config::File::from_path_no_includes(non_bare.as_path().into(), local)
9797
.or_else(|_| gix::config::File::from_path_no_includes(repo.join("config"), local))?;
9898
Ok(config
99-
.string_by_key("remote.origin.url")
99+
.string("remote.origin.url")
100100
.map(|url| gix_url::Url::from_bytes(url.as_ref()))
101101
.transpose()?)
102102
}

gix-config/src/file/access/comfort.rs

+65-72
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ use std::{borrow::Cow, convert::TryFrom};
22

33
use bstr::BStr;
44

5-
use crate::{file::MetadataFilter, value, File};
5+
use crate::{file::MetadataFilter, value, File, Key};
66

77
/// Comfortable API for accessing values
88
impl<'event> File<'event> {
99
/// Like [`value()`][File::value()], but returning `None` if the string wasn't found.
1010
///
1111
/// As strings perform no conversions, this will never fail.
12-
pub fn string(
12+
pub fn string_by(
1313
&self,
1414
section_name: impl AsRef<str>,
1515
subsection_name: Option<&BStr>,
1616
key: impl AsRef<str>,
1717
) -> Option<Cow<'_, BStr>> {
18-
self.string_filter(section_name, subsection_name, key, &mut |_| true)
18+
self.string_filter_by(section_name, subsection_name, key, &mut |_| true)
1919
}
2020

21-
/// Like [`string()`][File::string()], but suitable for statically known `key`s like `remote.origin.url`.
22-
pub fn string_by_key<'a>(&self, key: impl Into<&'a BStr>) -> Option<Cow<'_, BStr>> {
23-
self.string_filter_by_key(key, &mut |_| true)
21+
/// Like [`string()`][File::string_by()], but suitable for statically known `key`s like `remote.origin.url`.
22+
pub fn string(&self, key: impl Key) -> Option<Cow<'_, BStr>> {
23+
self.string_filter(key, &mut |_| true)
2424
}
2525

2626
/// Like [`string()`][File::string()], but the section containing the returned value must pass `filter` as well.
27-
pub fn string_filter(
27+
pub fn string_filter_by(
2828
&self,
2929
section_name: impl AsRef<str>,
3030
subsection_name: Option<&BStr>,
@@ -35,14 +35,13 @@ impl<'event> File<'event> {
3535
.ok()
3636
}
3737

38-
/// Like [`string_filter()`][File::string_filter()], but suitable for statically known `key`s like `remote.origin.url`.
39-
pub fn string_filter_by_key<'a>(
38+
/// Like [`string_filter()`][File::string_filter_by()], but suitable for statically known `key`s like `remote.origin.url`.
39+
pub fn string_filter(
4040
&self,
41-
key: impl Into<&'a BStr>,
41+
key: impl Key,
4242
filter: &mut MetadataFilter,
4343
) -> Option<Cow<'_, BStr>> {
44-
let key = crate::parse::key(key.into())?;
45-
self.raw_value_filter(key.section_name, key.subsection_name, key.value_name, filter)
44+
self.raw_value_filter(key.section_name(), key.subsection_name(), key.name(), filter)
4645
.ok()
4746
}
4847

@@ -52,18 +51,18 @@ impl<'event> File<'event> {
5251
/// to pose a security risk. Prefer using [`path_filter()`][File::path_filter()] instead.
5352
///
5453
/// As paths perform no conversions, this will never fail.
55-
pub fn path(
54+
pub fn path_by(
5655
&self,
5756
section_name: impl AsRef<str>,
5857
subsection_name: Option<&BStr>,
5958
key: impl AsRef<str>,
6059
) -> Option<crate::Path<'_>> {
61-
self.path_filter(section_name, subsection_name, key, &mut |_| true)
60+
self.path_filter_by(section_name, subsection_name, key, &mut |_| true)
6261
}
6362

64-
/// Like [`path()`][File::path()], but suitable for statically known `key`s like `remote.origin.url`.
65-
pub fn path_by_key<'a>(&self, key: impl Into<&'a BStr>) -> Option<crate::Path<'_>> {
66-
self.path_filter_by_key(key, &mut |_| true)
63+
/// Like [`path()`][File::path_by()], but suitable for statically known `key`s like `remote.origin.url`.
64+
pub fn path(&self, key: impl Key) -> Option<crate::Path<'_>> {
65+
self.path_filter(key, &mut |_| true)
6766
}
6867

6968
/// Like [`path()`][File::path()], but the section containing the returned value must pass `filter` as well.
@@ -72,7 +71,7 @@ impl<'event> File<'event> {
7271
/// locations can be
7372
///
7473
/// As paths perform no conversions, this will never fail.
75-
pub fn path_filter(
74+
pub fn path_filter_by(
7675
&self,
7776
section_name: impl AsRef<str>,
7877
subsection_name: Option<&BStr>,
@@ -84,33 +83,32 @@ impl<'event> File<'event> {
8483
.map(crate::Path::from)
8584
}
8685

87-
/// Like [`path_filter()`][File::path_filter()], but suitable for statically known `key`s like `remote.origin.url`.
88-
pub fn path_filter_by_key<'a>(
86+
/// Like [`path_filter()`][File::path_filter_by()], but suitable for statically known `key`s like `remote.origin.url`.
87+
pub fn path_filter(
8988
&self,
90-
key: impl Into<&'a BStr>,
89+
key: impl Key,
9190
filter: &mut MetadataFilter,
9291
) -> Option<crate::Path<'_>> {
93-
let key = crate::parse::key(key.into())?;
94-
self.path_filter(key.section_name, key.subsection_name, key.value_name, filter)
92+
self.path_filter_by(key.section_name(), key.subsection_name(), key.name(), filter)
9593
}
9694

9795
/// Like [`value()`][File::value()], but returning `None` if the boolean value wasn't found.
98-
pub fn boolean(
96+
pub fn boolean_by(
9997
&self,
10098
section_name: impl AsRef<str>,
10199
subsection_name: Option<&BStr>,
102100
key: impl AsRef<str>,
103101
) -> Option<Result<bool, value::Error>> {
104-
self.boolean_filter(section_name, subsection_name, key, &mut |_| true)
102+
self.boolean_filter_by(section_name, subsection_name, key, &mut |_| true)
105103
}
106104

107-
/// Like [`boolean()`][File::boolean()], but suitable for statically known `key`s like `remote.origin.url`.
108-
pub fn boolean_by_key<'a>(&self, key: impl Into<&'a BStr>) -> Option<Result<bool, value::Error>> {
109-
self.boolean_filter_by_key(key, &mut |_| true)
105+
/// Like [`boolean()`][File::boolean_by()], but suitable for statically known `key`s like `remote.origin.url`.
106+
pub fn boolean(&self, key: impl Key) -> Option<Result<bool, value::Error>> {
107+
self.boolean_filter(key, &mut |_| true)
110108
}
111109

112-
/// Like [`boolean()`][File::boolean()], but the section containing the returned value must pass `filter` as well.
113-
pub fn boolean_filter(
110+
/// Like [`boolean()`][File::boolean_by()], but the section containing the returned value must pass `filter` as well.
111+
pub fn boolean_filter_by(
114112
&self,
115113
section_name: impl AsRef<str>,
116114
subsection_name: Option<&BStr>,
@@ -136,33 +134,32 @@ impl<'event> File<'event> {
136134
None
137135
}
138136

139-
/// Like [`boolean_filter()`][File::boolean_filter()], but suitable for statically known `key`s like `remote.origin.url`.
140-
pub fn boolean_filter_by_key<'a>(
137+
/// Like [`boolean_filter()`][File::boolean_filter_by()], but suitable for statically known `key`s like `remote.origin.url`.
138+
pub fn boolean_filter(
141139
&self,
142-
key: impl Into<&'a BStr>,
140+
key: impl Key,
143141
filter: &mut MetadataFilter,
144142
) -> Option<Result<bool, value::Error>> {
145-
let key = crate::parse::key(key.into())?;
146-
self.boolean_filter(key.section_name, key.subsection_name, key.value_name, filter)
143+
self.boolean_filter_by(key.section_name(), key.subsection_name(), key.name(), filter)
147144
}
148145

149146
/// Like [`value()`][File::value()], but returning an `Option` if the integer wasn't found.
150-
pub fn integer(
147+
pub fn integer_by(
151148
&self,
152149
section_name: impl AsRef<str>,
153150
subsection_name: Option<&BStr>,
154151
key: impl AsRef<str>,
155152
) -> Option<Result<i64, value::Error>> {
156-
self.integer_filter(section_name, subsection_name, key, &mut |_| true)
153+
self.integer_filter_by(section_name, subsection_name, key, &mut |_| true)
157154
}
158155

159-
/// Like [`integer()`][File::integer()], but suitable for statically known `key`s like `remote.origin.url`.
160-
pub fn integer_by_key<'a>(&self, key: impl Into<&'a BStr>) -> Option<Result<i64, value::Error>> {
161-
self.integer_filter_by_key(key, &mut |_| true)
156+
/// Like [`integer()`][File::integer_by()], but suitable for statically known `key`s like `remote.origin.url`.
157+
pub fn integer(&self, key: impl Key) -> Option<Result<i64, value::Error>> {
158+
self.integer_filter(key, &mut |_| true)
162159
}
163160

164-
/// Like [`integer()`][File::integer()], but the section containing the returned value must pass `filter` as well.
165-
pub fn integer_filter(
161+
/// Like [`integer()`][File::integer_by()], but the section containing the returned value must pass `filter` as well.
162+
pub fn integer_filter_by(
166163
&self,
167164
section_name: impl AsRef<str>,
168165
subsection_name: Option<&BStr>,
@@ -178,35 +175,33 @@ impl<'event> File<'event> {
178175
}))
179176
}
180177

181-
/// Like [`integer_filter()`][File::integer_filter()], but suitable for statically known `key`s like `remote.origin.url`.
182-
pub fn integer_filter_by_key<'a>(
178+
/// Like [`integer_filter()`][File::integer_filter_by()], but suitable for statically known `key`s like `remote.origin.url`.
179+
pub fn integer_filter(
183180
&self,
184-
key: impl Into<&'a BStr>,
181+
key: impl Key,
185182
filter: &mut MetadataFilter,
186183
) -> Option<Result<i64, value::Error>> {
187-
let key = crate::parse::key(key.into())?;
188-
self.integer_filter(key.section_name, key.subsection_name, key.value_name, filter)
184+
self.integer_filter_by(key.section_name(), key.subsection_name(), key.name(), filter)
189185
}
190186

191187
/// Similar to [`values(…)`][File::values()] but returning strings if at least one of them was found.
192-
pub fn strings(
188+
pub fn strings_by(
193189
&self,
194190
section_name: impl AsRef<str>,
195191
subsection_name: Option<&BStr>,
196192
key: impl AsRef<str>,
197193
) -> Option<Vec<Cow<'_, BStr>>> {
198-
self.raw_values(section_name.as_ref(), subsection_name, key.as_ref())
194+
self.raw_values_by(section_name.as_ref(), subsection_name, key.as_ref())
199195
.ok()
200196
}
201197

202-
/// Like [`strings()`][File::strings()], but suitable for statically known `key`s like `remote.origin.url`.
203-
pub fn strings_by_key<'a>(&self, key: impl Into<&'a BStr>) -> Option<Vec<Cow<'_, BStr>>> {
204-
let key = crate::parse::key(key.into())?;
205-
self.strings(key.section_name, key.subsection_name, key.value_name)
198+
/// Like [`strings()`][File::strings_by()], but suitable for statically known `key`s like `remote.origin.url`.
199+
pub fn strings(&self, key: impl Key) -> Option<Vec<Cow<'_, BStr>>> {
200+
self.strings_by(key.section_name(), key.subsection_name(), key.name())
206201
}
207202

208-
/// Similar to [`strings(…)`][File::strings()], but all values are in sections that passed `filter`.
209-
pub fn strings_filter(
203+
/// Similar to [`strings(…)`][File::strings_by()], but all values are in sections that passed `filter`.
204+
pub fn strings_filter_by(
210205
&self,
211206
section_name: impl AsRef<str>,
212207
subsection_name: Option<&BStr>,
@@ -217,35 +212,34 @@ impl<'event> File<'event> {
217212
.ok()
218213
}
219214

220-
/// Like [`strings_filter()`][File::strings_filter()], but suitable for statically known `key`s like `remote.origin.url`.
221-
pub fn strings_filter_by_key<'a>(
215+
/// Like [`strings_filter()`][File::strings_filter_by()], but suitable for statically known `key`s like `remote.origin.url`.
216+
pub fn strings_filter(
222217
&self,
223-
key: impl Into<&'a BStr>,
218+
key: impl Key,
224219
filter: &mut MetadataFilter,
225220
) -> Option<Vec<Cow<'_, BStr>>> {
226-
let key = crate::parse::key(key.into())?;
227-
self.strings_filter(key.section_name, key.subsection_name, key.value_name, filter)
221+
self.strings_filter_by(key.section_name(), key.subsection_name(), key.name(), filter)
228222
}
229223

230224
/// Similar to [`values(…)`][File::values()] but returning integers if at least one of them was found
231225
/// and if none of them overflows.
232-
pub fn integers(
226+
pub fn integers_by(
233227
&self,
234228
section_name: impl AsRef<str>,
235229
subsection_name: Option<&BStr>,
236230
key: impl AsRef<str>,
237231
) -> Option<Result<Vec<i64>, value::Error>> {
238-
self.integers_filter(section_name, subsection_name, key, &mut |_| true)
232+
self.integers_filter_by(section_name, subsection_name, key, &mut |_| true)
239233
}
240234

241-
/// Like [`integers()`][File::integers()], but suitable for statically known `key`s like `remote.origin.url`.
242-
pub fn integers_by_key<'a>(&self, key: impl Into<&'a BStr>) -> Option<Result<Vec<i64>, value::Error>> {
243-
self.integers_filter_by_key(key, &mut |_| true)
235+
/// Like [`integers()`][File::integers_by()], but suitable for statically known `key`s like `remote.origin.url`.
236+
pub fn integers(&self, key: impl Key) -> Option<Result<Vec<i64>, value::Error>> {
237+
self.integers_filter(key, &mut |_| true)
244238
}
245239

246-
/// Similar to [`integers(…)`][File::integers()] but all integers are in sections that passed `filter`
240+
/// Similar to [`integers(…)`][File::integers_by()] but all integers are in sections that passed `filter`
247241
/// and that are not overflowing.
248-
pub fn integers_filter(
242+
pub fn integers_filter_by(
249243
&self,
250244
section_name: impl AsRef<str>,
251245
subsection_name: Option<&BStr>,
@@ -267,13 +261,12 @@ impl<'event> File<'event> {
267261
})
268262
}
269263

270-
/// Like [`integers_filter()`][File::integers_filter()], but suitable for statically known `key`s like `remote.origin.url`.
271-
pub fn integers_filter_by_key<'a>(
264+
/// Like [`integers_filter()`][File::integers_filter_by()], but suitable for statically known `key`s like `remote.origin.url`.
265+
pub fn integers_filter(
272266
&self,
273-
key: impl Into<&'a BStr>,
267+
key: impl Key,
274268
filter: &mut MetadataFilter,
275269
) -> Option<Result<Vec<i64>, value::Error>> {
276-
let key = crate::parse::key(key.into())?;
277-
self.integers_filter(key.section_name, key.subsection_name, key.value_name, filter)
270+
self.integers_filter_by(key.section_name(), key.subsection_name(), key.name(), filter)
278271
}
279272
}

gix-config/src/file/access/raw.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ use crate::{
1414
///
1515
/// These functions are the raw value API, returning normalized byte strings.
1616
impl<'event> File<'event> {
17+
/// TODO
18+
pub fn raw_value(
19+
&self,
20+
key: impl Key,
21+
) -> Result<Cow<'_, BStr>, lookup::existing::Error> {
22+
self.raw_value_by(key.section_name(), key.subsection_name(), key.name())
23+
}
24+
1725
/// Returns an uninterpreted value given a section, an optional subsection
1826
/// and key.
1927
///
2028
/// Consider [`Self::raw_values()`] if you want to get all values of
2129
/// a multivar instead.
22-
pub fn raw_value(
30+
pub fn raw_value_by(
2331
&self,
2432
section_name: impl AsRef<str>,
2533
subsection_name: Option<&BStr>,
@@ -151,6 +159,14 @@ impl<'event> File<'event> {
151159
Err(lookup::existing::Error::KeyMissing)
152160
}
153161

162+
/// TODO
163+
pub fn raw_values(
164+
&self,
165+
key: impl Key,
166+
) -> Result<Vec<Cow<'_, BStr>>, lookup::existing::Error> {
167+
self.raw_values_by(key.section_name(), key.subsection_name(), key.name())
168+
}
169+
154170
/// Returns all uninterpreted values given a section, an optional subsection
155171
/// ain order of occurrence.
156172
///
@@ -189,7 +205,7 @@ impl<'event> File<'event> {
189205
///
190206
/// Consider [`Self::raw_value`] if you want to get the resolved single
191207
/// value for a given key, if your key does not support multi-valued values.
192-
pub fn raw_values(
208+
pub fn raw_values_by(
193209
&self,
194210
section_name: impl AsRef<str>,
195211
subsection_name: Option<&BStr>,
@@ -237,6 +253,14 @@ impl<'event> File<'event> {
237253
}
238254
}
239255

256+
/// TODO
257+
pub fn raw_values_mut(
258+
&mut self,
259+
key: &'event dyn Key,
260+
) -> Result<MultiValueMut<'_, '_, 'event>, lookup::existing::Error> {
261+
self.raw_values_mut_by(key.section_name(), key.subsection_name(), key.name())
262+
}
263+
240264
/// Returns mutable references to all uninterpreted values given a section,
241265
/// an optional subsection and key.
242266
///
@@ -287,7 +311,7 @@ impl<'event> File<'event> {
287311
///
288312
/// Note that this operation is relatively expensive, requiring a full
289313
/// traversal of the config.
290-
pub fn raw_values_mut<'lookup>(
314+
pub fn raw_values_mut_by<'lookup>(
291315
&mut self,
292316
section_name: impl AsRef<str>,
293317
subsection_name: Option<&'lookup BStr>,
@@ -568,7 +592,7 @@ impl<'event> File<'event> {
568592
Iter: IntoIterator<Item = Item>,
569593
Item: Into<&'a BStr>,
570594
{
571-
self.raw_values_mut(section_name, subsection_name, key.as_ref())
595+
self.raw_values_mut_by(section_name, subsection_name, key.as_ref())
572596
.map(|mut v| v.set_values(new_values))
573597
}
574598
}

0 commit comments

Comments
 (0)