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

use frunk::Validated;
use frunk::prelude::*;
use frunk_core::{HList, hlist_pat};

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

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

fn get_age() -> Result<i32, String> {
    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,
                        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