pub struct Engine { /* private fields */ }
Expand description

Main entity of service-io.

It defines the following schema that runs asynchronously: Input -> n Services -> Output

A message received by the InputConnector will be sent to a specific Service based on the Message::service_name. The Service will process the message and optionally can sent any number of output messages that will be delivered by the OutputConnector.

Example

use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::{Echo, Alarm};
use service_io::secret_manager::PasswordManager;

#[tokio::main]
async fn main() {
    Engine::default()
        .input(
            ImapClient::default()
                .domain("imap.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        .output(
            SmtpClient::default()
                .domain("smtp.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        .add_service("s-echo", Echo)
        .add_service("s-alarm", Alarm)
        .run()
        .await;
}

Implementations

Set an input connector for this engine that will be run after calling Engine::run().

Default connectors can be found in connectors. This call is mandatory in order to run the engine.

Set an output connector for this engine that will be run after calling Engine::run().

Default connectors can be found in connectors. This call is mandatory in order to run the engine.

Maps the message processed by the input connector into other message before checking the destination service the message is for.

Example
use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::Echo;
use service_io::message::util;
use service_io::secret_manager::PasswordManager;

#[tokio::main]
async fn main() {
    Engine::default()
        .input(
            ImapClient::default()
                .domain("imap.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        .output(
            SmtpClient::default()
                .domain("smtp.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        // Now, if the user writes "S-echo", it will found the service "s-echo"
        .map_input(util::service_name_first_char_to_lowercase)
        .add_service("s-echo", Echo)
        .run()
        .await;
}

Allow or disallow passing the message to the service based of the message itself. This filter method is applied just after the mapping method set by Engine::map_input.

Example
use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::Echo;
use service_io::secret_manager::PasswordManager;

#[tokio::main]
async fn main() {
    Engine::default()
        .input(
            ImapClient::default()
                .domain("imap.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        .output(
            SmtpClient::default()
                .domain("smtp.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        // Now, only input messages from gmail are allowed
        .filter_input(|message| message.user.ends_with("gmail.com"))
        .add_service("s-echo", Echo)
        .run()
        .await;
}

Add a service to the engine registered with a name. If the Message::service_name value matches with this name, the message will be redirected to the service.

Note that the service will not run until you call Engine::run()

Default services can be found in services

Similar to Engine::add_service() but service only allow receive message for a whitelist of users. If the Message::user of the incoming message not belong to that list, the message is discarded.

Example
use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::Process;
use service_io::secret_manager::PasswordManager;

#[tokio::main]
async fn main() {
    Engine::default()
        .input(
            ImapClient::default()
                .domain("imap.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        .output(
            SmtpClient::default()
                .domain("smtp.domain.com")
                .email("service@domain.com")
                .secret_manager(PasswordManager::new("1234")),
        )
        // We only want messages comming from the admin user
        // to go to s-process service to avoid attacks.
        .add_service_for("s-process", Process, ["admin@domain.com"])
        .run()
        .await;
}

Run asynchronously the input, output and all services configured for this engine. The engine will run until all services finished or the input/output connector finalizes.

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more