spark-config
A lightweight crate to manage Spark configuration in Rust applications.
Overview
The spark-config crate provides a structured way to load, validate, and access configuration settings for Spark applications. It handles Bitcoin network settings, operator configurations, service provider connections, and integrations with Mempool, Electrs, and LRC20 Node services.
Features
- Load configuration from TOML files
- Support for multiple Bitcoin networks (Mainnet, Testnet, Signet, Regtest)
- Configuration for Spark operators and service providers
- Integration with Mempool, Electrs, and LRC20 Node services
- Automatic validation of configuration values
- Flexible path resolution (absolute paths, HOME-relative paths)
Feature Flags
The crate provides the following optional features that can be enabled in your Cargo.toml:
with-serde: Enables serialization and deserialization support usingserde. This is required for loading configuration from TOML files.bitcoin-conversion: Enables conversion betweenBitcoinNetworkandbitcoin::Networktypes from thebitcoincrate.
Example usage in your Cargo.toml:
[]
= { = "0.0.1", = ["with-serde", "bitcoin-conversion"] }
Usage
Add spark-config to your Cargo.toml:
[]
# Option 1: Using local path
= { = "crates/generic/spark-config" }
# Option 2: Using version and path
= { = "0.0.1", = "crates/generic/spark-config" }
# Option 3: From repository (when published)
= "0.0.1"
Loading Configuration
use SparkConfig;
use Result;
// Load config from a TOML file
let config = from_toml_file?;
// Or using a relative path (will be resolved relative to HOME)
let config = from_toml_file?;
Note: Configuration loading returns a
Resulttype using theanyhowcrate for error handling. Errors can occur if:
- The configuration file cannot be found or opened
- The file cannot be read
- The TOML syntax is invalid
- Required fields are missing or have invalid values
You should handle these errors appropriately in your application.
Accessing Configuration Values
// Get Bitcoin network
let network = config.bitcoin_network;
// Get Mempool configuration
if let Some = config.mempool_base_url
// Get authentication details
if let Some = config.mempool_auth
// Access service provider endpoint
let ssp_key = "lightspark";
if let Some = config.ssp_endpoint
// Get coordinator operator
if let Some = config.coordinator_operator
Configuration File Format
The configuration is stored in TOML format. Here's an example structure:
# Bitcoin network (Mainnet, Testnet, Signet, Regtest)
= "Regtest"
# Mempool configuration
[]
= "https://regtest-mempool.example.com"
= "mempool-username"
= "mempool-password"
# Electrs configuration
[]
= "https://regtest-mempool.example.com/api"
= "electrs-username"
= "electrs-password"
# LRC20 Node configuration
[]
= "https://lrc20node.example.com/api"
# Service Provider configurations
[]
= "https://api.example.com"
= "graphql"
= "02000000000000000000000000000000000000000000000000000000000000abcd"
= "Lightspark"
# Operator configurations
[[]]
= 1
= "https://0.spark.lightspark.com"
= "02000000000000000000000000000000000000000000000000000000000000abcd"
= "01000000000000000000000000000000000000000000000000000000000000abcd"
= "Lightspark"
= true
Environment Variables
When running tests, you can specify a custom configuration path using the SPARK_CONFIG_PATH environment variable:
SPARK_CONFIG_PATH=/path/to/test/config.toml
License
Licensed under either of:
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)