20 releases

new 0.2.9 Nov 15, 2024
0.2.8 Nov 9, 2024
0.2.7 Oct 26, 2024
0.2.0 Sep 28, 2024
0.0.8 Aug 25, 2024

#97 in Configuration

Download history 109/week @ 2024-08-05 234/week @ 2024-08-12 274/week @ 2024-08-19 58/week @ 2024-08-26 397/week @ 2024-09-02 99/week @ 2024-09-09 349/week @ 2024-09-16 159/week @ 2024-09-23 348/week @ 2024-09-30 252/week @ 2024-10-07 477/week @ 2024-10-14 248/week @ 2024-10-21 87/week @ 2024-10-28 136/week @ 2024-11-04 157/week @ 2024-11-11

776 downloads per month
Used in 14 crates

MIT license

54KB
1K SLoC

crates.io Documentation

Introduction

spring is the core module of this project, which includes: configuration management, plugin management, and component management.

  • All plugins need to implement the Plugin trait.
  • All configurations need to implement the Configurable trait.
  • All components need to implement the Clone trait.

Note: To avoid deep copying of large struct in Component, it is recommended to use the newtype pattern to reference them via Arc<T>.

How to write your own plugin

Add dependencies

spring = { version = "<version>" }           # This crate contains the definition of plugin traits
serde = { workspace = true, features = ["derive"] } # Used to parse plugin configuration items
use serde::Deserialize;
use spring::async_trait;
use spring::config::{Configurable, ConfigRegistry};
use spring::{app::AppBuilder, plugin::Plugin};

struct MyPlugin;

#[async_trait]
impl Plugin for MyPlugin {
    async fn build(&self, app: &mut AppBuilder) {
        // Call app.get_config::<Config>() method to get configuration items
        match app.get_config::<Config>() {
            Ok(config) => {
                println!("{:#?}", config);
                assert_eq!(config.a, 1);
                assert_eq!(config.b, true);

                // Get the configuration items to build the corresponding components

            }
            Err(e) => println!("{:?}", e),
        }
    }
}

/// Plugin configuration
#[derive(Debug, Configurable, Deserialize)]
#[config_prefix = "my-plugin"]
struct Config {
    a: u32,
    b: bool,
}

For the complete code, refer to plugin-example, or refer to other built-in plugin codes.

Dependencies

~10–19MB
~243K SLoC