-#[crate_id="cargo-compile"];
-#[allow(deprecated_owned_vector)];
+#![crate_id="cargo-compile"]
+#![allow(deprecated_owned_vector)]
extern crate serialize;
extern crate hammer;
-// extern crate cargo;
+extern crate cargo;
use serialize::{Decodable};
-use hammer::{FlagDecoder,FlagConfig,FlagConfiguration};
+use hammer::{FlagDecoder,FlagConfig,FlagConfiguration,HammerError};
use std::io;
-use io::{IoResult,IoError,OtherIoError,BufReader};
+use io::BufReader;
use io::process::{Process,ProcessExit,ProcessOutput,InheritFd,ProcessConfig};
+use cargo::{ToCargoError,CargoResult};
#[deriving(Decodable)]
struct Options {
}
}
-fn compile() -> IoResult<()> {
+fn compile() -> CargoResult<()> {
let options = try!(flags::<Options>());
- let manifest_bytes = try!(read_manifest(options.manifest_path));
+ let manifest_bytes = try!(read_manifest(options.manifest_path).to_cargo_error(~"Could not read manifest", 1));
call_rustc(~BufReader::new(manifest_bytes.as_slice()))
}
-fn flags<T: FlagConfig + Decodable<FlagDecoder>>() -> IoResult<T> {
+fn flags<T: FlagConfig + Decodable<FlagDecoder, HammerError>>() -> CargoResult<T> {
let mut decoder = FlagDecoder::new::<T>(std::os::args().tail());
- let flags: T = Decodable::decode(&mut decoder);
-
- if decoder.error.is_some() {
- Err(IoError{ kind: OtherIoError, desc: "could not decode flags", detail: Some(decoder.error.unwrap()) })
- } else {
- Ok(flags)
- }
+ Decodable::decode(&mut decoder).to_cargo_error(|e: HammerError| e.message, 1)
}
-fn read_manifest(manifest_path: &str) -> IoResult<~[u8]> {
+fn read_manifest(manifest_path: &str) -> CargoResult<~[u8]> {
Ok((try!(exec_with_output("cargo-read-manifest", [~"--manifest-path", manifest_path.to_owned()], None))).output)
}
-fn call_rustc(mut manifest_data: ~Reader:) -> IoResult<()> {
+fn call_rustc(mut manifest_data: ~Reader:) -> CargoResult<()> {
let data: &mut Reader = manifest_data;
try!(exec_tty("cargo-rustc", [], Some(data)));
Ok(())
}
-fn exec_with_output(program: &str, args: &[~str], input: Option<&mut Reader>) -> IoResult<ProcessOutput> {
+fn exec_with_output(program: &str, args: &[~str], input: Option<&mut Reader>) -> CargoResult<ProcessOutput> {
Ok((try!(exec(program, args, input, |_| {}))).wait_with_output())
}
-fn exec_tty(program: &str, args: &[~str], input: Option<&mut Reader>) -> IoResult<ProcessExit> {
+fn exec_tty(program: &str, args: &[~str], input: Option<&mut Reader>) -> CargoResult<ProcessExit> {
Ok((try!(exec(program, args, input, |config| {
config.stdout = InheritFd(1);
config.stderr = InheritFd(2);
}))).wait())
}
-fn exec(program: &str, args: &[~str], input: Option<&mut Reader>, configurator: |&mut ProcessConfig|) -> IoResult<Process> {
+fn exec(program: &str, args: &[~str], input: Option<&mut Reader>, configurator: |&mut ProcessConfig|) -> CargoResult<Process> {
let mut config = ProcessConfig::new();
config.program = program;
config.args = args;
println!("Executing {} {}", program, args);
- let mut process = try!(Process::configure(config));
+ let mut process = try!(Process::configure(config).to_cargo_error(~"Could not configure process", 1));
input.map(|mut reader| io::util::copy(&mut reader, process.stdin.get_mut_ref()));
-#[crate_id="cargo"];
-#[crate_type="rlib"];
+#![crate_id="cargo"]
+#![crate_type="rlib"]
-#[allow(deprecated_owned_vector)];
+#![allow(deprecated_owned_vector)]
extern crate serialize;
extern crate hammer;
use std::io;
use std::fmt;
use std::fmt::{Show,Formatter};
-use hammer::{FlagDecoder,FlagConfig};
+use hammer::{FlagDecoder,FlagConfig,HammerError};
pub mod util;
#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
pub struct Manifest {
- project: ~Project,
- root: ~str,
- lib: ~[LibTarget],
- bin: ~[ExecTarget]
+ pub project: ~Project,
+ pub root: ~str,
+ pub lib: ~[LibTarget],
+ pub bin: ~[ExecTarget]
}
#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
pub struct ExecTarget {
- name: ~str,
- path: ~str
+ pub name: ~str,
+ pub path: ~str
}
#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
pub struct LibTarget {
- name: ~str,
- path: ~str
+ pub name: ~str,
+ pub path: ~str
}
//pub type LibTarget = Target;
#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
pub struct Project {
- name: ~str,
- version: ~str,
- authors: ~[~str]
+ pub name: ~str,
+ pub version: ~str,
+ pub authors: ~[~str]
}
pub type CargoResult<T> = Result<T, CargoError>;
}
}
-pub trait ToCargoError<T> {
- fn to_cargo_error(self, message: ~str, exit_code: uint) -> Result<T, CargoError>;
+pub trait ToCargoErrorMessage<E> {
+ fn to_cargo_error_message(self, error: E) -> ~str;
}
-impl<T,U> ToCargoError<T> for Result<T,U> {
- fn to_cargo_error(self, message: ~str, exit_code: uint) -> Result<T, CargoError> {
+impl<E> ToCargoErrorMessage<E> for ~str {
+ fn to_cargo_error_message(self, _: E) -> ~str {
+ self
+ }
+}
+
+impl<E> ToCargoErrorMessage<E> for 'static |E| -> ~str {
+ fn to_cargo_error_message(self, err: E) -> ~str {
+ self(err)
+ }
+}
+
+pub trait ToCargoError<T, E> {
+ fn to_cargo_error<M: ToCargoErrorMessage<E>>(self, to_message: M, exit_code: uint) -> Result<T, CargoError>;
+}
+
+impl<T,E> ToCargoError<T, E> for Result<T,E> {
+ fn to_cargo_error<M: ToCargoErrorMessage<E>>(self, to_message: M, exit_code: uint) -> Result<T, CargoError> {
match self {
- Err(_) => Err(CargoError{ message: message, exit_code: exit_code }),
+ Err(err) => Err(CargoError{ message: to_message.to_cargo_error_message(err), exit_code: exit_code }),
Ok(val) => Ok(val)
}
}
}
-impl<T> ToCargoError<T> for Option<T> {
- fn to_cargo_error(self, message: ~str, exit_code: uint) -> CargoResult<T> {
+impl<T> ToCargoError<T, Option<T>> for Option<T> {
+ fn to_cargo_error<M: ToCargoErrorMessage<Option<T>>>(self, to_message: M, exit_code: uint) -> Result<T, CargoError> {
match self {
- None => Err(CargoError{ message: message, exit_code: exit_code }),
+ None => Err(CargoError{ message: to_message.to_cargo_error_message(None), exit_code: exit_code }),
Some(val) => Ok(val)
}
}
}
-trait RepresentsFlags : FlagConfig + Decodable<FlagDecoder> {}
-impl<T: FlagConfig + Decodable<FlagDecoder>> RepresentsFlags for T {}
+trait RepresentsFlags : FlagConfig + Decodable<FlagDecoder, HammerError> {}
+impl<T: FlagConfig + Decodable<FlagDecoder, HammerError>> RepresentsFlags for T {}
-trait RepresentsJSON : Decodable<json::Decoder> {}
-impl <T: Decodable<json::Decoder>> RepresentsJSON for T {}
+trait RepresentsJSON : Decodable<json::Decoder, json::Error> {}
+impl <T: Decodable<json::Decoder, json::Error>> RepresentsJSON for T {}
#[deriving(Decodable)]
pub struct NoFlags;
impl FlagConfig for NoFlags {}
-pub fn execute_main<'a, T: RepresentsFlags, U: RepresentsJSON, V: Encodable<json::Encoder<'a>>>(exec: fn(T, U) -> CargoResult<Option<V>>) {
- fn call<'a, T: RepresentsFlags, U: RepresentsJSON, V: Encodable<json::Encoder<'a>>>(exec: fn(T, U) -> CargoResult<Option<V>>) -> CargoResult<Option<V>> {
+pub fn execute_main<'a, T: RepresentsFlags, U: RepresentsJSON, V: Encodable<json::Encoder<'a>, io::IoError>>(exec: fn(T, U) -> CargoResult<Option<V>>) {
+ fn call<'a, T: RepresentsFlags, U: RepresentsJSON, V: Encodable<json::Encoder<'a>, io::IoError>>(exec: fn(T, U) -> CargoResult<Option<V>>) -> CargoResult<Option<V>> {
let flags = try!(flags_from_args::<T>());
let json = try!(json_from_stdin::<U>());
process_executed(call(exec))
}
-pub fn execute_main_without_stdin<'a, T: RepresentsFlags, V: Encodable<json::Encoder<'a>>>(exec: fn(T) -> CargoResult<Option<V>>) {
- fn call<'a, T: RepresentsFlags, V: Encodable<json::Encoder<'a>>>(exec: fn(T) -> CargoResult<Option<V>>) -> CargoResult<Option<V>> {
+pub fn execute_main_without_stdin<'a, T: RepresentsFlags, V: Encodable<json::Encoder<'a>, io::IoError>>(exec: fn(T) -> CargoResult<Option<V>>) {
+ fn call<'a, T: RepresentsFlags, V: Encodable<json::Encoder<'a>, io::IoError>>(exec: fn(T) -> CargoResult<Option<V>>) -> CargoResult<Option<V>> {
let flags = try!(flags_from_args::<T>());
exec(flags)
process_executed(call(exec))
}
-fn process_executed<'a, T: Encodable<json::Encoder<'a>>>(result: CargoResult<Option<T>>) {
+fn process_executed<'a, T: Encodable<json::Encoder<'a>, io::IoError>>(result: CargoResult<Option<T>>) {
match result {
Err(e) => {
let _ = write!(&mut std::io::stderr(), "{}", e.message);
fn flags_from_args<T: RepresentsFlags>() -> CargoResult<T> {
let mut decoder = FlagDecoder::new::<T>(std::os::args().tail());
- let flags: T = Decodable::decode(&mut decoder);
-
- match decoder.error {
- Some(err) => Err(CargoError::new(err, 1)),
- None => Ok(flags)
- }
+ Decodable::decode(&mut decoder).to_cargo_error(|e: HammerError| e.message, 1)
}
fn json_from_stdin<T: RepresentsJSON>() -> CargoResult<T> {
let json = try!(json::from_str(input).to_cargo_error(format!("Cannot parse json: {}", input), 1));
let mut decoder = json::Decoder::new(json);
- Ok(Decodable::decode(&mut decoder))
+ Decodable::decode(&mut decoder).to_cargo_error(|e: json::Error| format!("{}", e), 1)
}