moduforge_runtime/
types.rs

1use std::{collections::HashMap, env::current_dir, path::PathBuf, sync::Arc};
2use async_trait::async_trait;
3
4use crate::{event::EventHandler, extension::Extension, mark::Mark, node::Node};
5use moduforge_model::{
6    node_pool::NodePool,
7    schema::{AttributeSpec, Schema},
8};
9
10#[async_trait]
11pub trait NodePoolFnTrait: Send + Sync + std::fmt::Debug {
12    async fn create(
13        &self,
14        schema: &Schema,
15    ) -> NodePool;
16}
17
18pub type GlobalAttributes = Vec<GlobalAttributeItem>;
19#[derive(Clone, PartialEq, Debug, Eq, Default)]
20pub struct GlobalAttributeItem {
21    pub types: Vec<String>,
22    pub attributes: HashMap<String, AttributeSpec>,
23}
24
25unsafe impl Send for GlobalAttributeItem {}
26unsafe impl Sync for GlobalAttributeItem {}
27
28#[derive(Clone, Debug)]
29pub enum Extensions {
30    N(Node),
31    M(Mark),
32    E(Extension),
33}
34
35#[derive(Clone, Debug, Default)]
36pub enum Content {
37    NodePool(NodePool),
38    NodePoolFn(Arc<dyn NodePoolFnTrait>),
39    #[default]
40    None,
41}
42
43#[derive(Clone, Debug)]
44pub struct StorageOptions {
45    pub storage_path: PathBuf,
46
47    pub l2_path: PathBuf,
48}
49impl Default for StorageOptions {
50    fn default() -> Self {
51        let path = current_dir().unwrap().join("./data");
52        Self { l2_path: path.join("db"), storage_path: path }
53    }
54}
55
56#[derive(Clone, Debug, Default)]
57pub struct EditorOptions {
58    content: Content,
59    extensions: Vec<Extensions>,
60    history_limit: Option<usize>,
61    event_handlers: Vec<Arc<dyn EventHandler>>,
62}
63impl EditorOptions {
64    pub fn get_content(&self) -> Content {
65        self.content.clone()
66    }
67    pub fn set_content(
68        mut self,
69        content: Content,
70    ) -> Self {
71        self.content = content;
72        self
73    }
74    pub fn get_extensions(&self) -> Vec<Extensions> {
75        self.extensions.clone()
76    }
77    pub fn set_extensions(
78        mut self,
79        extensions: Vec<Extensions>,
80    ) -> Self {
81        self.extensions = extensions;
82        self
83    }
84    pub fn get_history_limit(&self) -> Option<usize> {
85        self.history_limit
86    }
87    pub fn set_history_limit(
88        mut self,
89        history_limit: usize,
90    ) -> Self {
91        self.history_limit = Some(history_limit);
92        self
93    }
94
95    pub fn get_event_handlers(&self) -> Vec<Arc<dyn EventHandler>> {
96        self.event_handlers.clone()
97    }
98    pub fn set_event_handlers(
99        mut self,
100        event_handlers: Vec<Arc<dyn EventHandler>>,
101    ) -> Self {
102        self.event_handlers = event_handlers;
103        self
104    }
105}