flarrow_url_scheme/
url_scheme_default.rs

1use std::sync::Arc;
2
3use crate::prelude::*;
4
5#[doc(hidden)]
6#[unsafe(no_mangle)]
7pub static FLARROW_URL_SCHEME_PLUGIN: DynamicallyLinkedUrlSchemePluginInstance =
8    || DefaultUrlSchemePlugin::new();
9
10static DEFAULT_TOKIO_RUNTIME: std::sync::LazyLock<tokio::runtime::Runtime> =
11    std::sync::LazyLock::new(|| {
12        tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime")
13    });
14
15fn default_runtime<T: Send + 'static>(
16    task: impl Future<Output = T> + Send + 'static,
17) -> tokio::task::JoinHandle<T> {
18    match tokio::runtime::Handle::try_current() {
19        Ok(handle) => handle.spawn(task),
20        Err(_) => DEFAULT_TOKIO_RUNTIME.spawn(task),
21    }
22}
23
24pub struct DefaultUrlSchemePlugin {}
25
26impl UrlSchemePlugin for DefaultUrlSchemePlugin {
27    fn new() -> tokio::task::JoinHandle<Result<Box<dyn UrlSchemePlugin>>>
28    where
29        Self: Sized,
30    {
31        default_runtime(async move {
32            Ok(Box::new(DefaultUrlSchemePlugin {}) as Box<dyn UrlSchemePlugin>)
33        })
34    }
35
36    fn target(&self) -> Vec<String> {
37        vec!["file".to_string(), "builtin".to_string()]
38    }
39
40    #[allow(clippy::too_many_arguments)]
41    fn load(
42        &self,
43        url: Url,
44
45        inputs: Inputs,
46        outputs: Outputs,
47        queries: Queries,
48        queryables: Queryables,
49        configuration: serde_yml::Value,
50        file_ext: Arc<FileExtManager>,
51    ) -> tokio::task::JoinHandle<Result<flarrow_runtime_core::prelude::RuntimeNode>> {
52        default_runtime(async move {
53            match url.scheme() {
54                "file" => {
55                    let path = url
56                        .to_file_path()
57                        .map_err(|_| eyre::eyre!("Url '{}' cannot be made into a path buf", url))?;
58
59                    file_ext
60                        .load(path, inputs, outputs, queries, queryables, configuration)
61                        .await
62                }
63                "builtin" => {
64                    // TODO!!
65                    eyre::bail!("Builtin url scheme is not supported yet!")
66                }
67                _ => Err(eyre::eyre!(
68                    "Url scheme '{}' is not supported",
69                    url.scheme()
70                )),
71            }
72        })
73    }
74}