Skip to content

Commit 79433d3

Browse files
committed
make group_cmnd express more clearly what it does
1 parent 7f1fd7a commit 79433d3

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/sudoers/mod.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ mod entry;
1010
mod tokens;
1111

1212
use std::collections::{HashMap, HashSet};
13+
use std::io;
1314
use std::path::{Path, PathBuf};
14-
use std::{io, mem};
1515

1616
use crate::common::resolve::resolve_path;
1717
use crate::defaults;
@@ -208,28 +208,20 @@ fn group_cmd_specs_per_runas<'a>(
208208
cmnd_specs: impl Iterator<Item = (Option<&'a RunAs>, (Tag, &'a Spec<Command>))>,
209209
cmnd_aliases: &'a [Def<Command>],
210210
) -> impl Iterator<Item = Entry<'a>> {
211+
use std::borrow::BorrowMut;
212+
let mut cmnd_specs = cmnd_specs.peekable();
211213
let mut entries = vec![];
212-
let mut last_runas = None;
213-
let mut collected_specs = vec![];
214-
215-
for (runas, (tag, spec)) in cmnd_specs {
216-
if runas.map(|r| r as *const _) != last_runas.map(|r| r as *const _) {
217-
if !collected_specs.is_empty() {
218-
entries.push(Entry::new(
219-
last_runas,
220-
mem::take(&mut collected_specs),
221-
cmnd_aliases,
222-
));
223-
}
224-
225-
last_runas = runas;
226-
}
227-
228-
collected_specs.push((tag, spec));
229-
}
230214

231-
if !collected_specs.is_empty() {
232-
entries.push(Entry::new(last_runas, collected_specs, cmnd_aliases));
215+
while let Some((cur_runas, _)) = cmnd_specs.peek() {
216+
let cur_runas = *cur_runas;
217+
let specs = cmnd_specs.borrow_mut().take_while(|(runas, _)| {
218+
runas.map(|r| r as *const _) == cur_runas.map(|r| r as *const _)
219+
});
220+
entries.push(Entry::new(
221+
cur_runas,
222+
specs.map(|x| x.1).collect(),
223+
cmnd_aliases,
224+
));
233225
}
234226

235227
entries.into_iter()

0 commit comments

Comments
 (0)