Skip to content

Commit 0a6af98

Browse files
committed
Simplify unicode_downloads.rs
Reduce duplication by moving fetching logic into a dedicated function.
1 parent 6a1f7af commit 0a6af98

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::UNICODE_DIRECTORY;
22
use std::path::Path;
3-
use std::process::Command;
3+
use std::process::{Command, Output};
44

55
static URL_PREFIX: &str = "https://www.unicode.org/Public/UCD/latest/ucd/";
66

@@ -9,6 +9,18 @@ static README: &str = "ReadMe.txt";
99
static RESOURCES: &[&str] =
1010
&["DerivedCoreProperties.txt", "PropList.txt", "UnicodeData.txt", "SpecialCasing.txt"];
1111

12+
#[track_caller]
13+
fn fetch(url: &str) -> Output {
14+
let output = Command::new("curl").arg(URL_PREFIX.to_owned() + url).output().unwrap();
15+
if !output.status.success() {
16+
panic!(
17+
"Failed to run curl to fetch {url}: stderr: {}",
18+
String::from_utf8_lossy(&output.stderr)
19+
);
20+
}
21+
output
22+
}
23+
1224
pub fn fetch_latest() {
1325
let directory = Path::new(UNICODE_DIRECTORY);
1426
if directory.exists() {
@@ -20,27 +32,14 @@ pub fn fetch_latest() {
2032
if let Err(e) = std::fs::create_dir_all(directory) {
2133
panic!("Failed to create {UNICODE_DIRECTORY:?}: {e}");
2234
}
23-
let output = Command::new("curl").arg(URL_PREFIX.to_owned() + README).output().unwrap();
24-
if !output.status.success() {
25-
panic!(
26-
"Failed to run curl to fetch readme: stderr: {}",
27-
String::from_utf8_lossy(&output.stderr)
28-
);
29-
}
35+
let output = fetch(README);
3036
let current = std::fs::read_to_string(directory.join(README)).unwrap_or_default();
3137
if current.as_bytes() != &output.stdout[..] {
3238
std::fs::write(directory.join(README), output.stdout).unwrap();
3339
}
3440

3541
for resource in RESOURCES {
36-
let output = Command::new("curl").arg(URL_PREFIX.to_owned() + resource).output().unwrap();
37-
if !output.status.success() {
38-
panic!(
39-
"Failed to run curl to fetch {}: stderr: {}",
40-
resource,
41-
String::from_utf8_lossy(&output.stderr)
42-
);
43-
}
42+
let output = fetch(resource);
4443
std::fs::write(directory.join(resource), output.stdout).unwrap();
4544
}
4645
}

0 commit comments

Comments
 (0)