flarrow_runtime/plugins/
file_ext_default.rs

1use crate::prelude::{thirdparty::libloading::Library, *};
2
3#[derive(FileExtPlugin)]
4pub struct DefaultFileExtPlugin {}
5
6#[file_ext_plugin(runtime = "default_runtime")]
7impl FileExtPlugin for DefaultFileExtPlugin {
8    async fn new() -> Result<Self>
9    where
10        Self: Sized,
11    {
12        Ok(DefaultFileExtPlugin {})
13    }
14
15    fn target(&self) -> Vec<String> {
16        vec!["so".to_string(), "dylib".to_string(), "dll".to_string()]
17    }
18
19    async fn load(
20        &self,
21        path: std::path::PathBuf,
22
23        inputs: Inputs,
24        outputs: Outputs,
25        queries: Queries,
26        queryables: Queryables,
27        configuration: serde_yml::Value,
28    ) -> Result<flarrow_runtime_core::prelude::RuntimeNode> {
29        match path.extension() {
30            Some(ext) => {
31                if ext == std::env::consts::DLL_EXTENSION {
32                    let path_buf = path.clone();
33                    let (library, constructor) = tokio::task::spawn_blocking(move || {
34                        let library = unsafe {
35                            Library::new(path_buf.clone())
36                                .wrap_err(format!("Failed to load path {:?}", path_buf))?
37                        };
38
39                        let constructor = unsafe {
40                            library
41                                .get::<*mut DynamicallyLinkedNodeInstance>(b"FLARROW_NODE")
42                                .wrap_err(format!(
43                                    "Failed to load symbol 'FLARROW_NODE' from dylib {:?}",
44                                    path_buf
45                                ))?
46                                .read()
47                        };
48
49                        Ok::<_, eyre::Report>((library, constructor))
50                    })
51                    .await??;
52
53                    Ok(RuntimeNode::DynamicallyLinked(DynamicallyLinkedNode {
54                        _library: library,
55                        handle: (constructor)(inputs, outputs, queries, queryables, configuration)
56                            .await?
57                            .wrap_err(format!(
58                                "Failed to create dynamically linked node from dylib {:?}",
59                                path,
60                            ))?,
61                    }))
62                } else {
63                    Err(eyre::eyre!(
64                        "Unsupported file extension '{:?}'. On this platform it must be '{}'",
65                        ext,
66                        std::env::consts::DLL_EXTENSION
67                    ))
68                }
69            }
70            None => Err(eyre::eyre!("No file extension found for path {:?}", path)),
71        }
72    }
73}