Skip to main content

Crate diode_base

Crate diode_base 

Source
Expand description

§diode-base

Base functionality and utilities for diode applications, providing essential building blocks for creating robust, configurable, and maintainable services.

This crate extends the core diode dependency injection framework with practical utilities for real-world applications including configuration management, daemon services, command-line interfaces, and application lifecycle management.

§Core Components

  • Configuration System: Type-safe configuration loading and merging from multiple sources
  • Daemon Framework: Long-running background services with graceful shutdown
  • Command System: CLI framework with subcommands and dependency injection
  • Bundle Management: Modular application component grouping
  • Tracing Integration: Structured logging and observability
  • Dynamic Configuration: Runtime configuration updates and hot-reloading

§Quick Start

use diode::App;
use diode_base::{RunMainExt, AddCommandExt, Command};
use std::process::ExitCode;
use std::sync::Arc;
use clap::Command as ClapCommand;

struct HelloCommand;

impl Command for HelloCommand {
    fn command() -> ClapCommand {
        ClapCommand::new("hello").about("Says hello")
    }

    async fn main(_app: Arc<App>, _matches: clap::ArgMatches) -> ExitCode {
        println!("Hello from diode!");
        ExitCode::SUCCESS
    }
}

#[tokio::main]
async fn main() -> ExitCode {
    App::builder()
        .add_command::<HelloCommand>()
        .run_main()
        .await
}

§Configuration Example

use diode::App;
use diode_base::Config;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct DatabaseConfig {
    host: String,
    port: u16,
}

let app = App::builder()
    .add_component(Config::new().with("database", DatabaseConfig {
        host: "localhost".to_string(),
        port: 5432,
    }))
    .build()
    .await?;

let config = app.get_component_ref::<Config>().unwrap();
let db_config = config.get::<DatabaseConfig>("database")?;
println!("Database: {}:{}", db_config.host, db_config.port);

§Features

  • macros (default): Enables procedural macros for simplified configuration and service definitions

Modules§

testing

Macros§

defer

Structs§

CancellationToken
Cooperative cancellation token used to signal daemons to shut down. A token which can be used to signal a cancellation request to one or more tasks.
Config
ConfigCommand
Built-in config command that displays the current configuration.
DynamicConfig
Main dynamic configuration store
DynamicConfigConfig
Configuration for dynamic config system
DynamicConfigFile
File-based dynamic configuration provider
DynamicConfigFileConfig
DynamicConfigUpdater
Updater interface for providers to update configuration
DynamicValue
DynamicValueGuard
Metrics
MetricsConfig
MetricsOtlpExporterConfig
ServerCommand
Built-in server command that runs all registered daemons.
Tracing
TracingConfig
TracingOtlpExporterConfig

Traits§

AddCommandExt
Extension trait for AppBuilder to add command registration methods.
AddDaemonExt
Registers concrete Daemon instances on the application.
AddDaemonServiceExt
Registers daemons resolved from the dependency-injection container.
AddDynamicConfigExt
BundleExt
Command
Trait for defining CLI commands that can access the application’s dependency container.
ConfigSection
Daemon
A long-running background task managed by the application.
DynamicConfigService
Trait for dynamic configuration providers
RunDaemonsExt
Runs every registered Daemon until shutdown.
RunMainExt
Extension trait for AppBuilder to run the main CLI application.

Functions§

defer

Attribute Macros§

async_trait
config_section