Skip to content

Commit 0a7391a

Browse files
committed
change!: conform APIs of file::MutableValue and file::MutableMultiValue. (#331)
There are more renames and removals than worth mentioning here given the current adoption of the crate.
1 parent 048b925 commit 0a7391a

File tree

3 files changed

+36
-66
lines changed

3 files changed

+36
-66
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'event> File<'event> {
185185
/// ]
186186
/// );
187187
///
188-
/// git_config.raw_values_mut("core", None, "a")?.set_str_all("g");
188+
/// git_config.raw_values_mut("core", None, "a")?.set_all("g");
189189
///
190190
/// assert_eq!(
191191
/// git_config.raw_values("core", None, "a")?,
@@ -306,7 +306,7 @@ impl<'event> File<'event> {
306306
new_value: &BStr,
307307
) -> Result<(), lookup::existing::Error> {
308308
self.raw_value_mut(section_name, subsection_name, key)
309-
.map(|mut entry| entry.set_bytes(new_value))
309+
.map(|mut entry| entry.set(new_value))
310310
}
311311

312312
/// Sets a multivar in a given section, optional subsection, and key value.
@@ -344,9 +344,9 @@ impl<'event> File<'event> {
344344
/// # use bstr::BStr;
345345
/// # let mut git_config = git_config::File::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
346346
/// let new_values = vec![
347-
/// Cow::<BStr>::Borrowed("x".into()),
348-
/// Cow::<BStr>::Borrowed("y".into()),
349-
/// Cow::<BStr>::Borrowed("z".into()),
347+
/// "x".into(),
348+
/// "y".into(),
349+
/// "z".into(),
350350
/// ];
351351
/// git_config.set_raw_multi_value("core", None, "a", new_values.into_iter())?;
352352
/// let fetched_config = git_config.raw_values("core", None, "a")?;
@@ -365,8 +365,8 @@ impl<'event> File<'event> {
365365
/// # use bstr::BStr;
366366
/// # let mut git_config = git_config::File::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
367367
/// let new_values = vec![
368-
/// Cow::<BStr>::Borrowed("x".into()),
369-
/// Cow::<BStr>::Borrowed("y".into()),
368+
/// "x".into(),
369+
/// "y".into(),
370370
/// ];
371371
/// git_config.set_raw_multi_value("core", None, "a", new_values.into_iter())?;
372372
/// let fetched_config = git_config.raw_values("core", None, "a")?;
@@ -384,21 +384,21 @@ impl<'event> File<'event> {
384384
/// # use bstr::BStr;
385385
/// # let mut git_config = git_config::File::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
386386
/// let new_values = vec![
387-
/// Cow::<BStr>::Borrowed("x".into()),
388-
/// Cow::<BStr>::Borrowed("y".into()),
389-
/// Cow::<BStr>::Borrowed("z".into()),
390-
/// Cow::<BStr>::Borrowed("discarded".into()),
387+
/// "x".into(),
388+
/// "y".into(),
389+
/// "z".into(),
390+
/// "discarded".into(),
391391
/// ];
392392
/// git_config.set_raw_multi_value("core", None, "a", new_values)?;
393393
/// assert!(!git_config.raw_values("core", None, "a")?.contains(&Cow::<BStr>::Borrowed("discarded".into())));
394394
/// # Ok::<(), git_config::lookup::existing::Error>(())
395395
/// ```
396-
pub fn set_raw_multi_value(
396+
pub fn set_raw_multi_value<'a>(
397397
&mut self,
398398
section_name: &str,
399399
subsection_name: Option<&str>,
400400
key: &str,
401-
new_values: impl IntoIterator<Item = Cow<'event, BStr>>,
401+
new_values: impl IntoIterator<Item = &'a BStr>,
402402
) -> Result<(), lookup::existing::Error> {
403403
self.raw_values_mut(section_name, subsection_name, key)
404404
.map(|mut v| v.set_values(new_values))

git-config/src/file/mutable/value.rs

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ impl<'borrow, 'lookup, 'event> MutableValue<'borrow, 'lookup, 'event> {
3131
/// the Value event(s) are replaced with a single new event containing the
3232
/// new value.
3333
pub fn set_string(&mut self, input: impl AsRef<str>) {
34-
self.set_bytes(input.as_ref().into());
34+
self.set(input.as_ref().into());
3535
}
3636

3737
/// Update the value to the provided one. This modifies the value such that
3838
/// the Value event(s) are replaced with a single new event containing the
3939
/// new value.
40-
pub fn set_bytes(&mut self, input: &BStr) {
40+
pub fn set(&mut self, input: &BStr) {
4141
if self.size.0 > 0 {
4242
self.section.delete(self.index, self.index + self.size);
4343
}
@@ -132,25 +132,16 @@ impl<'borrow, 'lookup, 'event> MutableMultiValue<'borrow, 'lookup, 'event> {
132132
/// # Safety
133133
///
134134
/// This will panic if the index is out of range.
135-
pub fn set_string(&mut self, index: usize, input: String) {
136-
self.set_bytes(index, input);
135+
pub fn set_string_at(&mut self, index: usize, input: impl AsRef<str>) {
136+
self.set_at(index, input.as_ref().into());
137137
}
138138

139139
/// Sets the value at the given index.
140140
///
141141
/// # Safety
142142
///
143143
/// This will panic if the index is out of range.
144-
pub fn set_bytes(&mut self, index: usize, input: impl Into<BString>) {
145-
self.set_value(index, Cow::Owned(input.into()));
146-
}
147-
148-
/// Sets the value at the given index.
149-
///
150-
/// # Safety
151-
///
152-
/// This will panic if the index is out of range.
153-
pub fn set_value(&mut self, index: usize, input: Cow<'event, BStr>) {
144+
pub fn set_at(&mut self, index: usize, input: &BStr) {
154145
let EntryData {
155146
section_id,
156147
offset_index,
@@ -172,7 +163,7 @@ impl<'borrow, 'lookup, 'event> MutableMultiValue<'borrow, 'lookup, 'event> {
172163
/// remaining values are ignored.
173164
///
174165
/// [`zip`]: std::iter::Iterator::zip
175-
pub fn set_values(&mut self, input: impl IntoIterator<Item = Cow<'event, BStr>>) {
166+
pub fn set_values<'a>(&mut self, input: impl IntoIterator<Item = &'a BStr>) {
176167
for (
177168
EntryData {
178169
section_id,
@@ -192,39 +183,14 @@ impl<'borrow, 'lookup, 'event> MutableMultiValue<'borrow, 'lookup, 'event> {
192183
}
193184
}
194185

195-
/// Sets all values in this multivar to the provided one by copying the
196-
/// `input` string to all values.
197-
pub fn set_str_all(&mut self, input: &str) {
198-
self.set_owned_values_all(input);
199-
}
200-
201-
/// Sets all values in this multivar to the provided one by copying the
202-
/// `input` bytes to all values.
203-
pub fn set_owned_values_all<'a>(&mut self, input: impl Into<&'a BStr>) {
204-
let input = input.into();
205-
for EntryData {
206-
section_id,
207-
offset_index,
208-
} in &self.indices_and_sizes
209-
{
210-
Self::set_value_inner(
211-
&self.key,
212-
&mut self.offsets,
213-
self.section.get_mut(section_id).expect("known section id"),
214-
*section_id,
215-
*offset_index,
216-
Cow::Owned(input.to_owned()),
217-
);
218-
}
219-
}
220-
221186
/// Sets all values in this multivar to the provided one without owning the
222187
/// provided input. Consider using [`Self::set_owned_values_all`] or
223188
/// [`Self::set_str_all`] unless you have a strict performance or memory
224189
/// need for a more ergonomic interface.
225190
///
226191
/// [`File`]: crate::File
227-
pub fn set_values_all(&mut self, input: &'event BStr) {
192+
pub fn set_all<'a>(&mut self, input: impl Into<&'a BStr>) {
193+
let input = input.into();
228194
for EntryData {
229195
section_id,
230196
offset_index,
@@ -236,7 +202,7 @@ impl<'borrow, 'lookup, 'event> MutableMultiValue<'borrow, 'lookup, 'event> {
236202
self.section.get_mut(section_id).expect("known section id"),
237203
*section_id,
238204
*offset_index,
239-
Cow::Borrowed(input),
205+
input,
240206
);
241207
}
242208
}
@@ -247,7 +213,7 @@ impl<'borrow, 'lookup, 'event> MutableMultiValue<'borrow, 'lookup, 'event> {
247213
section: &mut SectionBody<'event>,
248214
section_id: SectionBodyId,
249215
offset_index: usize,
250-
value: Cow<'a, BStr>,
216+
value: &BStr,
251217
) {
252218
let (offset, size) = MutableMultiValue::index_and_size(offsets, section_id, offset_index);
253219
let whitespace: Whitespace<'_> = (&*section).into();
@@ -256,7 +222,7 @@ impl<'borrow, 'lookup, 'event> MutableMultiValue<'borrow, 'lookup, 'event> {
256222

257223
let key_sep_events = whitespace.key_value_separators();
258224
MutableMultiValue::set_offset(offsets, section_id, offset_index, 2 + key_sep_events.len());
259-
section.insert(offset, Event::Value(escape_value(value.as_ref()).into()));
225+
section.insert(offset, Event::Value(escape_value(value).into()));
260226
section.insert_many(offset, key_sep_events.into_iter().rev());
261227
section.insert(offset, Event::SectionKey(key.to_owned()));
262228
}

git-config/tests/file/mutable/multi_value.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ mod get {
1818
"100"
1919
[core]
2020
a=d\
21-
b
21+
"b "\
22+
c
2223
a=f\
23-
a"#
24+
a"#
2425
.parse()?;
2526

2627
let mut values = config.raw_values_mut("core", None, "a")?;
27-
assert_eq!(&*values.get()?, vec![cow_str("b100"), cow_str("db"), cow_str("fa"),]);
28+
assert_eq!(
29+
&*values.get()?,
30+
vec![cow_str("b100"), cow_str("db c"), cow_str("f a"),]
31+
);
2832

2933
values.delete_all();
3034
assert!(values.get().is_err());
@@ -54,7 +58,7 @@ mod set {
5458
for value in ["a b", " a b", "a b\t", ";c", "#c", "a\nb\n\tc"] {
5559
let mut config = init_config();
5660
let mut values = config.raw_values_mut("core", None, "a")?;
57-
values.set_values_all(value.into());
61+
values.set_all(value);
5862

5963
let config_str = config.to_string();
6064
let config: git_config::File = config_str.parse()?;
@@ -71,7 +75,7 @@ mod set {
7175
fn single_at_start() -> crate::Result {
7276
let mut config = init_config();
7377
let mut values = config.raw_values_mut("core", None, "a")?;
74-
values.set_string(0, "Hello".into());
78+
values.set_string_at(0, "Hello");
7579
assert_eq!(
7680
config.to_string(),
7781
"[core]\n a = Hello\n [core]\n a =d\n a= f"
@@ -83,7 +87,7 @@ mod set {
8387
fn single_at_end() -> crate::Result {
8488
let mut config = init_config();
8589
let mut values = config.raw_values_mut("core", None, "a")?;
86-
values.set_string(2, "Hello".into());
90+
values.set_string_at(2, "Hello");
8791
assert_eq!(
8892
config.to_string(),
8993
"[core]\n a = b\"100\"\n [core]\n a =d\n a= Hello"
@@ -95,7 +99,7 @@ mod set {
9599
fn all() -> crate::Result {
96100
let mut config = init_config();
97101
let mut values = config.raw_values_mut("core", None, "a")?;
98-
values.set_owned_values_all("Hello");
102+
values.set_all("Hello");
99103
assert_eq!(
100104
config.to_string(),
101105
"[core]\n a = Hello\n [core]\n a= Hello\n a =Hello"
@@ -107,7 +111,7 @@ mod set {
107111
fn all_empty() -> crate::Result {
108112
let mut config = init_config();
109113
let mut values = config.raw_values_mut("core", None, "a")?;
110-
values.set_owned_values_all("");
114+
values.set_all("");
111115
assert_eq!(
112116
config.to_string(),
113117
"[core]\n a = \n [core]\n a= \n a ="

0 commit comments

Comments
 (0)