Crates.io | fallback-if |
lib.rs | fallback-if |
version | 1.0.1 |
source | src |
created_at | 2024-05-15 15:45:38.336652 |
updated_at | 2024-05-15 15:59:15.682948 |
description | Fall back to an alternative given some predicate |
homepage | |
repository | https://github.com/foresterre/fallback-if |
max_upload_size | |
id | 1241196 |
size | 24,700 |
Fallback to an alternative, given that the initial result is considered a fail and the predicate evaluates to true.
fn main() {
struct Config {
fallback_to_local: bool,
}
#[derive(Debug, PartialEq)]
struct Manifest;
impl Manifest {
pub fn try_fetch_remote() -> Result<Self, ()> {
// Oh noes! failed to fetch manifest remotely
Err(())
}
pub fn try_fetch_local() -> Result<Self, ()> {
// Yesss! Fetched locally!
Ok(Manifest)
}
}
let config = Config { fallback_to_local: true };
let result = Manifest::try_fetch_remote();
let outcome = result.fallback_if(config.fallback_to_local, || {
Manifest::try_fetch_local()
});
assert_eq!(outcome, Ok(Manifest))
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.