[][src]Crate cex

Combinators for EnumX in general, or Combinators for Error eXchange in error-handling.

Example

use enumx_derive::EnumX;
use enumx::prelude::*;

use cex_derive::{cex,Logger};
use cex::*;

#[derive( EnumX, Logger, Debug )]
enum ReadU32Error {
    IO(    Log<std::io::Error> ),
    Parse( Log<std::num::ParseIntError> ),
}
 
#[cex(to_log)]
fn read_u32( filename: &'static str )
    -> Result<u32, ReadU32Error>
{
    use std::io::Read;
 
    let mut f = std::fs::File::open( filename )?;
    let mut s = String::new();
    f.read_to_string( &mut s )?;
    let number = s.trim().parse::<u32>()?;
    Ok( number )
}
 
#[derive( Debug, PartialEq, Eq )]
struct MulOverflow( u32, u32 );
 
#[derive( EnumX, Logger, Debug )]
enum AMulBEqCError {
    IO(       Log<std::io::Error> ),
    Parse(    Log<std::num::ParseIntError> ),
    Overflow( Log<MulOverflow> ),
}
 
#[cex(log)]
fn a_mul_b_eq_c( file_a: &'static str, file_b: &'static str, file_c: &'static str )
    -> Result<bool, AMulBEqCError>
{
    let a = read_u32( file_a )?;
 
    let b = match read_u32( file_b ) {
        Ok(  value ) => value,
        Err( err ) => {
            if a == 0 {
                0 // 0 * b == 0, no matter what b is.
            } else {
                return err.error();
            }
        },
    };
  
    let c = match read_u32( file_c ) {
        Ok(  value ) => value,
        Err( err   ) => match err {
            ReadU32Error::IO(    _ ) => 0, // default to 0 if file is missing.
            ReadU32Error::Parse( e ) => return e.error(),
        },
    };
 
    Ok( a.checked_mul( b )
        .ok_or( MulOverflow(a,b) )
        .map( |result| result == c )
        .map_err_to_log( frame!() )
    ? )
}

Re-exports

pub use self::log::*;

Modules

log

Extrusive logging and backtrace support

Macros

frame

A macro to generate a Frame, to store the source of the error with file name, module path, line/column numbers, and an optional context info, using the same syntax with format!().

Enums

Enum0

Never type

Enum1
Enum2
Enum3
Enum4
Enum5
Enum6
Enum7
Enum8
Enum9
Enum10
Enum11
Enum12
Enum13
Enum14
Enum15
Enum16

Traits

EnumX

Indicates the prototype for a user-defined exchangeable enum.

EnumxFrom

Constructs an exchangeable enum from one of its variant type, or from another enum.

Error

Enum exchange to wrap an Err.

ExchangeFrom

Constructs an exchangeable enum from another one.

ExchangeInto

Converts an exchangeable enum into another one.

FromVariant

Constructs an enum from one of its variant type.

IntoEnum

Converts an exchangeable enum into another one.

IntoEnumx

Converts a variant, or an exchangeable enum into another exchangeable enum.

MapError

Enum exchange for Err combinator.

MapOkey

Enum exchange for Ok combinator.

Okey

Enum exchange to wrap an Ok.