From b30892fc013118186982f677412cadb249358346 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 21 Jul 2014 19:33:02 -0700 Subject: [PATCH] Use Path::display().to_string() less often Unfortunately this cannot yet have a test for it as rustc itself does not work if it is run on non-utf8 paths. --- src/cargo/util/toml.rs | 73 +++++++++++++++++++--------- tests/test_cargo_compile_git_deps.rs | 2 +- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index a8783c33e..4eab30bf4 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -1,7 +1,8 @@ use serialize::Decodable; use std::collections::HashMap; -use std::str; +use std::fmt; use std::io::fs; +use std::str; use toml; use core::{SourceId, GitKind}; @@ -230,7 +231,7 @@ fn inferred_lib_target(name: &str, layout: &Layout) -> Option> { vec![TomlTarget { name: name.to_string(), crate_type: None, - path: Some(lib.display().to_string()), + path: Some(TomlPath(lib.clone())), test: None, plugin: None, }] @@ -250,7 +251,7 @@ fn inferred_bin_targets(name: &str, layout: &Layout) -> Option> TomlTarget { name: name, crate_type: None, - path: Some(bin.display().to_string()), + path: Some(TomlPath(bin.clone())), test: None, plugin: None, } @@ -264,7 +265,7 @@ fn inferred_example_targets(layout: &Layout) -> Option> { TomlTarget { name: name.to_string(), crate_type: None, - path: Some(ex.display().to_string()), + path: Some(TomlPath(ex.clone())), test: None, plugin: None, } @@ -278,7 +279,7 @@ fn inferred_test_targets(layout: &Layout) -> Option> { TomlTarget { name: name.to_string(), crate_type: None, - path: Some(ex.display().to_string()), + path: Some(TomlPath(ex.clone())), test: None, plugin: None, } @@ -312,7 +313,7 @@ impl TomlManifest { TomlTarget { name: t.name.clone(), crate_type: t.crate_type.clone(), - path: layout.lib.as_ref().map(|p| p.display().to_string()), + path: layout.lib.as_ref().map(|p| TomlPath(p.clone())), test: t.test, plugin: t.plugin, } @@ -332,7 +333,7 @@ impl TomlManifest { TomlTarget { name: t.name.clone(), crate_type: t.crate_type.clone(), - path: bin.as_ref().map(|p| p.display().to_string()), + path: bin.as_ref().map(|&p| TomlPath(p.clone())), test: t.test, plugin: None, } @@ -455,11 +456,35 @@ fn process_dependencies<'a>(cx: &mut Context<'a>, dev: bool, struct TomlTarget { name: String, crate_type: Option>, - path: Option, + path: Option, test: Option, plugin: Option, } +#[deriving(Decodable,Encodable,PartialEq,Clone)] +enum TomlPath { + TomlString(String), + TomlPath(Path), +} + +impl TomlPath { + fn to_path(&self) -> Path { + match *self { + TomlString(ref s) => Path::new(s.as_slice()), + TomlPath(ref p) => p.clone(), + } + } +} + +impl fmt::Show for TomlPath { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + TomlString(ref s) => s.fmt(f), + TomlPath(ref p) => p.display().fmt(f), + } + } +} + fn normalize(lib: Option<&[TomlLibTarget]>, bin: Option<&[TomlBinTarget]>, example: Option<&[TomlExampleTarget]>, @@ -494,7 +519,9 @@ fn normalize(lib: Option<&[TomlLibTarget]>, fn lib_targets(dst: &mut Vec, libs: &[TomlLibTarget], dep: TestDep, metadata: &Metadata) { let l = &libs[0]; - let path = l.path.clone().unwrap_or_else(|| format!("src/{}.rs", l.name)); + let path = l.path.clone().unwrap_or_else(|| { + TomlString(format!("src/{}.rs", l.name)) + }); let crate_types = l.crate_type.clone().and_then(|kinds| { LibKind::from_strs(kinds).ok() }).unwrap_or_else(|| { @@ -503,7 +530,7 @@ fn normalize(lib: Option<&[TomlLibTarget]>, for profile in target_profiles(l, Some(dep)).iter() { dst.push(Target::lib_target(l.name.as_slice(), crate_types.clone(), - &Path::new(path.as_slice()), profile, + &path.to_path(), profile, metadata)); } } @@ -511,11 +538,13 @@ fn normalize(lib: Option<&[TomlLibTarget]>, fn bin_targets(dst: &mut Vec, bins: &[TomlBinTarget], default: |&TomlBinTarget| -> String) { for bin in bins.iter() { - let path = bin.path.clone().unwrap_or_else(|| default(bin)); + let path = bin.path.clone().unwrap_or_else(|| { + TomlString(default(bin)) + }); for profile in target_profiles(bin, None).iter() { dst.push(Target::bin_target(bin.name.as_slice(), - &Path::new(path.as_slice()), + &path.to_path(), profile)); } } @@ -524,28 +553,26 @@ fn normalize(lib: Option<&[TomlLibTarget]>, fn example_targets(dst: &mut Vec, examples: &[TomlExampleTarget], default: |&TomlExampleTarget| -> String) { for ex in examples.iter() { - let path = ex.path.clone().unwrap_or_else(|| default(ex)); + let path = ex.path.clone().unwrap_or_else(|| TomlString(default(ex))); let profile = &Profile::default_test().test(false); - { - dst.push(Target::example_target(ex.name.as_slice(), - &Path::new(path.as_slice()), + dst.push(Target::example_target(ex.name.as_slice(), + &path.to_path(), profile)); - } } } fn test_targets(dst: &mut Vec, tests: &[TomlTestTarget], default: |&TomlTestTarget| -> String) { for test in tests.iter() { - let path = test.path.clone().unwrap_or_else(|| default(test)); + let path = test.path.clone().unwrap_or_else(|| { + TomlString(default(test)) + }); let profile = &Profile::default_test(); - { - dst.push(Target::test_target(test.name.as_slice(), - &Path::new(path.as_slice()), - profile)); - } + dst.push(Target::test_target(test.name.as_slice(), + &path.to_path(), + profile)); } } diff --git a/tests/test_cargo_compile_git_deps.rs b/tests/test_cargo_compile_git_deps.rs index 3a0f52b2c..9af4b5004 100644 --- a/tests/test_cargo_compile_git_deps.rs +++ b/tests/test_cargo_compile_git_deps.rs @@ -1,4 +1,4 @@ -use std::io::File; +use std::io::{File, TempDir}; use support::{ProjectBuilder, ResultTest, project, execs, main_file, paths}; use support::{cargo_dir};