about summary refs log tree commit diff
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2022-08-23 19:04:07 +0200
committerest31 <MTest31@outlook.com>2022-08-23 19:04:07 +0200
commit0a6af989f60bd84a13cbcb77d2e9d1003ae8af5f (patch)
tree256639f69ca71fb20c13329f6b390f256453f3f0
parent6a1f7afd2fe8f0a49cb702ac4ac4f1ff45d48ee5 (diff)
downloadrust-0a6af989f60bd84a13cbcb77d2e9d1003ae8af5f.tar.gz
rust-0a6af989f60bd84a13cbcb77d2e9d1003ae8af5f.zip
Simplify unicode_downloads.rs
Reduce duplication by moving fetching logic into a dedicated function.
-rw-r--r--src/tools/unicode-table-generator/src/unicode_download.rs31
1 files changed, 15 insertions, 16 deletions
diff --git a/src/tools/unicode-table-generator/src/unicode_download.rs b/src/tools/unicode-table-generator/src/unicode_download.rs
index 9b2e0a25891..714bb53382e 100644
--- a/src/tools/unicode-table-generator/src/unicode_download.rs
+++ b/src/tools/unicode-table-generator/src/unicode_download.rs
@@ -1,6 +1,6 @@
 use crate::UNICODE_DIRECTORY;
 use std::path::Path;
-use std::process::Command;
+use std::process::{Command, Output};
 
 static URL_PREFIX: &str = "https://www.unicode.org/Public/UCD/latest/ucd/";
 
@@ -9,6 +9,18 @@ static README: &str = "ReadMe.txt";
 static RESOURCES: &[&str] =
     &["DerivedCoreProperties.txt", "PropList.txt", "UnicodeData.txt", "SpecialCasing.txt"];
 
+#[track_caller]
+fn fetch(url: &str) -> Output {
+    let output = Command::new("curl").arg(URL_PREFIX.to_owned() + url).output().unwrap();
+    if !output.status.success() {
+        panic!(
+            "Failed to run curl to fetch {url}: stderr: {}",
+            String::from_utf8_lossy(&output.stderr)
+        );
+    }
+    output
+}
+
 pub fn fetch_latest() {
     let directory = Path::new(UNICODE_DIRECTORY);
     if directory.exists() {
@@ -20,27 +32,14 @@ pub fn fetch_latest() {
     if let Err(e) = std::fs::create_dir_all(directory) {
         panic!("Failed to create {UNICODE_DIRECTORY:?}: {e}");
     }
-    let output = Command::new("curl").arg(URL_PREFIX.to_owned() + README).output().unwrap();
-    if !output.status.success() {
-        panic!(
-            "Failed to run curl to fetch readme: stderr: {}",
-            String::from_utf8_lossy(&output.stderr)
-        );
-    }
+    let output = fetch(README);
     let current = std::fs::read_to_string(directory.join(README)).unwrap_or_default();
     if current.as_bytes() != &output.stdout[..] {
         std::fs::write(directory.join(README), output.stdout).unwrap();
     }
 
     for resource in RESOURCES {
-        let output = Command::new("curl").arg(URL_PREFIX.to_owned() + resource).output().unwrap();
-        if !output.status.success() {
-            panic!(
-                "Failed to run curl to fetch {}: stderr: {}",
-                resource,
-                String::from_utf8_lossy(&output.stderr)
-            );
-        }
+        let output = fetch(resource);
         std::fs::write(directory.join(resource), output.stdout).unwrap();
     }
 }