Crates.io | tweak |
lib.rs | tweak |
version | 0.1.1 |
source | src |
created_at | 2018-09-02 18:10:54.342887 |
updated_at | 2018-09-10 00:02:46.991147 |
description | when/then clauses to run |
homepage | https://github.com/zoer/tweak |
repository | https://github.com/zoer/tweak.git |
max_upload_size | |
id | 82651 |
size | 13,300 |
Tweak provides when/then clauses to your context in described state.
Add the following to your Cargo.toml
:
[dependencies]
tweak = "*"
Simple context manipulation.
extern crate tweak;
use tweak::Case;
struct XY {
x: i32,
y: i32,
}
impl XY {
fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
let mut xy = XY::new(5, 0);
let res = Case::<XY, &str>::new("coords")
.when("x > 0", |xy| Ok(xy.x > 0))
.then_case("tweak x", |case| {
case.when("x == 5", |xy| Ok(xy.x == 5))
.then("multiply x by 3", |xy| {
xy.x *= 3;
Ok(())
})
.when("when x > 10", |xy| Ok(xy.x > 10))
.then("set x to 10", |xy| {
xy.x = 10;
Ok(())
})
})
.when("y > 0", |xy| Ok(xy.y > 0))
.then_case("tweak y", |case| {
case.when("y > 0", |xy| Ok(xy.y > 0))
.then("divide 10 by y", |xy| {
xy.y = 10 / xy.y;
Ok(())
})
})
.run(&mut xy);
assert_eq!(Ok(true), res);
assert_eq!(xy.x, 10);
assert_eq!(xy.y, 0);
License: MIT