crb_agent/performers/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pub mod async_performer;
pub mod interrupt_performer;
pub mod loopback;
pub mod process_performer;

#[cfg(feature = "sync")]
pub mod sync_performer;

use crate::address::Envelope;
use crate::agent::Agent;
use anyhow::{Error, Result};
use async_trait::async_trait;

pub trait AgentState: Send + 'static {}

impl<T> AgentState for T where T: Send + 'static {}

pub struct Next<T: ?Sized> {
    pub(crate) transition: Box<dyn StatePerformer<T>>,
}

impl<T> Next<T>
where
    T: Agent,
{
    pub(crate) fn new(performer: impl StatePerformer<T>) -> Self {
        Self {
            transition: Box::new(performer),
        }
    }
}

pub enum TransitionCommand<T> {
    Next(Result<Next<T>>),
    Interrupted,
    Process,
    InContext(Envelope<T>),
}

pub enum Transition<T> {
    Continue {
        agent: T,
        command: TransitionCommand<T>,
    },
    Crashed(Error),
}

#[async_trait]
pub trait StatePerformer<T: Agent>: Send + 'static {
    async fn perform(&mut self, agent: T, session: &mut T::Context) -> Transition<T>;
    async fn fallback(&mut self, agent: T, err: Error) -> (T, Next<T>) {
        (agent, Next::interrupt(Some(err)))
    }
}