cleanup .chain and .chain_err + fixing other files

This commit is contained in:
maikklein 2013-07-23 02:27:53 +02:00 committed by Erick Tryzelaar
parent e308167a2f
commit f6bcf5d5f1
4 changed files with 54 additions and 87 deletions

View File

@ -147,7 +147,7 @@ pub fn get_rustpkg_root() -> Result<Path, ~str> {
}
pub fn get_rustpkg_root_nearest() -> Result<Path, ~str> {
do result::chain(get_rustpkg_root()) |p| {
do get_rustpkg_root().chain |p| {
let cwd = os::getcwd();
let cwd_rustpkg = cwd.push(".rustpkg");
let rustpkg_is_non_root_file =
@ -173,13 +173,13 @@ pub fn get_rustpkg_root_nearest() -> Result<Path, ~str> {
}
fn get_rustpkg_lib_path() -> Result<Path, ~str> {
do result::chain(get_rustpkg_root()) |p| {
do get_rustpkg_root().chain |p| {
result::Ok(p.push(libdir()))
}
}
fn get_rustpkg_lib_path_nearest() -> Result<Path, ~str> {
do result::chain(get_rustpkg_root_nearest()) |p| {
do get_rustpkg_root_nearest().chain |p| {
result::Ok(p.push(libdir()))
}
}

View File

@ -138,7 +138,7 @@ fn config_from_opts(
let config = default_config(input_crate);
let result = result::Ok(config);
let result = do result::chain(result) |config| {
let result = do result.chain |config| {
let output_dir = getopts::opt_maybe_str(matches, opt_output_dir());
let output_dir = output_dir.map(|s| Path(*s));
result::Ok(Config {
@ -146,14 +146,10 @@ fn config_from_opts(
.. config
})
};
let result = do result::chain(result) |config| {
let output_format = getopts::opt_maybe_str(
matches, opt_output_format());
do output_format.map_default(result::Ok(config.clone()))
|output_format| {
do result::chain(parse_output_format(*output_format))
|output_format| {
let result = do result.chain |config| {
let output_format = getopts::opt_maybe_str(matches, opt_output_format());
do output_format.map_default(result::Ok(config.clone())) |output_format| {
do parse_output_format(*output_format).chain |output_format| {
result::Ok(Config {
output_format: output_format,
.. config.clone()
@ -161,13 +157,11 @@ fn config_from_opts(
}
}
};
let result = do result::chain(result) |config| {
let result = do result.chain |config| {
let output_style =
getopts::opt_maybe_str(matches, opt_output_style());
do output_style.map_default(result::Ok(config.clone()))
|output_style| {
do result::chain(parse_output_style(*output_style))
|output_style| {
do output_style.map_default(result::Ok(config.clone())) |output_style| {
do parse_output_style(*output_style).chain |output_style| {
result::Ok(Config {
output_style: output_style,
.. config.clone()
@ -176,11 +170,11 @@ fn config_from_opts(
}
};
let process_output = Cell::new(process_output);
let result = do result::chain(result) |config| {
let result = do result.chain |config| {
let pandoc_cmd = getopts::opt_maybe_str(matches, opt_pandoc_cmd());
let pandoc_cmd = maybe_find_pandoc(
&config, pandoc_cmd, process_output.take());
do result::chain(pandoc_cmd) |pandoc_cmd| {
do pandoc_cmd.chain |pandoc_cmd| {
result::Ok(Config {
pandoc_cmd: pandoc_cmd,
.. config.clone()

View File

@ -1726,21 +1726,21 @@ pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) ->
}
pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> {
result::chain(read_whole_file(file), |bytes| {
do read_whole_file(file).chain |bytes| {
if str::is_utf8(bytes) {
result::Ok(str::from_bytes(bytes))
} else {
result::Err(file.to_str() + " is not UTF-8")
}
})
}
}
// FIXME (#2004): implement this in a low-level way. Going through the
// abstractions is pointless.
pub fn read_whole_file(file: &Path) -> Result<~[u8], ~str> {
result::chain(file_reader(file), |rdr| {
do file_reader(file).chain |rdr| {
result::Ok(rdr.read_whole_stream())
})
}
}
// fsync related
@ -1851,10 +1851,10 @@ mod tests {
~"A hoopy frood who really knows where his towel is.";
debug!(frood.clone());
{
let out: @io::Writer = io::file_writer(tmpfile, [io::Create, io::Truncate]).unwrap();
let out = io::file_writer(tmpfile, [io::Create, io::Truncate]).unwrap();
out.write_str(frood);
}
let inp: @io::Reader = io::file_reader(tmpfile).unwrap();
let inp = io::file_reader(tmpfile).unwrap();
let frood2: ~str = inp.read_c_str();
debug!(frood2.clone());
assert_eq!(frood, frood2);

View File

@ -77,61 +77,6 @@ pub fn to_either<T:Clone,U:Clone>(res: &Result<U, T>)
}
}
/**
* Call a function based on a previous result
*
* If `res` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `res` is `err` then it is immediately
* returned. This function can be used to compose the results of two
* functions.
*
* Example:
*
* let res = chain(read_file(file)) { |buf|
* ok(parse_bytes(buf))
* }
*/
#[inline]
pub fn chain<T, U, V>(res: Result<T, V>, op: &fn(T)
-> Result<U, V>) -> Result<U, V> {
match res {
Ok(t) => op(t),
Err(e) => Err(e)
}
}
/**
* Call a function based on a previous result
*
* If `res` is `err` then the value is extracted and passed to `op`
* whereupon `op`s result is returned. if `res` is `ok` then it is
* immediately returned. This function can be used to pass through a
* successful result while handling an error.
*/
#[inline]
pub fn chain_err<T, U, V>(
res: Result<T, V>,
op: &fn(t: V) -> Result<T, U>)
-> Result<T, U> {
match res {
Ok(t) => Ok(t),
Err(v) => op(v)
}
}
/**
* Call a function based on a previous result
*
* If `res` is `err` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `res` is `ok` then it is immediately returned.
* This function can be used to pass through a successful result while
* handling an error.
*/
/**
* Call a function based on a previous result
*
@ -208,7 +153,7 @@ impl<T, E> Result<T, E> {
* Call a function based on a previous result
*
* If `*self` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `res` is `err` then it is immediately
* `op`s result is returned. if `*self` is `err` then it is immediately
* returned. This function can be used to compose the results of two
* functions.
*
@ -230,7 +175,7 @@ impl<T, E> Result<T, E> {
* Call a function based on a previous result
*
* If `*self` is `err` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `res` is `ok` then it is immediately returned.
* `op`s result is returned. if `*self` is `ok` then it is immediately returned.
* This function can be used to pass through a successful result while
* handling an error.
*/
@ -260,14 +205,42 @@ impl<T, E> Result<T, E> {
}
}
/**
* Call a function based on a previous result
*
* If `self` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `self` is `err` then it is immediately
* returned. This function can be used to compose the results of two
* functions.
*
* Example:
*
* let res = read_file(file).chain(op) { |buf|
* ok(parse_bytes(buf))
* }
*/
#[inline]
pub fn chain<U>(self, op: &fn(T) -> Result<U,E>) -> Result<U,E> {
chain(self, op)
match self {
Ok(t) => op(t),
Err(e) => Err(e)
}
}
/**
* Call a function based on a previous result
*
* If `self` is `err` then the value is extracted and passed to `op`
* whereupon `op`s result is returned. if `self` is `ok` then it is
* immediately returned. This function can be used to pass through a
* successful result while handling an error.
*/
#[inline]
pub fn chain_err<F>(self, op: &fn(E) -> Result<T,F>) -> Result<T,F> {
chain_err(self, op)
match self {
Ok(t) => Ok(t),
Err(v) => op(v)
}
}
}
@ -390,7 +363,7 @@ pub fn iter_vec2<S,T,U>(ss: &[S], ts: &[T],
#[cfg(test)]
mod tests {
use result::{Err, Ok, Result, chain, get, get_err};
use result::{Err, Ok, Result, get, get_err};
use result;
pub fn op1() -> result::Result<int, ~str> { result::Ok(666) }
@ -403,12 +376,12 @@ mod tests {
#[test]
pub fn chain_success() {
assert_eq!(get(&chain(op1(), op2)), 667u);
assert_eq!(get(&(op1().chain(op2))), 667u);
}
#[test]
pub fn chain_failure() {
assert_eq!(get_err(&chain(op3(), op2)), ~"sadface");
assert_eq!(get_err(&op3().chain( op2)), ~"sadface");
}
#[test]