Skip to main content

UseCase

Trait UseCase 

Source
pub trait UseCase:
    Send
    + Sync
    + Send {
    type Input: Send;
    type Output: Send;
    type Error: Error + Send + Sync + 'static;

    // Required method
    fn execute(
        &self,
        input: Self::Input,
    ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send;
}
Expand description

A single, named application operation that orchestrates domain objects and ports to do one thing.

§Examples

use ddd_toolkit_core::application::usecase::UseCase;
use std::fmt::Display;

#[derive(Debug)]
struct GreetError;

impl Display for GreetError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "name must not be empty")
    }
}

impl std::error::Error for GreetError {}

struct Greet;

impl UseCase for Greet {
    type Input = String;
    type Output = String;
    type Error = GreetError;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output, Self::Error> {
        if input.trim().is_empty() {
            return Err(GreetError);
        }
        Ok(format!("hello, {input}"))
    }
}

let greeting = block_on(Greet.execute("world".to_string()))?;
assert_eq!(greeting, "hello, world");

assert!(block_on(Greet.execute(String::new())).is_err());

Required Associated Types§

Source

type Input: Send

The input required to run this use case.

Source

type Output: Send

What this use case produces on success.

Source

type Error: Error + Send + Sync + 'static

What this use case produces on failure.

Required Methods§

Source

fn execute( &self, input: Self::Input, ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send

Runs the use case.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§