lib.rs
Overview
This file serves as the main entry point for initializing the SDK client in the project. It provides a public function to create and configure a ClientContext instance used for interacting with the TVM client network. Additionally, it exposes several modules related to web integration, default configurations, helper utilities, and schema definitions that support the SDK's operations.
Modules
web
Contains web-related functionality and integrations for the SDK. Refer to web for detailed descriptions and usage.defaults
Defines default values and configurations used across the SDK. See defaults for more details.helpers
Provides auxiliary functions to simplify common tasks within the SDK. Detailed information is available under helpers.schema
Contains data structures and schema definitions for the SDK's data exchange formats. Consult schema for schema specifics.
Functions
init_sdk
pub fn init_sdk(api_url: Option<String>) -> anyhow::Result<Arc<ClientContext>>
Description
Initializes the SDK's client context by configuring and creating a ClientContext object. This function optionally accepts a custom API URL to override the default network endpoints.
Parameters
api_url: Option<String>
An optional string containing the base URL for the API endpoint. If provided, this URL is used as the sole endpoint in the network configuration; otherwise, the default endpoints are used.
Returns
anyhow::Result<Arc<ClientContext>>
Returns anArc-wrappedClientContexton success, allowing shared ownership across threads. On failure, it returns an error wrapped inanyhow::Resultwith a descriptive message.
Usage Example
use std::sync::Arc;
fn main() -> anyhow::Result<()> {
let sdk_client: Arc<ClientContext> = lib::init_sdk(Some("https://api.example.com".to_string()))?;
// Use sdk_client for network operations
Ok(())
}
Implementation Details
Creates a vector of endpoints from the optional
api_urlparameter.Constructs a
ClientConfigwith aNetworkConfigthat includes these endpoints.Calls
ClientContext::newwith the config and wraps the result in anArc.Uses the
anyhowcrate for error handling to provide context-specific error messages.
Dependencies and Interactions
tvm_client crate
Uses types from thetvm_clientcrate:NetworkConfigfor network-related configuration.ClientConfigfor client-wide configuration.ClientContextas the primary client SDK context object.
Arc (Atomic Reference Counting)
TheArcpointer type from the standard library is used to enable thread-safe shared ownership of theClientContext.anyhow crate
Employed for convenient error handling and propagation.
This file encapsulates the creation and configuration logic for the SDK client and serves as a foundation for the rest of the SDK's modules (web, defaults, helpers, and schema). Other parts of the application will import and use the init_sdk function to initialize network communications.
Mermaid Diagram: Structure and Workflow
flowchart TD
A[lib.rs] --> B[init_sdk]
A --> C[web module]
A --> D[defaults module]
A --> E[helpers module]
A --> F[schema module]
B --> G[Create endpoints vector]
B --> H[Build ClientConfig]
H --> I[NetworkConfig with endpoints]
B --> J[Create ClientContext]
J --> K[Return Arc<ClientContext>]
style A fill:#f9f,stroke:#333,stroke-width:2px
style B fill:#bbf,stroke:#333
style C fill:#eef,stroke:#333
style D fill:#eef,stroke:#333
style E fill:#eef,stroke:#333
style F fill:#eef,stroke:#333
style G fill:#afa,stroke:#333
style H fill:#afa,stroke:#333
style I fill:#afa,stroke:#333
style J fill:#afa,stroke:#333
style K fill:#afa,stroke:#333
This flowchart illustrates the main function init_sdk and its steps, along with the modules exposed by this file. It highlights how the function processes the optional API URL to create network configuration and initializes the client context.