reactive_graph_plugin_api/
lib.rs

1#[cfg(feature = "json5")]
2pub use json5;
3pub use serde_json;
4pub use springtime_di;
5pub use springtime_di::Component;
6pub use springtime_di::component_alias;
7pub use springtime_di::injectable;
8pub use springtime_di::instance_provider::ComponentInstancePtr;
9pub use springtime_di::instance_provider::ErrorPtr;
10#[cfg(feature = "toml")]
11pub use toml;
12
13pub use crate::PluginLoadingError;
14pub use PluginActivationError;
15pub use PluginDeactivationError;
16pub use PluginUnloadingError;
17pub use behaviours::entities::entity_behaviour_registry::*;
18pub use behaviours::entities::entity_component_behaviour_registry::*;
19pub use behaviours::relations::relation_behaviour_registry::*;
20pub use behaviours::relations::relation_component_behaviour_registry::*;
21pub use error::activation::*;
22pub use error::hot_deploy::*;
23pub use error::lifecycle::*;
24pub use error::loading::*;
25pub use graphql::graphql_query_service::*;
26pub use graphql::http_body::HttpBody;
27pub use graphql::web_resource_manager::*;
28pub use graphql::web_resource_provider::*;
29pub use instances::entities::entity_instance_manager::*;
30pub use instances::flows::flow_instance_manager::*;
31pub use instances::relations::relation_instance_manager::*;
32pub use plugin::PLUGIN_NAME_PREFIX;
33pub use plugin::Plugin;
34pub use plugin_context::*;
35pub use plugin_declaration::*;
36pub use plugin_dependency::*;
37pub use plugin_state::PluginDeployError;
38pub use plugin_state::PluginDisableError;
39pub use plugin_state::PluginRefreshingState;
40pub use plugin_state::PluginResolveState;
41pub use plugin_state::PluginStartError;
42pub use plugin_state::PluginStartingState;
43pub use plugin_state::PluginState;
44pub use plugin_state::PluginStopError;
45pub use plugin_state::PluginStoppingState;
46pub use plugin_state::PluginUninstallError;
47pub use plugin_state::PluginUninstallingState;
48pub use reactive_graph_graph as model;
49pub use system::command_manager::*;
50pub use system::config_manager::*;
51pub use types::components::component_import_export_manager::*;
52pub use types::components::component_manager::*;
53pub use types::entities::entity_type_import_export_manager::*;
54pub use types::entities::entity_type_manager::*;
55pub use types::flows::flow_type_import_export_manager::*;
56pub use types::flows::flow_type_manager::*;
57pub use types::relations::relation_type_import_export_manager::*;
58pub use types::relations::relation_type_manager::*;
59pub use types::type_system::type_system_event_manager::*;
60pub use types::type_system::type_system_provider_registry::*;
61
62// pub use reactive_graph_type_system_api::TypeProvider;
63// TODO: pub use reactive_graph_type_system_api::*;
64
65pub mod behaviours;
66pub mod instances;
67pub mod types;
68
69pub mod system;
70
71pub mod embedded_asset_provider;
72pub mod error;
73pub mod graphql;
74pub mod plugin;
75pub mod plugin_context;
76pub mod plugin_declaration;
77pub mod plugin_dependency;
78pub mod plugin_state;
79
80pub static RUSTC_VERSION: &str = env!("RUSTC_VERSION");
81pub static PLUGIN_API_VERSION: &str = env!("CARGO_PKG_VERSION");
82
83#[macro_export]
84macro_rules! export_plugin {
85    () => {
86        $crate::export_plugin_constants!();
87        $crate::construct_plugin!();
88        $crate::register_plugin!();
89        $crate::plugin_dependencies!();
90        $crate::export_plugin_declaration!();
91    };
92    ({
93        "plugin": {
94            "name": $name: expr,
95            "description": $description: expr,
96            "version": $version: expr $(,)?
97        } $(,)?
98    }) => {
99        $crate::export_plugin_constants!(
100            "plugin": {
101                "name": $name,
102                "description": $description,
103                "version": $version,
104            },
105        );
106        $crate::construct_plugin!();
107        $crate::register_plugin!();
108        $crate::plugin_dependencies!();
109        $crate::export_plugin_declaration!();
110    };
111    ({
112        "dependencies": [
113            $({
114                "name": $plugin_name: expr,
115                "version": $version_range: expr $(,)?
116            } $(,)?)*
117        ]
118    }) => {
119        $crate::export_plugin_constants!();
120        $crate::construct_plugin!();
121        $crate::register_plugin!();
122        $crate::plugin_dependencies!(
123            "dependencies": [
124                $({
125                    "name": $plugin_name,
126                    "version": $version_range,
127                },)*
128            ],
129        );
130        $crate::export_plugin_declaration!();
131    };
132    ({
133        "plugin": {
134            "name": $name: expr,
135            "description": $description: expr,
136            "version": $version: expr $(,)?
137        },
138        "dependencies": [
139            $({
140                "name": $plugin_name: expr,
141                "version": $version_range: expr $(,)?
142            } $(,)?)*
143        ]
144        $(,)?
145    }) => {
146        $crate::export_plugin_constants!(
147            "plugin": {
148                "name": $name,
149                "description": $description,
150                "version": $version,
151            },
152        );
153        $crate::construct_plugin!();
154        $crate::register_plugin!();
155        $crate::plugin_dependencies!(
156            "dependencies": [
157                $({
158                    "name": $plugin_name,
159                    "version": $version_range,
160                },)*
161            ],
162        );
163        $crate::export_plugin_declaration!();
164    };
165}
166#[macro_export]
167macro_rules! export_plugin_constants {
168    () => {
169        /// The name of the plugin (CARGO_PKG_NAME).
170        pub static PLUGIN_NAME: &str = env!("CARGO_PKG_NAME");
171
172        /// The description of the plugin (CARGO_PKG_DESCRIPTION).
173        pub static PLUGIN_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
174
175        /// The version of the plugin (CARGO_PKG_VERSION).
176        pub static PLUGIN_VERSION: &str = env!("CARGO_PKG_VERSION");
177    };
178    (
179        "plugin": {
180            "name": $name: expr,
181            "description": $description: expr,
182            "version": $version: expr $(,)?
183        } $(,)?
184    ) => {
185        /// The name of the plugin (CARGO_PKG_NAME).
186        pub static PLUGIN_NAME: &str = $name;
187
188        /// The description of the plugin (CARGO_PKG_DESCRIPTION).
189        pub static PLUGIN_DESCRIPTION: &str = $description;
190
191        /// The version of the plugin (CARGO_PKG_VERSION).
192        pub static PLUGIN_VERSION: &str = $version;
193    };
194}
195
196#[macro_export]
197macro_rules! construct_plugin {
198    () => {
199        /// Static plugin context.
200        ///
201        /// This plugin context is created by construct_plugin before constructing
202        /// the dependency injection container.
203        pub static PLUGIN_CONTEXT: std::sync::OnceLock<std::sync::Arc<dyn $crate::PluginContext + Send + Sync>> = std::sync::OnceLock::new();
204
205        /// Returns the static plugin context if called after construct_plugin or an empty option if called before construct_plugin.
206        ///
207        /// ```
208        /// pub struct MyPluginImpl {
209        ///   #[component(default = "inject_plugin_context_checked")]
210        ///   context: Option<std::sync::Arc<dyn reactive_graph_plugin_api::PluginContext + Send + Sync>>,
211        /// }
212        /// ```
213        pub fn inject_plugin_context_checked() -> Option<std::sync::Arc<dyn $crate::PluginContext + Send + Sync>> {
214            PLUGIN_CONTEXT.get().cloned()
215        }
216
217        /// Returns the static plugin context.
218        ///
219        /// ```
220        /// pub struct MyPluginImpl {
221        ///   #[component(default = "inject_plugin_context")]
222        ///   context: std::sync::Arc<dyn reactive_graph_plugin_api::PluginContext + Send + Sync>,
223        /// }
224        /// ```
225        ///
226        /// # Panics
227        ///
228        /// Panics if the inject_plugin_context was called before construct_plugin!
229        ///
230        pub fn inject_plugin_context() -> std::sync::Arc<dyn $crate::PluginContext + Send + Sync> {
231            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
232                panic!("The plugin context is uninitialized!");
233            };
234            context
235        }
236
237        pub fn type_system_provider_registry() -> std::sync::Arc<dyn reactive_graph_plugin_api::TypeSystemProviderRegistry + Send + Sync> {
238            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
239                panic!("The plugin context is uninitialized!");
240            };
241            context.get_type_system_provider_registry()
242        }
243
244        pub fn entity_behaviour_registry() -> std::sync::Arc<dyn reactive_graph_plugin_api::EntityBehaviourRegistry + Send + Sync> {
245            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
246                panic!("The plugin context is uninitialized!");
247            };
248            context.get_entity_behaviour_registry()
249        }
250
251        pub fn entity_component_behaviour_registry() -> std::sync::Arc<dyn reactive_graph_plugin_api::EntityComponentBehaviourRegistry + Send + Sync> {
252            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
253                panic!("The plugin context is uninitialized!");
254            };
255            context.get_entity_component_behaviour_registry()
256        }
257
258        pub fn relation_behaviour_registry() -> std::sync::Arc<dyn reactive_graph_plugin_api::RelationBehaviourRegistry + Send + Sync> {
259            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
260                panic!("The plugin context is uninitialized!");
261            };
262            context.get_relation_behaviour_registry()
263        }
264
265        pub fn relation_component_behaviour_registry() -> std::sync::Arc<dyn reactive_graph_plugin_api::RelationComponentBehaviourRegistry + Send + Sync> {
266            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
267                panic!("The plugin context is uninitialized!");
268            };
269            context.get_relation_component_behaviour_registry()
270        }
271
272        pub fn component_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::ComponentManager + Send + Sync> {
273            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
274                panic!("The plugin context is uninitialized!");
275            };
276            context.get_component_manager()
277        }
278
279        pub fn entity_type_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::EntityTypeManager + Send + Sync> {
280            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
281                panic!("The plugin context is uninitialized!");
282            };
283            context.get_entity_type_manager()
284        }
285
286        pub fn relation_type_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::RelationTypeManager + Send + Sync> {
287            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
288                panic!("The plugin context is uninitialized!");
289            };
290            context.get_relation_type_manager()
291        }
292
293        pub fn flow_type_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::FlowTypeManager + Send + Sync> {
294            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
295                panic!("The plugin context is uninitialized!");
296            };
297            context.get_flow_type_manager()
298        }
299
300        pub fn web_resource_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::WebResourceManager + Send + Sync> {
301            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
302                panic!("The plugin context is uninitialized!");
303            };
304            context.get_web_resource_manager()
305        }
306
307        pub fn config_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::ConfigManager + Send + Sync> {
308            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
309                panic!("The plugin context is uninitialized!");
310            };
311            context.get_config_manager()
312        }
313
314        pub fn entity_instance_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::EntityInstanceManager + Send + Sync> {
315            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
316                panic!("The plugin context is uninitialized!");
317            };
318            context.get_entity_instance_manager()
319        }
320
321        pub fn relation_instance_manager() -> std::sync::Arc<dyn reactive_graph_plugin_api::RelationInstanceManager + Send + Sync> {
322            let Some(context) = PLUGIN_CONTEXT.get().cloned() else {
323                panic!("The plugin context is uninitialized!");
324            };
325            context.get_relation_instance_manager()
326        }
327
328        /// Constructs the plugin and initializes the plugin context.
329        ///
330        /// This method guarantees that the plugin context is set before initializing the
331        /// dependency injection framework. After `PLUGIN_CONTEXT.set` has been called, it's
332        /// guaranteed that PLUGIN_CONTEXT has a value. See also the documentation of OnceLock::set.
333        ///
334        /// Returns an error if the plugin construction failed, by either:
335        /// - The dependency injection container failed to construct
336        /// - The dependency injection container doesn't contain a component of type Plugin
337        /// - A component has unsatisfied dependencies.
338        pub fn construct_plugin(
339            context: std::sync::Arc<dyn $crate::PluginContext + Send + Sync>,
340        ) -> Result<std::sync::Arc<dyn $crate::Plugin + Send + Sync>, $crate::PluginLoadingError> {
341            let _ = PLUGIN_CONTEXT.set(context);
342            // After set has been called, it's guaranteed that PLUGIN_CONTEXT has a value.
343            $crate::springtime_di::factory::ComponentFactoryBuilder::new()
344                // Propagate errors from the ComponentFactoryBuilder
345                .map_err($crate::PluginLoadingError::ComponentDefinitionRegistryError)
346                .map(|component_factory| component_factory.build())
347                .and_then(|mut component_factory| {
348                    // Get the
349                    $crate::springtime_di::instance_provider::TypedComponentInstanceProvider::primary_instance_typed::<dyn $crate::Plugin + Send + Sync>(
350                        &mut component_factory,
351                    )
352                    .map_err($crate::PluginLoadingError::ComponentInstanceProviderError)
353                })
354        }
355    };
356}
357
358#[macro_export]
359macro_rules! register_plugin {
360    () => {
361        /// The `register` method is
362        #[allow(improper_ctypes_definitions)]
363        extern "C" fn register(registrar: &mut dyn $crate::PluginRegistrar) -> Result<(), $crate::PluginLoadingError> {
364            if let Err(error) = log4rs::init_file("config/logging.toml", Default::default()) {
365                println!("Failed to configure logger in {}: {}", PLUGIN_NAME, error);
366            }
367            match construct_plugin(registrar.context()) {
368                Ok(plugin) => {
369                    registrar.register_plugin(Box::new(plugin));
370                    Ok(())
371                }
372                Err(e) => Err(e),
373            }
374        }
375    };
376}
377
378#[macro_export]
379macro_rules! get_context {
380    ($context: expr, $err: expr) => {
381        $context.clone().ok_or($err)?
382    };
383}
384
385pub mod prelude;
386
387#[cfg(test)]
388#[cfg(not(tarpaulin_include))]
389pub mod tests;