try_or-0.2.0/.gitignore 0000644 0000000 0000000 00000000007 12757340662 0013216 0 ustar 00 0000000 0000000 target
try_or-0.2.0/.travis.yml 0000644 0000000 0000000 00000000767 12757336763 0013363 0 ustar 00 0000000 0000000 language: rust
rust:
- stable
- beta
- nightly
matrix:
allow_failures:
- rust: nightly
after_success: |
[ $TRAVIS_BRANCH = master ] &&
[ $TRAVIS_PULL_REQUEST = false ] &&
cargo doc &&
echo "" > target/doc/index.html &&
sudo pip install ghp-import &&
ghp-import -n target/doc &&
git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages
try_or-0.2.0/Cargo.toml 0000644 0000000 0000000 00000001502 12760035762 0013153 0 ustar 00 0000000 0000000 # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies
#
# If you believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
name = "try_or"
version = "0.2.0"
authors = ["Georg Echterling "]
description = "Contains helper macros for unwrapping Results and Options."
documentation = "http://mixthos.github.com/try_or"
readme = "README.md"
license = "MIT"
repository = "https://github.com/Mixthos/try_or"
try_or-0.2.0/Cargo.toml.orig 0000644 0000000 0000000 00000000467 12760035762 0014123 0 ustar 00 0000000 0000000 [package]
name = "try_or"
version = "0.2.0"
authors = ["Georg Echterling "]
description = "Contains helper macros for unwrapping Results and Options."
readme = "README.md"
license = "MIT"
documentation = "http://mixthos.github.com/try_or"
repository = "https://github.com/Mixthos/try_or"
try_or-0.2.0/LICENSE 0000644 0000000 0000000 00000002066 12760035267 0012236 0 ustar 00 0000000 0000000 Copyright (c) 2016 Georg Echterling
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
try_or-0.2.0/README.md 0000644 0000000 0000000 00000002411 12760035600 0012471 0 ustar 00 0000000 0000000 # try_or [](https://travis-ci.org/Mixthos/try_or)
Contains the macros `try_or!`, `try_or_else!`, `try_opt_or!` and
`try_opt_or_else!`. These are helper macros for unwrapping a `Result` or an
`Option` while returning early for `Err` and `None` values. The semantics
are similar to `unwrap_or` and `unwrap_or_else`.
If you want a `try_opt!` macro, there's already another crate (`try_opt`)
for that.
## [Documentation](http://mixthos.github.com/try_or)
## Usage
Add to your Cargo.toml:
```toml
[dependencies]
try_or = "0.1"
```
Use the macros like this:
```rust
#[macro_use]
extern crate try_or;
fn main() {
// test try_or!
assert_eq!({ || try_or!("5".parse::(), 1) } (), 5);
assert_eq!({ || try_or!("a".parse::(), 1) } (), 1);
// test try_or_else!
assert_eq!({ || try_or_else!("1".parse::(), |_| 5) } (), 1);
assert_eq!({ || try_or_else!("a".parse::(), |_| 5) } (), 5);
// test try_opt_or!
assert_eq!({ || try_opt_or!(Some(1), 2) } (), 1);
assert_eq!({ || try_opt_or!(None, 2) } (), 2);
// test try_or_else!
assert_eq!({ || try_opt_or_else!(Some(1), || 2) } (), 1);
assert_eq!({ || try_opt_or_else!(None, || 2) } (), 2);
}
```
try_or-0.2.0/src/lib.rs 0000644 0000000 0000000 00000004441 12760034730 0013125 0 ustar 00 0000000 0000000 //! Contains the macros `try_or!`, `try_or_else!`, `try_opt_or!` and
//! `try_opt_or_else!`. These are helper macros for unwrapping a `Result` or an
//! `Option` while returning early for `Err` and `None` values. The semantics
//! are similar to `unwrap_or` and `unwrap_or_else`.
//!
//! If you want a `try_opt!` macro, there's already another crate (`try_opt`)
//! for that.
#![no_std]
/// Helper macro for unwrapping `Result` values. Returns early with the value
/// of the second parameter if the first parameter is `Err`.
#[macro_export]
macro_rules! try_or {
( $expr:expr , $or:expr ) => {{
match { $expr } {
Ok(res) => res,
Err(_) => { return { $or }; },
}
}}
}
/// Unwraps a `Result`. If the result is `Err`, calls the function `$or_fn` with
/// its value and returns early with its result.
#[macro_export]
macro_rules! try_or_else {
( $expr:expr , $or_fn:expr ) => {{
match { $expr } {
Ok(res) => res,
Err(err) => { return { $or_fn }(err); },
}
}}
}
/*/// Unwraps an `Option`, returning `None` if the option is `None`.
#[macro_export]
macro_rules! try_opt {
( $expr:expr ) => {{
match { $expr } {
Some(val) => val,
None => return None,
}
}}
}*/
/// Unwraps an `Option`, returning the second parameter if the option is `None`.
#[macro_export]
macro_rules! try_opt_or {
( $expr:expr , $or:expr ) => {{
match { $expr } {
Some(res) => res,
None => return { $or },
}
}}
}
/// Unwraps an `Option`. If the result is `None`, calls the function `$or_fn`
/// and returns its result.
#[macro_export]
macro_rules! try_opt_or_else {
( $expr:expr , $or_fn:expr ) => {{
match { $expr } {
Some(res) => res,
None => return { $or_fn }(),
}
}}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
// test try_or!
assert_eq!({ || try_or!("5".parse::(), 1) } (), 5);
assert_eq!({ || try_or!("a".parse::(), 1) } (), 1);
// test try_or_else!
assert_eq!({ || try_or_else!("1".parse::(), |_| 5) } (), 1);
assert_eq!({ || try_or_else!("a".parse::(), |_| 5) } (), 5);
// test try_opt_or!
assert_eq!({ || try_opt_or!(Some(1), 2) } (), 1);
assert_eq!({ || try_opt_or!(None, 2) } (), 2);
// test try_or_else!
assert_eq!({ || try_opt_or_else!(Some(1), || 2) } (), 1);
assert_eq!({ || try_opt_or_else!(None, || 2) } (), 2);
}
}