crb_agent/performers/
mod.rs

1pub mod async_performer;
2pub mod consume_performer;
3pub mod events_performer;
4pub mod interrupt_performer;
5pub mod sync_performer;
6
7use crate::address::Envelope;
8use crate::agent::Agent;
9use crate::context::Context;
10use anyhow::Error;
11use async_trait::async_trait;
12use std::fmt;
13
14pub trait AgentState: Send + 'static {}
15
16impl<T> AgentState for T where T: Send + 'static {}
17
18pub struct Next<T: ?Sized> {
19    pub(crate) transition: Box<dyn StatePerformer<T>>,
20}
21
22impl<T: Agent> Next<T> {
23    pub fn new(performer: impl StatePerformer<T>) -> Self {
24        Self {
25            transition: Box::new(performer),
26        }
27    }
28}
29
30pub enum TransitionCommand<T> {
31    Next(Next<T>),
32    Stop(StopReason),
33    ProcessEvents,
34    InContext(Envelope<T>),
35}
36
37impl<T> fmt::Debug for TransitionCommand<T> {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        let value = match self {
40            Self::Next(_) => "Next(_)",
41            Self::Stop(reason) => &format!("Stop({reason:?})"),
42            Self::ProcessEvents => "ProcessEvents",
43            Self::InContext(_) => "InContext(_)",
44        };
45        write!(f, "TransitionCommand::{}", value)
46    }
47}
48
49pub enum StopReason {
50    Failed(Error),
51    Stopped,
52}
53
54impl fmt::Debug for StopReason {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        let value = match self {
57            Self::Failed(_) => "Failed(_)",
58            Self::Stopped => "Stopped",
59        };
60        write!(f, "StopReason::{}", value)
61    }
62}
63
64pub enum ConsumptionReason {
65    Transformed,
66    Crashed(Error),
67}
68
69impl fmt::Debug for ConsumptionReason {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        let value = match self {
72            Self::Transformed => "Transformed",
73            Self::Crashed(_) => "Crashed(_)",
74        };
75        write!(f, "ConsumptionReason::{}", value)
76    }
77}
78
79pub enum Transition<T: Agent> {
80    Continue {
81        agent: T,
82        command: TransitionCommand<T>,
83    },
84    Consume {
85        reason: ConsumptionReason,
86    },
87}
88
89impl<T: Agent> fmt::Debug for Transition<T> {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        match self {
92            Self::Continue { command, .. } => {
93                write!(f, "Transition::{command:?}")
94            }
95            Self::Consume { reason } => {
96                write!(f, "Transition::{reason:?}")
97            }
98        }
99    }
100}
101
102#[async_trait]
103pub trait StatePerformer<T: Agent>: Send + 'static {
104    async fn perform(&mut self, agent: T, session: &mut Context<T>) -> Transition<T>;
105}