1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! # fuel_indexer_lib
//!
//! A collection of utilities used by the various `fuel-indexer-*` crates.

#![deny(unused_crate_dependencies)]
pub mod config;
pub mod defaults;
pub mod graphql;
pub mod manifest;
pub mod utils;

use proc_macro2::TokenStream;
use quote::quote;
use sha2::{Digest, Sha256};

/// Max size of Postgres array types.
pub const MAX_ARRAY_LENGTH: usize = 2500;

/// The source of execution for the indexer.
#[derive(Default, Clone, Debug)]
pub enum ExecutionSource {
    /// The indexer is being executed as a standalone binary.
    Native,

    /// The indexer is being executed in a WASM runtime.
    #[default]
    Wasm,
}

impl ExecutionSource {
    pub fn async_awaitness(&self) -> (TokenStream, TokenStream) {
        match self {
            Self::Native => (quote! {async}, quote! {.await}),
            Self::Wasm => (quote! {}, quote! {}),
        }
    }
}

/// Derive a type ID from a namespace and given abstraction name.
pub fn type_id(namespace: &str, name: &str) -> i64 {
    // IMPORTANT: https://github.com/launchbadge/sqlx/issues/499
    let mut bytes = [0u8; 8];
    let digest = Sha256::digest(format!("{name}:{namespace}").as_bytes());
    bytes[..8].copy_from_slice(&digest[..8]);
    i64::from_be_bytes(bytes)
}

/// Return a fully qualified indexer namespace.
pub fn fully_qualified_namespace(namespace: &str, identifier: &str) -> String {
    format!("{}_{}", namespace, identifier)
}