Crates.io | sum_error |
lib.rs | sum_error |
version | 0.1.2 |
source | src |
created_at | 2019-11-03 03:41:27.938186 |
updated_at | 2019-11-03 07:03:18.542104 |
description | Derive macros for fast summing of error types into error enum. |
homepage | https://github.com/alexander-bzikadze/sum_error |
repository | https://github.com/alexander-bzikadze/sum_error |
max_upload_size | |
id | 177661 |
size | 17,139 |
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!
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.
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.
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.