fugue_db/backend/
mod.rs

1use std::ops::Deref;
2use std::path::PathBuf;
3use url::Url;
4
5#[derive(Debug)]
6pub enum Imported {
7    Bytes(Vec<u8>),
8    File(PathBuf),
9}
10
11/// The Backend trait for adding support for new backend
12/// # Example
13/// ```
14/// use fugue_db::backend::{Backend, DatabaseImporterBackend, Imported};
15/// use fugue_db::Error;
16/// use url::Url;
17/// pub struct MyNewTool{}
18/// impl Backend for MyNewTool {
19///     type Error = Error;
20///     fn name(&self) -> &'static str {
21///         "fugue-my-new-tool"
22///     }
23///     fn is_available(&self) -> bool {
24///        todo!()
25///     }
26///     // ...
27///     fn import(&self, program: &Url) -> Result<Imported, Self::Error> {
28///         todo!()
29///     }
30///     fn is_preferred_for(&self, _: &Url) -> Option<bool> { todo!() }
31/// }
32/// ```
33pub trait Backend {
34    type Error: Into<crate::Error>;
35
36    fn name(&self) -> &'static str;
37
38    fn is_available(&self) -> bool;
39    fn is_preferred_for(&self, path: &Url) -> Option<bool>;
40
41    fn import(&self, program: &Url) -> Result<Imported, Self::Error>;
42}
43
44/// Wrapper for Backend 
45#[repr(transparent)]
46struct Importer<B, E>(B)
47where
48    B: Backend<Error = E>;
49
50impl<B, E> Backend for Importer<B, E>
51where
52    B: Backend<Error = E>,
53    E: Into<crate::Error>,
54{
55    type Error = crate::Error;
56
57    fn name(&self) -> &'static str {
58        self.0.name()
59    }
60
61    fn is_available(&self) -> bool {
62        self.0.is_available()
63    }
64
65    fn is_preferred_for(&self, path: &Url) -> Option<bool> {
66        self.0.is_preferred_for(path)
67    }
68
69    fn import(&self, program: &Url) -> Result<Imported, Self::Error> {
70        self.0.import(program).map_err(|e| e.into())
71    }
72}
73
74#[repr(transparent)]
75pub struct DatabaseImporterBackend(Box<dyn Backend<Error = crate::Error>>);
76
77impl DatabaseImporterBackend {
78    pub fn new<B, E>(backend: B) -> Self
79    where
80        B: Backend<Error = E> + 'static,
81        E: Into<crate::Error> + 'static,
82    {
83        Self(Box::new(Importer(backend)))
84    }
85}
86
87impl Deref for DatabaseImporterBackend {
88    type Target = dyn Backend<Error=crate::Error>;
89
90    fn deref(&self) -> &Self::Target {
91        &*self.0
92    }
93}