Summary
};
use core::dependency::SerializedDependency;
-use util::{CargoResult, box_error};
+use util::{CargoResult, human};
#[deriving(PartialEq,Clone)]
pub struct Manifest {
"rlib" => Ok(Rlib),
"dylib" => Ok(Dylib),
"staticlib" => Ok(StaticLib),
- _ => Err(box_error(format!("{} was not one of lib|rlib|dylib|staticlib", string)))
+ _ => Err(human(format!("{} was not one of lib|rlib|dylib|staticlib", string)))
}
}
}
))
+macro_rules! cargo_try (
+ ($expr:expr) => ({
+ use util::BoxError;
+ try!($expr.box_error())
+ })
+)
+
pub mod core;
pub mod ops;
pub mod sources;
use std::io::File;
use util;
use core::{Package,Manifest,SourceId};
-use util::{CargoResult, box_error, human};
+use util::{CargoResult, human};
pub fn read_manifest(contents: &[u8], source_id: &SourceId) -> CargoResult<(Manifest, Vec<Path>)> {
util::toml::to_manifest(contents, source_id).map_err(human)
pub fn read_package(path: &Path, source_id: &SourceId) -> CargoResult<(Package, Vec<Path>)> {
log!(5, "read_package; path={}; source-id={}", path.display(), source_id);
- let mut file = try!(File::open(path).map_err(box_error));
- let data = try!(file.read_to_end().map_err(box_error));
- let (manifest, nested) = try!(read_manifest(data.as_slice(), source_id));
+ let mut file = cargo_try!(File::open(path));
+ let data = cargo_try!(file.read_to_end());
+ let (manifest, nested) = cargo_try!(read_manifest(data.as_slice(), source_id));
Ok((Package::new(manifest, path), nested))
}
use url::Url;
-use util::{CargoResult, ProcessBuilder, process, box_error};
+use util::{CargoResult, ChainError, ProcessBuilder, process};
use std::fmt;
use std::fmt::{Show,Formatter};
use std::str;
macro_rules! git(
($config:expr, $str:expr, $($rest:expr),*) => (
- try!(git_inherit(&$config, format!($str, $($rest),*)))
+ cargo_try!(git_inherit(&$config, format!($str, $($rest),*)))
);
($config:expr, $str:expr) => (
- try!(git_inherit(&$config, format!($str)))
+ cargo_try!(git_inherit(&$config, format!($str)))
);
)
macro_rules! git_output(
($config:expr, $str:expr, $($rest:expr),*) => (
- try!(git_output(&$config, format!($str, $($rest),*)))
+ cargo_try!(git_output(&$config, format!($str, $($rest),*)))
);
($config:expr, $str:expr) => (
- try!(git_output(&$config, format!($str)))
+ cargo_try!(git_output(&$config, format!($str)))
);
)
pub fn checkout(&self, into: &Path) -> CargoResult<GitDatabase> {
if into.exists() {
- try!(self.fetch_into(into));
+ cargo_try!(self.fetch_into(into));
} else {
- try!(self.clone_into(into));
+ cargo_try!(self.clone_into(into));
}
Ok(GitDatabase { remote: self.clone(), path: into.clone() })
fn clone_into(&self, path: &Path) -> CargoResult<()> {
let dirname = Path::new(path.dirname());
- try!(mkdir_recursive(path, UserDir).map_err(box_error));
+ cargo_try!(mkdir_recursive(path, UserDir));
Ok(git!(dirname, "clone {} {} --bare --no-hardlinks --quiet", self.fetch_location(), path.display()))
}
}
pub fn copy_to<S: Str>(&self, reference: S, dest: &Path) -> CargoResult<GitCheckout> {
- let checkout = try!(GitCheckout::clone_into(dest, self.clone(), GitReference::for_str(reference.as_slice())));
+ let checkout = cargo_try!(GitCheckout::clone_into(dest, self.clone(), GitReference::for_str(reference.as_slice())));
- try!(checkout.fetch());
- try!(checkout.update_submodules());
+ cargo_try!(checkout.fetch());
+ cargo_try!(checkout.update_submodules());
Ok(checkout)
}
impl GitCheckout {
fn clone_into(into: &Path, database: GitDatabase, reference: GitReference) -> CargoResult<GitCheckout> {
- let revision = try!(database.rev_for(reference.as_slice()));
+ let revision = cargo_try!(database.rev_for(reference.as_slice()));
let checkout = GitCheckout { location: into.clone(), database: database, reference: reference, revision: revision };
// If the git checkout already exists, we don't need to clone it again
if !checkout.location.join(".git").exists() {
- try!(checkout.clone_repo());
+ cargo_try!(checkout.clone_repo());
}
Ok(checkout)
fn clone_repo(&self) -> CargoResult<()> {
let dirname = Path::new(self.location.dirname());
- try!(mkdir_recursive(&dirname, UserDir).map_err(|_|
- box_error(format!("Couldn't mkdir {}", Path::new(self.location.dirname()).display()))));
+ cargo_try!(mkdir_recursive(&dirname, UserDir).chain_error(||
+ format!("Couldn't mkdir {}", Path::new(self.location.dirname()).display())));
if self.location.exists() {
- try!(rmdir_recursive(&self.location).map_err(|_|
- box_error(format!("Couldn't rmdir {}", Path::new(&self.location).display()))));
+ cargo_try!(rmdir_recursive(&self.location).chain_error(||
+ format!("Couldn't rmdir {}", Path::new(&self.location).display())));
}
git!(dirname, "clone --no-checkout --quiet {} {}", self.get_source().display(), self.location.display());
- try!(chmod(&self.location, AllPermissions).map_err(box_error));
+ cargo_try!(chmod(&self.location, AllPermissions));
Ok(())
}
fn fetch(&self) -> CargoResult<()> {
git!(self.location, "fetch --force --quiet --tags {}", self.get_source().display());
- try!(self.reset(self.revision.as_slice()));
+ cargo_try!(self.reset(self.revision.as_slice()));
Ok(())
}
}
fn git_inherit(path: &Path, str: String) -> CargoResult<()> {
- git(path, str.as_slice()).exec().map_err(|err|
- box_error(format!("Executing `git {}` failed: {}", str, err)))
+ git(path, str.as_slice()).exec().chain_error(|| format!("Executing `git {}` failed", str))
}
fn git_output(path: &Path, str: String) -> CargoResult<String> {
- let output = try!(git(path, str.as_slice()).exec_with_output().map_err(|_|
- box_error(format!("Executing `git {}` failed", str))));
+ let output = cargo_try!(git(path, str.as_slice()).exec_with_output().chain_error(||
+ format!("Executing `git {}` failed", str)));
Ok(to_str(output.output.as_slice()).as_slice().trim_right().to_str())
}
use std::fmt::{Show,Formatter};
use core::{Package,PackageId,Summary,SourceId,Source};
use ops;
-use util::{CargoResult, box_error};
+use util::{CargoResult, error};
pub struct PathSource {
id: SourceId,
log!(5, "get_root_package; source={}", self);
if !self.updated {
- return Err(box_error("source has not been updated"))
+ return Err(error("source has not been updated"))
}
match self.packages.as_slice().head() {
Some(pkg) => Ok(pkg.clone()),
- None => Err(box_error("no package found in source"))
+ None => Err(error("no package found in source"))
}
}
}
fn cause<'a>(&'a self) -> Option<&'a CargoError> { None }
fn is_human(&self) -> bool { false }
+ fn box_error(self) -> Box<CargoError> {
+ box self as Box<CargoError>
+ }
+
fn concrete(&self) -> ConcreteCargoError {
ConcreteCargoError {
description: self.description(),
fn is_human(&self) -> bool {
(*self).is_human()
}
+
+ fn box_error(self) -> Box<CargoError> {
+ self
+ }
}
pub type CargoResult<T> = Result<T, Box<CargoError>>;
+pub trait BoxError<T> {
+ fn box_error(self) -> CargoResult<T>;
+}
+
+pub trait ChainError<T> {
+ fn chain_error<E: CargoError>(self, callback: || -> E) -> CargoResult<T> ;
+}
+
+impl<T, E: CargoError> BoxError<T> for Result<T, E> {
+ fn box_error(self) -> CargoResult<T> {
+ self.map_err(|err| err.box_error())
+ }
+}
+
+impl<T, E: CargoError> ChainError<T> for Result<T, E> {
+ fn chain_error<E: CargoError>(self, callback: || -> E) -> CargoResult<T> {
+ self.map_err(|err| {
+ let mut update = callback().concrete();
+ update.cause = Some(err.box_error());
+ box update as Box<CargoError>
+ })
+ }
+}
+
impl CargoError for &'static str {
fn description(&self) -> String { self.to_str() }
fn is_human(&self) -> bool { true }
concrete.cause = Some(original);
box concrete as Box<CargoError>
}
-
-pub fn box_error<S: CargoError + 'static>(err: S) -> Box<CargoError> {
- box err as Box<CargoError>
-}
pub use self::config::Config;
pub use self::process_builder::{process,ProcessBuilder};
pub use self::result::{Wrap, Require};
-pub use self::errors::{CargoResult, CargoError, CliResult, CliError, ProcessError};
-pub use self::errors::{process_error, box_error, internal_error, error, human, chain};
+pub use self::errors::{CargoResult, CargoError, BoxError, ChainError, CliResult, CliError, ProcessError};
+pub use self::errors::{process_error, internal_error, error, human, chain};
pub use self::paths::realpath;
pub mod graph;
use core::{SourceId,GitKind};
use core::manifest::{LibKind,Lib};
use core::{Summary,Manifest,Target,Dependency,PackageId};
-use util::{CargoResult, Require, error, box_error};
+use util::{CargoResult, Require, error};
pub fn to_manifest(contents: &[u8], source_id: &SourceId) -> CargoResult<(Manifest, Vec<Path>)> {
let root = try!(toml::parse_from_bytes(contents).map_err(|_| error("Cargo.toml is not valid Toml")));
toml::from_toml(root.clone())
}
- let project = try!(decode(&root, "project").map_err(box_error));
+ let project = cargo_try!(decode(&root, "project"));
let lib = decode(&root, "lib").ok();
let bin = decode(&root, "bin").ok();