yang_base/definition/
plugins.rs1use super::{Registry, TypedActionHandle};
4use crate::action::ActionContext;
5use crate::error::BaseError;
6use std::sync::Arc;
7use std::sync::OnceLock;
8
9pub struct ActionLink<I, O> {
11 reference: super::ActionRef,
12 handle: Arc<OnceLock<TypedActionHandle<I, O>>>,
13}
14
15impl<I, O> Clone for ActionLink<I, O> {
16 fn clone(&self) -> Self {
17 Self {
18 reference: self.reference.clone(),
19 handle: Arc::clone(&self.handle),
20 }
21 }
22}
23
24impl<I, O> ActionLink<I, O>
25where
26 I: Send + 'static,
27 O: Send + 'static,
28{
29 pub fn new(reference: super::ActionRef) -> Self {
31 Self {
32 reference,
33 handle: Arc::new(OnceLock::new()),
34 }
35 }
36
37 pub fn reference(&self) -> &super::ActionRef {
39 &self.reference
40 }
41
42 pub fn bind(&self, registry: &Registry) -> Result<(), BaseError> {
44 let handle = registry.resolve_typed(&self.reference)?;
45 self.handle.set(handle).map_err(|_| {
46 BaseError::ConfigError(format!("ActionLink {} 被重复绑定", self.reference))
47 })
48 }
49
50 pub fn handle(&self) -> Result<TypedActionHandle<I, O>, BaseError> {
52 self.handle.get().copied().ok_or_else(|| {
53 BaseError::ConfigError(format!("ActionLink {} 尚未绑定", self.reference))
54 })
55 }
56}
57
58pub struct Plugins {
63 registry: Arc<Registry>,
64 context: ActionContext,
65}
66
67impl Plugins {
68 pub(crate) fn new(registry: Arc<Registry>, context: ActionContext) -> Self {
69 Self { registry, context }
70 }
71
72 pub async fn api_run<I, O>(
74 self,
75 handle: TypedActionHandle<I, O>,
76 input: I,
77 ) -> Result<O, BaseError>
78 where
79 I: Send + 'static,
80 O: Send + 'static,
81 {
82 self.registry.call(handle, self.context, input).await
83 }
84}