tnn/extension/
repository.rs

1use anyhow::Result;
2use thiserror::Error;
3use tokio::sync::Mutex;
4
5use super::{opaque_fn::OpaqueFunctionCall, Call, Extension, Mixin, State};
6use crate::{call, mixin};
7use std::{any::Any, future::Future, pin::Pin, sync::Arc};
8
9pub const ADD_EXTENSION: Call<&'static Extension, ()> = call!(&'static Extension, (), "ADD_EXTENSION", "");
10
11pub struct AddCallArgs {
12	pub call: &'static str,
13	pub handler: OpaqueFunctionCall,
14}
15
16pub struct AddMixinArgs {
17	pub mixin: &'static dyn Any,
18	pub handler: &'static dyn Any,
19}
20
21pub const ADD_CALL: Call<AddCallArgs, ()> = call!(AddCallArgs, (), "ADD_CALL", "");
22
23pub const ADD_MIXIN: Call<AddMixinArgs, ()> = call!(AddMixinArgs, (), "ADD_MIXIN", "");
24
25pub const DEPENDENT_ACTIVATED: Mixin<&'static str> = mixin!(&'static str, "DEPENDENT_ACTIVATED", "");
26
27/// Fired when the repository has been locked and
28/// extensions can no longer be added.
29pub const REPOSITORY_LOCKED: Mixin<()> = mixin!((), "REPOSITORY_LOCKED", "");
30
31pub trait Protocol {
32	fn get_dependency_state(
33		&self,
34		dependency: &'static str,
35	) -> Pin<Box<dyn Future<Output = Result<Arc<Mutex<State>>>> + '_>>;
36
37	fn get_handler_for_call(
38		&self,
39		call_owner: &'static str,
40		call_id: &'static str,
41	) -> Pin<Box<dyn Future<Output = Result<Arc<OpaqueFunctionCall>>> + '_>>;
42}
43
44pub struct Repository(pub Pin<Box<dyn Protocol>>);
45
46#[derive(Error, Debug)]
47#[error("Extension '{0}' tried to use '{1}', access denied")]
48pub struct ExtensionAcessDeniedError(pub &'static str, pub &'static str);