moduforge_core/helpers/
create_doc.rs

1use std::sync::Arc;
2
3use moduforge_state::StateConfig;
4
5use crate::{types::Content, EditorResult};
6
7/// 创建文档
8pub async fn create_doc(
9    content: &Content,
10    config: &mut StateConfig,
11) -> EditorResult<()> {
12    match content {
13        Content::NodePool(node_pool) => {
14            config.doc = Some(Arc::new(node_pool.clone()));
15        },
16        Content::None => {
17            config.doc = None;
18        },
19        Content::NodePoolFn(node_pool_fn_trait) => {
20            config.doc =
21                Some(Arc::new(node_pool_fn_trait.create(&config).await?));
22        },
23    };
24    if let Some(doc) = &config.doc {
25        if let Err(err) = doc.validate_hierarchy() {
26            return Err(anyhow::anyhow!(err.to_string()));
27        }
28    }
29    Ok(())
30}