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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! # fuel_indexer_lib
//!
//! A collection of utilities used by the various `fuel-indexer-*` crates.

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

use proc_macro2::TokenStream;
use quote::quote;

/// 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(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum WasmIndexerError {
    DeserializationError = 0,
    SerializationError,
    PutObjectError,
    UnableToSaveListType,
    UninitializedMemory,
    KillSwitch,
    DatabaseError,
    MissingBlocksError,
    GeneralError,
}

impl From<u32> for WasmIndexerError {
    fn from(value: u32) -> Self {
        match value {
            0 => Self::DeserializationError,
            1 => Self::SerializationError,
            2 => Self::PutObjectError,
            3 => Self::UnableToSaveListType,
            4 => Self::UninitializedMemory,
            5 => Self::KillSwitch,
            6 => Self::DatabaseError,
            7 => Self::MissingBlocksError,
            _ => Self::GeneralError,
        }
    }
}

impl std::fmt::Display for WasmIndexerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SerializationError => {
                write!(f, "Failed to serialize object.")
            }
            Self::DeserializationError => {
                write!(f, "Failed to deserialize object.")
            }
            Self::UnableToSaveListType => {
                write!(f, "Failed to save list")
            }
            Self::PutObjectError => {
                write!(f, "Failed to save object")
            }
            Self::UninitializedMemory => {
                write!(f, "Failed to create MemoryView for indexer")
            }
            Self::KillSwitch => {
                write!(
                    f,
                    "Indexer kill switch has been triggered. Indexer will halt."
                )
            }
            Self::DatabaseError => {
                write!(f, "Failed performing a database operation")
            }
            Self::MissingBlocksError => {
                write!(f, "Some blocks are missing")
            }
            Self::GeneralError => write!(f, "Some unspecified WASM error occurred."),
        }
    }
}

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

/// Return the name of the join table for the given entities.
pub fn join_table_name(a: &str, b: &str) -> String {
    format!("{}s_{}s", a, b)
}

/// Return the name of each TypeDefinition in the join table.
pub fn join_table_typedefs_name(join_table_name: &str) -> (String, String) {
    let mut parts = join_table_name.split('_');
    let a = parts.next().unwrap();
    let b = parts.next().unwrap();

    // Trim the plural 's' from the end of the TypeDefinition name.
    (a[0..a.len() - 1].to_string(), b[0..b.len() - 1].to_string())
}