Module frunk::validated

source ·
Expand description

Module for holding Validated logic

Validated is a way of running a bunch of operations that can go wrong (for example, functions returning Result<T, E>) and, in the case of one or more things going wrong, having all the errors returned to you all at once. In the case that everything went well, you get an HList of all your results.

Examples

#[macro_use]
extern crate frunk;

use frunk::Validated;
use frunk::prelude::*; // for .into_validated()

#[derive(PartialEq, Eq, Debug)]
struct Person {
    age: i32,
    name: String,
}

fn get_name() -> Result<String, String> {
    Result::Ok("James".to_owned())
}

fn get_age() -> Result<i32, String> {
    Result::Ok(32)
}

let v: Validated<Hlist!(String, i32), String> = get_name().into_validated() + get_age();
let person = v.into_result()
               .map(|hlist_pat!(name, age)| {
                    Person {
                        name: name,
                        age: age,
                    }
                });

 assert_eq!(person.unwrap(),
            Person {
                name: "James".to_owned(),
                age: 32,
            });
Run

Enums

A Validated is either an Ok holding an HList or an Err, holding a vector of collected errors.

Traits

Trait for “lifting” a given type into a Validated