fluid-macros
A macro that allows you to write long method call chains as a series of steps instead, with support for sub-scopes.
Basic Usage
let x = fluid!;
This is equivalent to writing:
let x = Some.unwrap_or_default.clamp.to_string;
(Motivating) Example
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:
new.with_character
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!
Enabling nested blocks
Since the macro simply moves around the identifiers it is given, any method that matches after all transformations are done will automatically work. There is really only one assumption made: Blocks (like the choice call in the example above) are simply a a one-argument closure as a final argument. An example is given in multiplied below:
let b = fluid!;
assert_eq!;
It can, of course, be any closure type--not just impl FnOnce.
For completeness, the above macro invocation expands to the following:
let b = default
.add
.multiplied;
Known limitations
You can't turbofish. I'm sure this is a solveable issue, though.
let x = fluid!
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.