1use crate::base::SettingType;
6use crate::message::base::MessengerType;
7use crate::service::message::Receptor;
8use crate::service_context::ServiceContext;
9use crate::{event, payload_convert, service};
10
11use futures::future::LocalBoxFuture;
12use std::collections::HashSet;
13use std::fmt::Debug;
14use std::rc::Rc;
15use thiserror::Error;
16
17pub(crate) mod camera_watcher;
19
20pub(crate) mod media_buttons;
22
23pub(crate) mod authority;
25
26pub(crate) mod restore_agent;
28
29pub(crate) mod earcons;
31
32pub(crate) mod inspect;
34
35#[derive(Error, Debug, Clone, Copy, PartialEq)]
36pub enum AgentError {
37 #[error("Unhandled Lifespan")]
38 UnhandledLifespan,
39 #[error("Unexpected Error")]
40 UnexpectedError,
41}
42
43pub(crate) type InvocationResult = Result<(), AgentError>;
44
45#[derive(Clone, Copy, Debug, PartialEq)]
49pub(crate) enum Lifespan {
50 Initialization,
51 Service,
52}
53
54#[derive(Clone)]
56pub struct Invocation {
57 pub(crate) lifespan: Lifespan,
58 pub(crate) service_context: Rc<ServiceContext>,
59}
60
61impl Debug for Invocation {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 f.debug_struct("Invocation").field("lifespan", &self.lifespan).finish_non_exhaustive()
64 }
65}
66
67impl PartialEq for Invocation {
68 fn eq(&self, other: &Self) -> bool {
69 self.lifespan == other.lifespan
70 }
71}
72
73pub type AgentFuture = LocalBoxFuture<'static, ()>;
74
75pub enum CreationFunc {
77 Static(fn(Context) -> AgentFuture),
85 Dynamic(Rc<dyn Fn(Context) -> AgentFuture>),
98}
99
100pub struct AgentCreator {
102 pub debug_id: &'static str,
103 pub create: CreationFunc,
104}
105
106impl AgentCreator {
107 pub(crate) fn create(&self, context: Context) -> AgentFuture {
108 match &self.create {
109 CreationFunc::Static(f) => (f)(context),
110 CreationFunc::Dynamic(f) => (f)(context),
111 }
112 }
113}
114
115pub struct Context {
117 pub receptor: Receptor,
119
120 publisher: event::Publisher,
122
123 pub delegate: service::message::Delegate,
125
126 pub(crate) available_components: HashSet<SettingType>,
128}
129
130impl Context {
131 pub(crate) async fn new(
132 receptor: Receptor,
133 delegate: service::message::Delegate,
134 available_components: HashSet<SettingType>,
135 ) -> Self {
136 let publisher = event::Publisher::create(&delegate, MessengerType::Unbound).await;
137 Self { receptor, publisher, delegate, available_components }
138 }
139
140 async fn create_messenger(
144 &self,
145 ) -> Result<service::message::Messenger, service::message::MessageError> {
146 Ok(self.delegate.create(MessengerType::Unbound).await?.0)
147 }
148
149 pub(crate) fn get_publisher(&self) -> event::Publisher {
150 self.publisher.clone()
151 }
152}
153
154#[derive(Clone, Debug, PartialEq)]
155pub enum Payload {
156 Invocation(Invocation),
157 Complete(InvocationResult),
158}
159
160payload_convert!(Agent, Payload);