[][src]Crate asserter

Assertion library for rust.

In a function annotated wit assterer, you can use some macros.

unwrap!(foo as Pat)

use asserter::*;

#[asserter]
fn main () {
    let foo = Some("example");

    // If there's no issue with rustfmt, use `as` like the code below.
    unwrap!(foo as Some(s));
    assert_eq!(s, "example");
}

Sometimes, rustfmt does not work for the default syntax. There's an alternative syntax to help rustfmt.

use asserter::*;
enum Complex {
    Struct { foo: usize, bar: usize },
}

#[asserter]
fn main () {
    let foo = Complex::Struct { foo: 1, bar: 2 };

    // This syntax allows formatting
    unwrap!(foo, Complex::Struct { foo, bar });
}

Note that you can use box_patterns inside unwrap!() (on stable).

use asserter::*;

enum Complex {
    Normal(usize),
    Boxed(Box<Complex>),
}

#[asserter]
fn main () {
    let foo = Complex::Boxed(Box::new(Complex::Normal(0)));

    unwrap!(foo as Complex::Boxed(box Complex::Normal(v)));
    assert_eq!(v, 0);
}

unbox!(Pat)

If you want rustfmt to work correctly while unwrapping box, there's a helper for it.

use asserter::*;

enum Complex {
    Normal(usize),
    Boxed(Box<Complex>),
}

#[asserter]
fn main () {
    let foo = Complex::Boxed(Box::new(Complex::Normal(0)));

    unwrap!(foo as Complex::Boxed(unbox!(Complex::Normal(v))));
    assert_eq!(v, 0);
}

Attribute Macros

asserter