2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "A type representing either success or failure"];
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "The result type"]
|
2012-03-13 16:39:28 -05:00
|
|
|
enum result<T, U> {
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Contains the successful result value"]
|
2012-01-19 21:08:08 -06:00
|
|
|
ok(T),
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Contains the error value"]
|
2012-01-19 21:29:21 -06:00
|
|
|
err(U)
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
2011-12-13 18:25:51 -06:00
|
|
|
Get the value out of a successful result
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
# Failure
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
If the result is an error
|
2012-03-06 21:09:32 -06:00
|
|
|
"]
|
2012-03-13 16:39:28 -05:00
|
|
|
fn get<T: copy, U>(res: result<T, U>) -> T {
|
2011-12-13 18:25:51 -06:00
|
|
|
alt res {
|
|
|
|
ok(t) { t }
|
|
|
|
err(_) {
|
|
|
|
// FIXME: Serialize the error value
|
2011-12-16 09:31:35 -06:00
|
|
|
// and include it in the fail message (maybe just note it)
|
2011-12-13 18:25:51 -06:00
|
|
|
fail "get called on error result";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
2011-12-13 18:25:51 -06:00
|
|
|
Get the value out of an error result
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
# Failure
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
If the result is not an error
|
2012-03-06 21:09:32 -06:00
|
|
|
"]
|
2012-03-13 16:39:28 -05:00
|
|
|
fn get_err<T, U: copy>(res: result<T, U>) -> U {
|
2011-12-13 18:25:51 -06:00
|
|
|
alt res {
|
|
|
|
err(u) { u }
|
|
|
|
ok(_) {
|
|
|
|
fail "get_error called on ok result";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Returns true if the result is `ok`"]
|
2012-03-13 16:39:28 -05:00
|
|
|
pure fn success<T, U>(res: result<T, U>) -> bool {
|
2011-12-13 18:25:51 -06:00
|
|
|
alt res {
|
|
|
|
ok(_) { true }
|
|
|
|
err(_) { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Returns true if the result is `error`"]
|
2012-03-13 16:39:28 -05:00
|
|
|
pure fn failure<T, U>(res: result<T, U>) -> bool {
|
2011-12-13 18:25:51 -06:00
|
|
|
!success(res)
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
|
|
|
Convert to the `either` type
|
|
|
|
|
|
|
|
`ok` result variants are converted to `either::right` variants, `err`
|
|
|
|
result variants are converted to `either::left`.
|
|
|
|
"]
|
2012-03-13 16:39:28 -05:00
|
|
|
pure fn to_either<T: copy, U: copy>(res: result<U, T>) -> either<T, U> {
|
2011-12-16 09:31:35 -06:00
|
|
|
alt res {
|
|
|
|
ok(res) { either::right(res) }
|
|
|
|
err(fail_) { either::left(fail_) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
2011-12-13 18:25:51 -06:00
|
|
|
Call a function based on a previous result
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
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.
|
2011-12-13 18:25:51 -06:00
|
|
|
This function can be used to compose the results of two functions.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
let res = chain(read_file(file)) { |buf|
|
|
|
|
ok(parse_buf(buf))
|
|
|
|
}
|
|
|
|
"]
|
2012-03-13 16:39:28 -05:00
|
|
|
fn chain<T, U: copy, V: copy>(res: result<T, V>, op: fn(T) -> result<U, V>)
|
|
|
|
-> result<U, V> {
|
2011-12-13 18:25:51 -06:00
|
|
|
alt res {
|
|
|
|
ok(t) { op(t) }
|
|
|
|
err(e) { err(e) }
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-03-13 16:39:28 -05:00
|
|
|
fn op1() -> result::result<int, str> { result::ok(666) }
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn op2(&&i: int) -> result::result<uint, str> {
|
|
|
|
result::ok(i as uint + 1u)
|
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn op3() -> result::result<int, str> { result::err("sadface") }
|
2012-01-17 19:28:21 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chain_success() {
|
|
|
|
assert get(chain(op1(), op2)) == 667u;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chain_failure() {
|
|
|
|
assert get_err(chain(op3(), op2)) == "sadface";
|
|
|
|
}
|
|
|
|
}
|