tangle_indexer


git clone https://radroots.dev/git/tangle_indexer.git
Log | Files | Refs | Submodules | LICENSE

config.rs (1634B)


      1 use anyhow::Result;
      2 use config::{Config, ConfigError, File};
      3 use serde::{Deserialize, Serialize};
      4 use std::path::Path;
      5 use thiserror::Error;
      6 
      7 #[derive(Debug, Error)]
      8 pub enum SettingsError {
      9     #[error("Configuration loading failed: {0}")]
     10     Load(#[from] ConfigError),
     11 }
     12 
     13 #[derive(Debug, Serialize, Deserialize, Clone)]
     14 pub struct Indexer {
     15     pub data_dir: String,
     16     pub logs_dir: String,
     17     pub flush_interval: u64,
     18 }
     19 
     20 #[derive(Debug, Serialize, Deserialize, Clone)]
     21 pub struct Relay {
     22     pub url: String,
     23     pub database_path: String,
     24 }
     25 
     26 #[derive(Debug, Serialize, Deserialize, Clone)]
     27 pub struct Listings {
     28     pub country_shard_size: usize,
     29     pub profile_shard_size: usize,
     30 }
     31 
     32 #[derive(Debug, Clone, Serialize, Deserialize)]
     33 pub struct Settings {
     34     pub indexer: Indexer,
     35     pub relay: Relay,
     36     pub listings: Listings,
     37 }
     38 
     39 impl Settings {
     40     pub fn load(config_path: &Option<String>) -> Result<Self, SettingsError> {
     41         let path = config_path.as_deref().unwrap_or("config.toml");
     42         let builder = Config::builder()
     43             .add_source(File::from(Path::new(path)).required(true));
     44 
     45         match builder.build() {
     46             Ok(cfg) => match cfg.try_deserialize::<Settings>() {
     47                 Ok(settings) => Ok(settings),
     48                 Err(err) => {
     49                     eprintln!("Failed to deserialize configuration: {err}");
     50                     Err(SettingsError::Load(err))
     51                 }
     52             },
     53             Err(err) => {
     54                 eprintln!("Failed to load configuration from '{}': {err}", path);
     55                 Err(SettingsError::Load(err))
     56             }
     57         }
     58     }
     59 }