Add some convenience methods

This commit is contained in:
Yehuda Katz 2014-05-05 19:46:14 -07:00
parent 50f110a4c0
commit f0c9fcd215
4 changed files with 28 additions and 20 deletions

@ -1 +1 @@
Subproject commit 138de49f217604d22e019afd71c529a7d76643f0
Subproject commit de700414aab1aaa4461618ce7a516cb24a8e6665

View File

@ -107,6 +107,14 @@ impl CargoError {
CargoInternalError(error)
}
pub fn described<T: ToStr>(description: T) -> CargoError {
CargoInternalError(Described(description.to_str()))
}
pub fn other() -> CargoError {
CargoInternalError(Other)
}
pub fn cli_error(self) -> CLIError {
match self {
CargoInternalError(err) =>

View File

@ -2,7 +2,7 @@ extern crate collections;
extern crate serialize;
extern crate toml;
use core::errors::{CargoResult,CargoError,ToResult,Described,Other};
use core::errors::{CargoResult,CargoError,ToResult};
use serialize::{Encodable,Encoder};
use std::{io,fmt};
@ -76,7 +76,7 @@ impl fmt::Show for ConfigValue {
pub fn get_config(pwd: Path, key: &str) -> CargoResult<ConfigValue> {
find_in_tree(&pwd, |file| extract_config(file, key)).to_result(|_|
CargoError::internal(Described(format!("Config key not found: {}", key))))
CargoError::described(format!("Config key not found: {}", key)))
}
pub fn all_configs(pwd: Path) -> CargoResult<collections::HashMap<~str, ConfigValue>> {
@ -100,7 +100,7 @@ fn find_in_tree<T>(pwd: &Path, walk: |io::fs::File| -> CargoResult<T>) -> CargoR
loop {
let possible = current.join(".cargo").join("config");
if possible.exists() {
let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::internal(Other)));
let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::other()));
match walk(file) {
Ok(res) => return Ok(res),
_ => ()
@ -110,7 +110,7 @@ fn find_in_tree<T>(pwd: &Path, walk: |io::fs::File| -> CargoResult<T>) -> CargoR
if !current.pop() { break; }
}
Err(CargoError::internal(Other))
Err(CargoError::other())
}
fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult<()> {
@ -120,14 +120,14 @@ fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult
loop {
let possible = current.join(".cargo").join("config");
if possible.exists() {
let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::internal(Other)));
let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::other()));
match walk(file) {
Err(_) => err = false,
_ => ()
}
}
if err { return Err(CargoError::internal(Other)); }
if err { return Err(CargoError::other()); }
if !current.pop() { break; }
}
@ -135,25 +135,25 @@ fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult
}
fn extract_config(file: io::fs::File, key: &str) -> CargoResult<ConfigValue> {
let path = try!(file.path().as_str().to_result(|_| CargoError::internal(Other))).to_owned();
let path = try!(file.path().as_str().to_result(|_| CargoError::other())).to_owned();
let mut buf = io::BufferedReader::new(file);
let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::internal(Other)));
let val = try!(root.lookup(key).to_result(|_| CargoError::internal(Other)));
let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::other()));
let val = try!(root.lookup(key).to_result(|_| CargoError::other()));
let v = match val {
&toml::String(ref val) => String(val.to_owned()),
&toml::Array(ref val) => List(val.iter().map(|s: &toml::Value| s.to_str()).collect()),
_ => return Err(CargoError::internal(Other))
_ => return Err(CargoError::other())
};
Ok(ConfigValue{ value: v, path: vec!(path) })
}
fn extract_all_configs(file: io::fs::File, map: &mut collections::HashMap<~str, ConfigValue>) -> CargoResult<()> {
let path = try!(file.path().as_str().to_result(|_| CargoError::internal(Other))).to_owned();
let path = try!(file.path().as_str().to_result(|_| CargoError::other())).to_owned();
let mut buf = io::BufferedReader::new(file);
let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::internal(Other)));
let table = try!(root.get_table().to_result(|_| CargoError::internal(Other)));
let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::other()));
let table = try!(root.get_table().to_result(|_| CargoError::other()));
for (key, value) in table.iter() {
match value {
@ -174,11 +174,11 @@ fn extract_all_configs(file: io::fs::File, map: &mut collections::HashMap<~str,
fn merge_array(existing: &mut ConfigValue, val: &[toml::Value], path: &str) -> CargoResult<()> {
match existing.value {
String(_) => return Err(CargoError::internal(Other)),
String(_) => return Err(CargoError::other()),
List(ref mut list) => {
let new_list: Vec<CargoResult<~str>> = val.iter().map(|s: &toml::Value| toml_string(s)).collect();
if new_list.iter().any(|v| v.is_err()) {
return Err(CargoError::internal(Other));
return Err(CargoError::other());
} else {
let new_list: Vec<~str> = new_list.move_iter().map(|v| v.unwrap()).collect();
list.push_all(new_list.as_slice());
@ -192,6 +192,6 @@ fn merge_array(existing: &mut ConfigValue, val: &[toml::Value], path: &str) -> C
fn toml_string(val: &toml::Value) -> CargoResult<~str> {
match val {
&toml::String(ref str) => Ok(str.to_owned()),
_ => Err(CargoError::internal(Other))
_ => Err(CargoError::other())
}
}

View File

@ -3,7 +3,7 @@ use std::path::Path;
use std::io;
use std::io::process::{Process,ProcessConfig,ProcessOutput,InheritFd};
use collections::HashMap;
use core::errors::{ToResult,CargoResult,CargoError,Described};
use core::errors::{ToResult,CargoResult,CargoError};
#[deriving(Clone,Eq)]
pub struct ProcessBuilder {
@ -68,7 +68,7 @@ impl ProcessBuilder {
config.cwd = Some(&self.cwd);
let os_path = try!(os::getenv("PATH").to_result(|_|
CargoError::internal(Described("Could not find the PATH environment variable".to_owned()))));
CargoError::described("Could not find the PATH environment variable")));
let path = os_path + PATH_SEP + self.path.connect(PATH_SEP);
@ -76,7 +76,7 @@ impl ProcessBuilder {
config.env = Some(path.as_slice());
Process::configure(config).map(|mut ok| ok.wait_with_output()).to_result(|_|
CargoError::internal(Described("Could not spawn process".to_owned())))
CargoError::described("Could not spawn process"))
}
fn build_config<'a>(&'a self) -> io::IoResult<ProcessConfig<'a>> {