Skip to content

Plugins ​

A plugin groups a reusable integration into a crate. A database, a cache, or a client for an external service can be distributed as a plugin. The Plugin trait declares the providers and components of the integration.

Plugin and module ​

A plugin and a module register the same pieces in the same container, but they play different roles.

  • A module belongs to the application and can register controllers, components, and providers.
  • A plugin is distributed as a reusable crate and does not register controllers.

Both share the container. A component can inject a plugin provider without any distinction.

API Reference: 'Plugin' trait

Plugin name ​

Identifies the plugin in the startup logs.

rust
fn name() -> &'static str;

Example

rust
fn name() -> &'static str {
    "database"
}

Registering components ​

Registers injectable structs that the dependency container builds automatically. Learn more

rust
fn register_components(components: &ComponentRegistry)

Parameters

  • components: the registry where the plugin's components are declared.

Example

rust
fn register_components(components: &ComponentRegistry) {
    components.register::<DatabaseHealth>();
}

Registering providers ​

Registers injectable structs that are built manually, usually connections or external clients. It is asynchronous because it usually opens connections. Learn more

rust
async fn register_providers(config: &Config, providers: &ProviderRegistry)

Parameters

  • config: the loaded configuration, used to read the plugin configuration.
  • providers: the registry where the plugin's providers are declared.

Example

rust
async fn register_providers(config: &Config, providers: &ProviderRegistry) {
    let config = config.get_or_default::<DatabaseConfig>();

    providers.register(
        Database::connect(&config)
            .await
            .expect("database plugin: failed to connect"),
    );
}

Registering a plugin ​

Plugins are registered with with_plugin on the ApplicationBuilder, in the same chain as modules.

rust
use database_plugin::DatabasePlugin;
use sword::prelude::*;

#[sword::main]
async fn main() -> Result<(), Error> {
    let app = Application::builder()?
        .with_plugin::<DatabasePlugin>()
        .with_module::<UsersModule>()
        .build()?;

    app.run().await
}

Plugin configuration ​

In most cases a plugin needs configuration, and it can be declared in the application's TOML configuration file.

rust
#[config(key = "database")]
#[derive(Debug, Clone, Deserialize)]
pub struct DatabaseConfig {
    pub url: String,
    pub max_connections: u32,
}

Then, inside the Plugin implementation:

rust
async fn register_providers(config: &Config, providers: &ProviderRegistry) {
    let config = config.expect::<DatabaseConfig>();

    providers.register(Database::connect(&config).await);
}

In this case expect is used because the plugin configuration is required. Learn more in the 'Configuration' section

Distribution ​

It is recommended to distribute plugins following the 'one plugin per crate' approach, that is, create and publish plugins with a single feature as a separate crate.

For reference implementations, see the plugins example in the repository.