formats.rs

Overview

This file defines the BigIntFormat enumeration, which specifies the string representation formats for big integers in the context of GraphQL responses. Because GraphQL has limitations with returning large numeric values directly, big integers are serialized as strings. The BigIntFormat enum provides options to control how these big integers are formatted as strings when sent over GraphQL.

Enum: BigIntFormat

#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum BigIntFormat {
    HEX,
    DEC,
}

Description

BigIntFormat is an enum annotated with async_graphql::Enum to integrate with GraphQL schema definitions. It allows specifying the string format to be used when serializing big integers for GraphQL responses.

Variants

Attributes and Derives

Usage Example

use async_graphql::{Enum, SimpleObject};

#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum BigIntFormat {
    HEX,
    DEC,
}

#[derive(SimpleObject)]
struct Query {
    #[graphql(format = "bigint_format")]
    big_number: String,
}

fn format_big_int(value: &num_bigint::BigInt, format: BigIntFormat) -> String {
    match format {
        BigIntFormat::HEX => format!("0x{:x}", value),
        BigIntFormat::DEC => value.to_string(),
    }
}

In this example, the format_big_int function converts a big integer into either hexadecimal or decimal string based on the BigIntFormat enum passed.

Implementation Details

Interaction with Other Parts of the System


Mermaid Diagram

classDiagram
class BigIntFormat {
<<Enum>>
+HEX
+DEC
}

This diagram shows the BigIntFormat enum with its two variants, representing the available string formatting options for big integers.