2 releases

0.8.0-beta.3 Nov 22, 2023
0.8.0-beta.2 Jun 6, 2023

#966 in Procedural macros

Download history 5/week @ 2024-02-09 62/week @ 2024-02-16 19/week @ 2024-02-23 15/week @ 2024-03-01 7/week @ 2024-03-08 6/week @ 2024-03-15 4/week @ 2024-03-22 40/week @ 2024-03-29 14/week @ 2024-04-05

58 downloads per month

MIT/Apache

6KB
65 lines

NEAR Lake Context Derive

Lake Context Derive is a Rust crate that provides a derive macro for easy and convenient implementation of the near_lake_framework::LakeContextExt trait. This trait has two functions: execute_before_run and execute_after_run that are executed before and after the user-provided indexer function respectively.

Usage

The Lake Context Derive macro can be utilized by annotating the context struct with #[derive(LakeContext)]. This trait implementation will then facilitate the combination of different contexts. For instance, to use a ParentTransactionCache with some additional data, one would define a context like:

use near_lake_parent_transaction_cache::ParentTransactionCache;

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

Instantiation

You can create an instance of your context as follows:

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(),
};

User Indexer Function

This will simplify your indexer function signature. It now needs only the context as an additional parameter:

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

The Lake Context Derive will look for all fields in the struct that implement LakeContextExt, and will append their trait methods to the top-level calls. For execute_before_run, it is done in ascending order, and for execute_after_run in descending order.

Purpose

The purpose of the Lake Context Derive crate is to alleviate some of the common pain points in context development and usage in Rust. By encapsulating and standardizing the handling of these function calls, we aim to create a more accessible and user-friendly approach to context implementation.

Collaboration

We hope that this tool will be useful for the Rust community and look forward to seeing how it can be used in a range of different projects. We encourage community contributions, whether that's through sharing your own unique context implementations or by providing feedback and suggestions for how we can continue to improve the Lake Context Derive.

Dependencies

~310–760KB
~18K SLoC