fallback-if

Crates.iofallback-if
lib.rsfallback-if
version1.0.1
sourcesrc
created_at2024-05-15 15:45:38.336652
updated_at2024-05-15 15:59:15.682948
descriptionFall back to an alternative given some predicate
homepage
repositoryhttps://github.com/foresterre/fallback-if
max_upload_size
id1241196
size24,700
Martijn Gribnau (foresterre)

documentation

README

fallback-if

Fallback to an alternative, given that the initial result is considered a fail and the predicate evaluates to true.

Example

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))
}

License

Licensed under either of

at your option.

Contribution

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.

Commit count: 3

cargo fmt