sum_error

Crates.iosum_error
lib.rssum_error
version0.1.2
sourcesrc
created_at2019-11-03 03:41:27.938186
updated_at2019-11-03 07:03:18.542104
descriptionDerive macros for fast summing of error types into error enum.
homepagehttps://github.com/alexander-bzikadze/sum_error
repositoryhttps://github.com/alexander-bzikadze/sum_error
max_upload_size
id177661
size17,139
Alexander Bzikadze (alexander-bzikadze)

documentation

README

Sum error

The library is ment to ease coding functions with try calls (or with the ? operator) and provides a derive macro to easily sum errors into a enum that automaticaly derives all the required traits including std::error::Error and std::convert::From from all the contained error types.

In order to use this functionality create a enum containing unnamed variants with a single error type. Then use the derive macro. And now it is done!

Example

To better illustrate described functionality consider the following example.

use std::fs::File;
use std::rc::Rc;
use sprite::Sprite;
use piston_window::Texture;
use image::gif::Decoder;
use image::AnimationDecoder;
use piston_window::{TextureContext, TextureSettings};
use sum_error::*;

/// Load a gif sprite.
pub fn load<F: gfx::Factory<R>,
            R: gfx::Resources,
            C: gfx::CommandBuffer<R>>(ctx: &mut TextureContext<F, R, C>)
                -> Result<Vec<Sprite<Texture<R>>>, CombineError> {
    let file = File::open("file.gif")?;
    let decoder = Decoder::new(file)?;
    let frames = decoder.into_frames().collect_frames()?;
    frames.iter()
        .map(|frame| {
            Texture::from_image(ctx, frame.buffer(), &TextureSettings::new())
                .map(Rc::new)
                .map(Sprite::from_texture)
                .map_err(|e| e.into())
        }).collect()
}

#[derive(SumError)]
pub enum CombineError {
    FileError(std::io::Error),
    ImageError(image::ImageError),
    GfxError(gfx_texture::Error),
}

Using SumError derive macro allows us to effectively describe summing error type with standard tools and then don't waste precious time on writing tons of traits' implementations in order to use it conviniently.

Requirments for the enum

  • First of all, it should be a enum -- not a struct, not a union. Enum
  • Variants should contain only one unnamed field
  • Types of the fields should implement std::error::Error
  • Types of the fields should be unique inside the enum scope (as otherwise std::convert::From trait would be impossible to imlement)

Alternatives

If you do not need the std::error::Error functionality for your summing class, consider using sum_type crate. It does provide functionality to easily sum types and implement std::convert::From traits. However, beware as it does not use cute derive syntax but raw macro by design.

Contribution

Feel free to raise issues, demand more functionality and propose any enhancements. You can use github tools for that or address the authors directly via email.

Authors

Commit count: 8

cargo fmt