4个版本 (2个破坏性更新)
| 0.3.1 | 2023年1月27日 | 
|---|---|
| 0.3.0 | 2023年1月27日 | 
| 0.2.0 | 2023年1月27日 | 
| 0.1.0 | 2023年1月27日 | 
#1864 in Rust模式
7KB
fluid-macros
这是一个宏,允许你将长方法调用链写成一系列步骤,支持子作用域。
基本用法
let x = fluid!("123", {
    parse::<i32>();
    unwrap_or_default();
    [- 100];
    [* 2];
    clamp(5, 100);
    to_string();
});
这相当于写入
let x = (("123".parse::<i32>().unwrap_or_default() - 100) * 2).clamp(5, 100).to_string();
(启发) 示例
我正在为一个小型视觉小说模块编写一个游戏,其中包含一个领域特定语言(dsl)。我最终创建了一组构建器来模拟该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 */ })
        })
})
尽管整体结构良好,但有很多行噪音--句点、|b|等。更不用说,其他方面非常漂亮的rustfmt在嵌套构建器方面真的很挣扎。因此,我编写了这个宏,以便能够这样编写
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 */
            }
        }
    }
})
已知限制
在编写时对IDE不太友好。您必须已经知道您想要使用的方法的名称。然而,在编译后,符号查找等工作运行良好。