currency.rs
Overview
The currency.rs file defines the OtherCurrency data structure, which represents a currency amount with optional numeric and string representations. It provides functionality for formatting the currency value into different big integer formats asynchronously. This file leverages the async_graphql framework to expose OtherCurrency as a GraphQL object with complex fields, facilitating its use in GraphQL APIs.
Data Structures and Types
OtherCurrency
OtherCurrency is a GraphQL object that encapsulates a currency amount in two forms:
currency: An optional floating-point number (Option<f64>) representing the currency value in a numeric format.value: An optional string (Option<String>) representing the raw or formatted currency value. This field is hidden from the GraphQL schema (#[graphql(skip)]).
The struct is annotated with:
SimpleObject: Automatically generates GraphQL object type with fields exposed except those marked to skip.Cloneand Debug traits: Allow cloning and debugging of instances.graphql(complex): Enables defining additional complex fields via theComplexObjectimplementation.
Methods
value(&self, format: Option<BigIntFormat>) -> Option<String>
Type: Asynchronous method (returns a
Future).Parameters:
format: OptionalBigIntFormatenum specifying the desired format for the big integer representation.
Returns:
Option<String>asynchronously resolving to a formatted string representation of the currency value.Description: This method formats the internal
valuefield using the helper functionformat_big_int_dec. It takes into account the optional format provided and produces a formatted string that suits various big integer display requirements.Usage Example:
let currency = OtherCurrency { currency: Some(1234.56), value: Some("1234560000000000000000".to_string()), }; let formatted_value = currency.value(Some(BigIntFormat::Decimal)).await; println!("Formatted currency value: {:?}", formatted_value);
Implementation Details
The
OtherCurrencystruct usesasync_graphqlmacros to integrate with GraphQL, allowing thevaluemethod to be exposed as a complex, computed field.The formatting logic is delegated to the
format_big_int_decfunction from thehelpersmodule, ensuring consistent formatting across the system.The
valuefield is intentionally skipped in the GraphQL schema to control access via the computedvalue()method instead, allowing formatting on-demand.The
currencyfield provides a straightforward numeric value, but it is optional and may be absent depending on data availability.
Interaction with Other Modules
formats::BigIntFormat: An enum or type that defines possible formats for big integer representation. This is used by thevaluemethod to determine how to format the internal string value.helpers::format_big_int_dec: A helper function that performs the actual conversion of the stored big integer string into a human-readable decimal format, potentially applying formatting rules defined byBigIntFormat.The file resides in a module that includes other currency and formatting utilities, and it integrates into a larger GraphQL API schema by providing currency-related objects.
Diagram: Class Structure of OtherCurrency
classDiagram
class OtherCurrency {
+currency: Option<f64>
-value: Option<String>
+value(format: Option<BigIntFormat>): Option<String> (async)
}
This diagram illustrates the OtherCurrency struct's fields and its asynchronous method for retrieving formatted currency values. The value field is private to GraphQL and accessed through the value() method.