Move filename logic to FileType
authorAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 2 Apr 2018 21:11:38 +0000 (00:11 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 2 Apr 2018 21:11:38 +0000 (00:11 +0300)
src/cargo/ops/cargo_rustc/context/compilation_files.rs
src/cargo/ops/cargo_rustc/context/target_info.rs

index 752b07cf6c6261118dc7c5e33a4a28d51885166a..81272fe8e6fd93264b53873c3416f134d99bf9c2 100644 (file)
@@ -235,7 +235,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
                     .map(|(ld, ls)| ld.join(format!("lib{}.rmeta", ls)));
                 ret.push((filename, link_dst, FileFlavor::Linkable));
             } else {
-                let mut add = |crate_type: &str, file_type: FileFlavor| -> CargoResult<()> {
+                let mut add = |crate_type: &str, flavor: FileFlavor| -> CargoResult<()> {
                     let crate_type = if crate_type == "lib" {
                         "rlib"
                     } else {
@@ -243,43 +243,19 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
                     };
                     let file_types = info.file_types(
                         crate_type,
-                        file_type,
+                        flavor,
                         unit.target.kind(),
                         cx.target_triple(),
                     )?;
 
                     match file_types {
-                        Some(types) => {
-                            for file_type in types {
-                                // wasm bin target will generate two files in deps such as
-                                // "web-stuff.js" and "web_stuff.wasm". Note the different usages of
-                                // "-" and "_". should_replace_hyphens is a flag to indicate that
-                                // we need to convert the stem "web-stuff" to "web_stuff", so we
-                                // won't miss "web_stuff.wasm".
-                                let conv = |s: String| {
-                                    if file_type.should_replace_hyphens {
-                                        s.replace("-", "_")
-                                    } else {
-                                        s
-                                    }
-                                };
-                                let filename = out_dir.join(format!(
-                                    "{}{}{}",
-                                    file_type.prefix,
-                                    conv(file_stem.clone()),
-                                    file_type.suffix,
-                                ));
-                                let link_dst = link_stem.clone().map(|(ld, ls)| {
-                                    ld.join(format!(
-                                        "{}{}{}",
-                                        file_type.prefix,
-                                        conv(ls),
-                                        file_type.suffix
-                                    ))
-                                });
-                                ret.push((filename, link_dst, file_type.flavor));
-                            }
-                        }
+                        Some(types) => for file_type in types {
+                            let filename = out_dir.join(file_type.filename(&file_stem));
+                            let link_dst = link_stem
+                                .as_ref()
+                                .map(|&(ref ld, ref ls)| ld.join(file_type.filename(ls)));
+                            ret.push((filename, link_dst, file_type.flavor));
+                        },
                         // not supported, don't worry about it
                         None => {
                             unsupported.push(crate_type.to_string());
index 6dbb3076bdfa8bad6d97f287cc87b4f901897a7b..30978395324b09a61db03c4ae2ca1df2f834bc06 100644 (file)
@@ -28,10 +28,26 @@ pub enum FileFlavor {
 }
 
 pub struct FileType {
-    pub suffix: String,
-    pub prefix: String,
     pub flavor: FileFlavor,
-    pub should_replace_hyphens: bool,
+    suffix: String,
+    prefix: String,
+    // wasm bin target will generate two files in deps such as
+    // "web-stuff.js" and "web_stuff.wasm". Note the different usages of
+    // "-" and "_". should_replace_hyphens is a flag to indicate that
+    // we need to convert the stem "web-stuff" to "web_stuff", so we
+    // won't miss "web_stuff.wasm".
+    should_replace_hyphens: bool,
+}
+
+impl FileType {
+    pub fn filename(&self, stem: &str) -> String {
+        let stem = if self.should_replace_hyphens {
+            stem.replace("-", "_")
+        } else {
+            stem.to_string()
+        };
+        format!("{}{}{}", self.prefix, stem, self.suffix)
+    }
 }
 
 impl TargetInfo {
@@ -126,7 +142,7 @@ impl TargetInfo {
     pub fn file_types(
         &self,
         crate_type: &str,
-        file_type: FileFlavor,
+        flavor: FileFlavor,
         kind: &TargetKind,
         target_triple: &str,
     ) -> CargoResult<Option<Vec<FileType>>> {
@@ -145,9 +161,9 @@ impl TargetInfo {
         };
         let mut ret = vec![
             FileType {
-                suffix: suffix.to_string(),
+                suffix: suffix.clone(),
                 prefix: prefix.clone(),
-                flavor: file_type,
+                flavor,
                 should_replace_hyphens: false,
             },
         ];