fallback-if 1.0.1

Fall back to an alternative given some predicate
Documentation
  • Coverage
  • 66.67%
    2 out of 3 items documented0 out of 2 items with examples
  • Size
  • Source code size: 25.77 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.16 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • foresterre/fallback-if
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • foresterre

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.