Skip to content

Commit 73e33a2

Browse files
committed
Add collate() method
1 parent f866918 commit 73e33a2

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

crates/oak_package/src/collation.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ use crate::package_description::Description;
88
/// file list is used. Otherwise files are sorted in C locale order
99
/// (byte-wise, same as R's default).
1010
pub fn collation_order(description: &Description, files: &[String]) -> Vec<String> {
11-
if let Some(collate) = description.fields.get("Collate") {
12-
let available: HashSet<&str> = files.iter().map(|s| s.as_str()).collect();
13-
collate
14-
.split_whitespace()
15-
.filter(|name| available.contains(name))
16-
.map(|name| name.to_string())
17-
.collect()
18-
} else {
19-
let mut sorted = files.to_vec();
20-
sorted.sort();
21-
sorted
22-
}
11+
description
12+
.collate()
13+
.map(|collate| {
14+
let available: HashSet<&str> = files.iter().map(|s| s.as_str()).collect();
15+
collate
16+
.into_iter()
17+
.filter(|name| available.contains(name.as_str()))
18+
.collect()
19+
})
20+
.unwrap_or_else(|| {
21+
let mut sorted = files.to_vec();
22+
sorted.sort();
23+
sorted
24+
})
2325
}
2426

2527
#[cfg(test)]

crates/oak_package/src/package_description.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ impl Description {
7777
fields,
7878
})
7979
}
80+
81+
/// Parse the `Collate` field, if present, returning the whitespace-separated
82+
/// file names in the order specified.
83+
pub fn collate(&self) -> Option<Vec<String>> {
84+
let collate = self.fields.get("Collate")?;
85+
Some(collate.split_whitespace().map(|s| s.to_string()).collect())
86+
}
8087
}
8188

8289
/// Parse a DCF (Debian Control File) format string into a key-value map.

0 commit comments

Comments
 (0)