Skip to content

Commit 4a29a19

Browse files
committed
refactor
- make clear that `ContextInfo` is an intermediary, a utility by immediately destructuring it. - address copilot comments
1 parent 56f88ef commit 4a29a19

File tree

1 file changed

+22
-18
lines changed
  • crates/but/src/legacy/id

1 file changed

+22
-18
lines changed

crates/but/src/legacy/id/mod.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,17 @@ impl IdMap {
137137
// TODO: create an API that enforces re-use of `RefInfo` by its users.
138138
pub fn new(ctx: &mut Context) -> anyhow::Result<Self> {
139139
let mut max_zero_count = 1; // Ensure at least two "0" in ID.
140-
let context_info = context_info(ctx)?;
140+
let ContextInfo {
141+
branch_names,
142+
commit_ids,
143+
uncommitted_files,
144+
committed_files,
145+
} = context_info(ctx)?;
141146
let mut pairs_to_count: HashMap<u16, u8> = HashMap::new();
142147
fn u8_pair_to_u16(two: [u8; 2]) -> u16 {
143148
two[0] as u16 * 256 + two[1] as u16
144149
}
145-
for branch_name in &context_info.branch_names {
150+
for branch_name in &branch_names {
146151
for pair in branch_name.windows(2) {
147152
let pair: [u8; 2] = pair.try_into()?;
148153
if !pair[0].is_ascii_alphanumeric() || !pair[1].is_ascii_alphanumeric() {
@@ -166,7 +171,7 @@ impl IdMap {
166171

167172
let mut branch_name_to_cli_id: HashMap<BString, CliId> = HashMap::new();
168173
let mut ids_used: HashSet<String> = HashSet::new();
169-
'branch_name: for branch_name in context_info.branch_names {
174+
'branch_name: for branch_name in branch_names {
170175
// Find first non-conflicting pair and use it as CliId.
171176
for pair in branch_name.windows(2) {
172177
let pair: [u8; 2] = pair.try_into()?;
@@ -194,25 +199,25 @@ impl IdMap {
194199
}
195200
};
196201

197-
let mut uncommitted_files: BTreeSet<UncommittedFile> = BTreeSet::new();
198-
for assignment_path in context_info.uncommitted_files {
199-
uncommitted_files.insert(UncommittedFile {
202+
let uncommitted_files: BTreeSet<_> = uncommitted_files
203+
.into_iter()
204+
.map(|assignment_path| UncommittedFile {
200205
assignment_path,
201206
id: get_next_id(),
202-
});
203-
}
207+
})
208+
.collect();
204209

205-
let mut committed_files: BTreeSet<CommittedFile> = BTreeSet::new();
206-
for commit_oid_path in context_info.committed_files {
207-
committed_files.insert(CommittedFile {
210+
let committed_files: BTreeSet<_> = committed_files
211+
.into_iter()
212+
.map(|commit_oid_path| CommittedFile {
208213
commit_oid_path,
209214
id: get_next_id(),
210-
});
211-
}
215+
})
216+
.collect();
212217

213218
Ok(Self {
214219
branch_name_to_cli_id,
215-
commit_ids: context_info.commit_ids,
220+
commit_ids,
216221
uncommitted_files,
217222
committed_files,
218223
unassigned: CliId::Unassigned {
@@ -252,13 +257,13 @@ impl IdMap {
252257

253258
// Then try CliId matching
254259
if s.len() == 2 {
255-
let mut cli_matches = Vec::new();
260+
let mut matches = Vec::new();
256261
if let Some(UncommittedFile {
257262
assignment_path: (assignment, path),
258263
..
259264
}) = self.uncommitted_files.get(s)
260265
{
261-
cli_matches.push(CliId::UncommittedFile {
266+
matches.push(CliId::UncommittedFile {
262267
assignment: *assignment,
263268
path: path.to_owned(),
264269
id: s.to_string(),
@@ -269,13 +274,12 @@ impl IdMap {
269274
..
270275
}) = self.committed_files.get(s)
271276
{
272-
cli_matches.push(CliId::CommittedFile {
277+
matches.push(CliId::CommittedFile {
273278
commit_oid: *commit_oid,
274279
path: path.to_owned(),
275280
id: s.to_string(),
276281
});
277282
}
278-
matches.extend(cli_matches);
279283
}
280284
if s.find(|c: char| c != '0').is_none() {
281285
matches.push(self.unassigned().clone());

0 commit comments

Comments
 (0)