hyperion/effects/
providers.rs

1use std::{path::Path, sync::Arc};
2
3use thiserror::Error;
4
5use super::instance::RuntimeMethods;
6
7#[cfg(feature = "python")]
8mod python;
9
10#[derive(Debug, Error)]
11pub enum ProviderError {
12    #[cfg(feature = "python")]
13    #[error(transparent)]
14    Python(#[from] python::Error),
15}
16
17/// Trait for effect providers.
18///
19/// An effect provider is able to load an effect script and run it when given input parameters.
20pub trait Provider: std::fmt::Debug + Send + Sync {
21    /// Returns if this provider supports the given effect
22    ///
23    /// # Parameters
24    ///
25    /// * `script_path`: path to the script file describing this effect. This is the `script` field
26    ///   in the effect definition JSON.
27    ///
28    /// # Returns
29    ///
30    /// `true` if this provider can handle this script file, `false` otherwise.
31    fn supports(&self, script_path: &str) -> bool;
32
33    /// Run the given effect to completion in a blocking fashion
34    ///
35    /// # Parameters
36    ///
37    /// * `full_script_path`: resolved script path
38    /// * `args`: arguments to the effect
39    /// * `methods`: instance interaction methods
40    fn run(
41        &self,
42        full_script_path: &Path,
43        args: serde_json::Value,
44        methods: Arc<dyn RuntimeMethods>,
45    ) -> Result<(), ProviderError>;
46}
47
48#[derive(Debug)]
49pub struct Providers {
50    providers: Vec<Arc<dyn Provider>>,
51}
52
53impl Default for Providers {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl Providers {
60    pub fn new() -> Self {
61        Self {
62            providers: vec![
63                #[cfg(feature = "python")]
64                Arc::new(python::PythonProvider::new()),
65            ],
66        }
67    }
68
69    pub fn get(&self, script_path: &str) -> Option<Arc<dyn Provider>> {
70        self.providers
71            .iter()
72            .find(|provider| provider.supports(script_path))
73            .cloned()
74    }
75}