fallible_map 0.1.1

Utilities for fallible mapping over `Option` and iterators using functions that can return `Result`s.
Documentation
  • Coverage
  • 81.82%
    9 out of 11 items documented0 out of 10 items with examples
  • Size
  • Source code size: 14.76 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.4 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • tifaremosapere/fallible_map
    5 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Emulator000

Fallible Map

Crates.io License: MIT Version Repository Homepage

fallible_map provides utilities for fallible mapping over Option types and iterators, allowing the use of functions that can return Results.

This library includes traits to enhance Option and Iterator types with methods to handle fallible operations gracefully.

Overview

This crate offers extensions for optional values and iterators to perform fallible mapping operations, returning results that properly reflect potential errors during computation.

These extensions can be useful in scenarios where operations may fail, and error handling is required.

Features

  • ExtractOption trait: A helper trait to extract the inner value of an optional container;
  • FallibleMapExt trait: Extends Option with methods for fallible operations, such as try_map, try_unwrap_or, and try_and_then;
  • FallibleMapIteratorExt trait: Extends iterators with a try_map method, allowing the use of functions that return Results during iteration, providing an iterator adaptor for seamless chaining and collection.

Installation

Add this to your Cargo.toml:

[dependencies]
fallible_map = "^0.1"

Usage

Examples

Using FallibleMapExt with Option

use fallible_map::FallibleMapExt;

fn main() -> Result<(), String> {
    let some_number: Option<i32> = Some(2);

    let result: Result<Option<i32>, String> = some_number.try_map(|num| {
        if num % 2 == 0 {
            Ok(num * 2)
        } else {
            Err("Odd number".to_string())
        }
    });

    assert_eq!(result, Ok(Some(4)));

    Ok(())
}

Using FallibleMapExt with try_and_then

use fallible_map::FallibleMapExt;

fn main() -> Result<(), String> {
    let some_number: Option<i32> = Some(2);

    let result: Result<Option<i32>, String> = some_number.try_and_then(|num| {
        if num % 2 == 0 {
            Ok(Some(num * 2))
        } else {
            Err("Odd number".to_string())
        }
    });

    assert_eq!(result, Ok(Some(4)));

    let none_number: Option<i32> = None;

    let result = none_number.try_and_then(|num| {
        if num % 2 == 0 {
            Ok(Some(num * 2))
        } else {
            Err("Odd number".to_string())
        }
    });

    assert_eq!(result, Ok(None));

    Ok(())
}

Using FallibleMapIteratorExt with Iterator

use fallible_map::FallibleMapIteratorExt;

fn main() -> Result<(), String> {
    let numbers = vec![1, 2, 3, 4, 5];

    let mapped_numbers: Result<Vec<i32>, String> = numbers.into_iter().try_map(|x| {
        if x % 2 == 0 {
            Ok(x * 2)
        } else {
            Err(format!("Failed to process {}", x))
        }
    }).collect();

    match mapped_numbers {
        Ok(nums) => println!("Mapped successfully: {:?}", nums),
        Err(e) => println!("Error occurred: {}", e),
    }

    Ok(())
}

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contribution

Contributions are welcome! Please feel free to submit a pull request, open an issue, or suggest features and improvements.