AppBuilder

Struct AppBuilder 

Source
pub struct AppBuilder { /* private fields */ }
Expand description

Builder for constructing an App with registered services, plugins, and components.

The AppBuilder provides a fluent API for configuring an application before building it. It handles dependency resolution, plugin initialization, and component registration in the correct order based on declared dependencies.

§Examples

use diode::{App, Service, StdError, AddServiceExt};
use std::sync::Arc;

struct DatabaseService;
struct ApiService;

impl Service for DatabaseService {
    type Handle = Arc<Self>;
    async fn build(_app: &diode::AppBuilder) -> Result<Self::Handle, StdError> {
        Ok(Arc::new(Self))
    }
}

impl Service for ApiService {
    type Handle = Arc<Self>;
    async fn build(_app: &diode::AppBuilder) -> Result<Self::Handle, StdError> {
        Ok(Arc::new(Self))
    }
}

let app = App::builder()
    .add_service::<DatabaseService>()
    .add_service::<ApiService>()
    .add_component("config_value".to_string())
    .build()
    .await?;

Implementations§

Source§

impl AppBuilder

Source

pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Self
where T: Plugin + 'static,

Adds a plugin to the application builder.

Plugins are initialized during the build process in dependency order. Each plugin type can only be added once.

§Type Parameters
  • T - The plugin type to add. Must implement Plugin + 'static.
§Arguments
  • plugin - The plugin instance to add.
§Returns

Returns &mut Self for method chaining.

§Panics

Panics if a plugin of the same type has already been added.

§Examples
use diode::{App, Plugin, Dependencies, StdError};

struct MyPlugin;

impl Plugin for MyPlugin {
    async fn build(&self, _app: &mut diode::AppBuilder) -> Result<(), StdError> {
        Ok(())
    }
}

let app = App::builder()
    .add_plugin(MyPlugin)
    .build()
    .await?;
Source

pub fn has_plugin<T>(&self) -> bool
where T: Plugin + 'static,

Checks if a plugin of the specified type has been added.

§Type Parameters
  • T - The plugin type to check for.
§Returns

Returns true if the plugin has been added, false otherwise.

Source

pub fn add_component<T>(&mut self, component: T) -> &mut Self
where T: Send + Sync + 'static,

Adds a component directly to the application builder.

Components added this way are immediately available and do not require dependency resolution. Each component type can only be added once.

§Type Parameters
  • T - The component type to add. Must implement Send + Sync + 'static.
§Arguments
  • component - The component instance to add.
§Returns

Returns &mut Self for method chaining.

§Panics

Panics if a component of the same type has already been added.

§Examples
use diode::App;

let app = App::builder()
    .add_component(42i32)
    .add_component("configuration".to_string())
    .build()
    .await?;

assert_eq!(app.get_component::<i32>(), Some(42));
Source

pub fn get_component<T>(&self) -> Option<T>
where T: Clone + Send + Sync + 'static,

Retrieves a component by type, returning a clone if available.

§Type Parameters
  • T - The type of component to retrieve.
§Returns

Returns Some(T) if the component exists and can be cloned, None otherwise.

Source

pub fn has_component<T>(&self) -> bool
where T: Send + Sync + 'static,

Checks if a component of the specified type exists.

§Type Parameters
  • T - The type of component to check for.
§Returns

Returns true if the component exists, false otherwise.

Source

pub fn get_component_ref<T>(&self) -> Option<&T>
where T: Send + Sync + 'static,

Retrieves a reference to a component by type.

§Type Parameters
  • T - The type of component to retrieve a reference to.
§Returns

Returns Some(&T) if the component exists, None otherwise.

Source

pub fn get_component_mut<T>(&mut self) -> Option<&mut T>
where T: Send + Sync + 'static,

Retrieves a mutable reference to a component by type.

§Type Parameters
  • T - The type of component to retrieve a mutable reference to.
§Returns

Returns Some(&mut T) if the component exists, None otherwise.

Source

pub async fn build(&mut self) -> Result<App, AppError>

Trait Implementations§

Source§

impl AddServiceExt for AppBuilder

Source§

fn add_service<T>(&mut self) -> &mut Self
where T: Service + 'static,

Registers a service with the application builder. Read more
Source§

fn has_service<T>(&self) -> bool
where T: Service + 'static,

Checks if a service of the specified type has been registered. Read more
Source§

impl ExtractRef<AppBuilder> for AppBuilder

Source§

fn extract_ref(app: &AppBuilder) -> Result<&AppBuilder, AppError>

Extracts a reference to a value of type T from the application builder. Read more
Source§

fn dependencies() -> Dependencies

Returns the dependencies required by this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.