Crates.io | fluid-macro |
lib.rs | fluid-macro |
version | 0.3.1 |
source | src |
created_at | 2023-01-27 18:55:58.087051 |
updated_at | 2023-01-27 20:13:11.98322 |
description | Write long method chains as a series of steps instead, and more! |
homepage | |
repository | https://github.com/Narvius/fluid-macro |
max_upload_size | |
id | 769846 |
size | 7,825 |
A macro that allows you to write long method call chains as a series of steps instead, with support for sub-scopes.
let x = fluid!("123", {
parse::<i32>();
unwrap_or_default();
[- 100];
[* 2];
clamp(5, 100);
to_string();
});
This is equivalent to writing:
let x = (("123".parse::<i32>().unwrap_or_default() - 100) * 2).clamp(5, 100).to_string();
I was working on a little Visual Novel-like module for a game, with a DSL. I ended up creating a bunch of builders that modeled that DSL:
SceneBuilder::new().with_character(Cid::Player, |b| {
b.message("Oh, what's this, a loose brick?")
.choice("Should I push it?", |b| {
b.branch("Yes", |b| { /* omitted */ })
.branch("No", |b| { /* omitted */ })
})
})
While the overall structure is fine, there's a lot of line noise--periods, |b|
s, etc. Not to mention that the otherwise-lovely rustfmt
really struggles with the nested builders. Thus I wrote this macro to be able to write this instead:
fluid!(SceneBuilder::new(), {
with_character(Cid::Player) {
message("Oh, what's this, a loose brick?");
choice("Should I push it?") {
branch("Yes") {
/* omitted */
}
branch("No") {
/* omitted */
}
}
}
})
It's not very friendly to the IDE whilst writing. You will have to already know the names of methods you want to use. After compilation, however, symbol lookup and the like works fine.