mod.rs
Overview
This file defines the Event data structure, which represents a message event within the system. The Event struct is designed to be used with GraphQL APIs, enabling the exposure of message event data in a standardized and accessible format. The file also implements a conversion from the underlying database message representation (db::Message) into the Event struct, facilitating the transformation of raw persistence data into API-ready objects.
Detailed Description
Struct: Event
The Event struct encapsulates the essential information related to a message event. It is annotated with #[derive(SimpleObject, Clone, Debug)] from the async_graphql crate, making it compatible for GraphQL query responses.
Fields
Field | Type | Description |
|---|---|---|
|
| Public identifier of the message event. |
|
| Optional message body, base64 encoded if present. |
|
| Optional creation timestamp in Unix time, automatically set by the block containing the transaction generating this event. |
|
| Optional destination address string. |
|
| Optional field indicating the order of the message in a chain. |
GraphQL Behavior
The
#[graphql(rename_fields = "snake_case")]attribute ensures that all field names are exposed in snake_case format in the GraphQL schema, which aligns with common GraphQL naming conventions.
Usage Example
// Assuming `db_msg` is an instance of `db::Message` retrieved from the database
let event: Event = db_msg.into(); // Converts db::Message to Event
// Accessing fields
println!("Message ID: {}", event.msg_id);
if let Some(body) = &event.body {
println!("Message Body (base64): {}", body);
}
if let Some(timestamp) = event.created_at {
println!("Created At (Unix time): {}", timestamp);
}
Implementation Details
Conversion: From<db::Message> for Event
The From trait is implemented to convert a db::Message instance (presumably the database representation of a message) into an Event struct. This conversion involves:
Mapping
msg_iddirectly fromdb::Message.id.Encoding the
bodyfield from raw bytes or string to a base64 string usingtvm_types::base64_encode, if present.Converting the
created_attimestamp from a custom type (likely a wrapper or optional type) to anOption<u64>via theToOptU64helper trait.Passing through
dstandmsg_chain_orderas-is.
This ensures that the data conforms to the API's expected format and that optional fields are handled gracefully.
Interaction with Other Components
Database Layer (
dbmodule): TheEventstruct is constructed from thedb::Messagestruct, indicating a tight coupling with the database schema. This file relies on thedbmodule for the source data model.Helpers (
helpersmodule): Uses theToOptU64trait from thehelpersmodule to convert thecreated_atfield into an optionalu64.External Library (
async_graphql): UsesSimpleObjectderive macro fromasync_graphqlfor GraphQL schema generation.Encoding Utility (
tvm_types): Usesbase64_encodefunction to convert message bodies into base64 strings for safe transmission over GraphQL.
This file acts as a bridge between the persistence layer and the GraphQL API layer, translating internal database objects into API-exposed types.
Mermaid Diagram: Structure of mod.rs
classDiagram
class Event {
+msg_id: String
-body: Option<String>
-created_at: Option<u64>
-dst: Option<String>
+msg_chain_order: Option<String>
}
Event ..> db::Message : from()