2个版本

0.8.0-beta.32023年11月22日
0.8.0-beta.22023年6月6日

1400过程宏 中排名

Download history 1/week @ 2024-05-15 4/week @ 2024-05-22 1/week @ 2024-05-29 3/week @ 2024-06-05 2/week @ 2024-06-12

84 每月下载量

MIT/Apache

6KB
65

NEAR Lake Context Derive

Lake Context Derive是一个Rust crate,它提供了一个推导宏,用于方便地实现near_lake_framework::LakeContextExt特质。这个特质有两个函数:execute_before_runexecute_after_run,分别在用户提供的索引器函数执行前后执行。

用法

可以通过在上下文结构上使用#[derive(LakeContext)]来利用Lake Context Derive宏。这个特质实现将促进不同上下文的组合。例如,要使用带有一些附加数据的ParentTransactionCache,可以定义一个如下的上下文:

use near_lake_parent_transaction_cache::ParentTransactionCache;

#[derive(LakeContext)]
struct MyContext {
  db_connection_string: String,
  parent_tx_cache: ParentTransactionCache,
}

实例化

您可以通过以下方式创建上下文的实例:

use near_lake_parent_transaction_cache::{ParentTransactionCacheBuilder};

let my_context = MyContext {
  db_connection_string: String::from("postgres://user:pass@host/db"),
  parent_tx_cache: ParentTransactionCacheBuilder::default().build().unwrap(),
};

用户索引器函数

这将简化您的索引器函数签名。现在只需要将上下文作为额外的参数

async fn handle_block(
    mut block: Block,
    ctx: &MyContext,
) -> anyhow::Result<()> {
  // body
}

Lake Context Derive将查找结构体中实现LakeContextExt的所有字段,并将它们的特质方法附加到顶层调用。对于execute_before_run,它是按升序进行的,对于execute_after_run,它是按降序进行的。

目的

Lake Context Derive crate的目的在于减轻Rust中上下文开发和使用的某些常见痛点。通过封装和标准化这些函数调用的处理,我们旨在创建一个更易于访问和用户友好的上下文实现方法。

合作

我们希望这款工具对Rust社区有所帮助,并期待看到它在不同项目中如何被应用。我们鼓励社区贡献,无论是通过分享您独特的上下文实现,还是通过提供反馈和建议,帮助我们持续改进Lake Context Derive。

依赖项

~250–690KB
~16K SLoC