mf_core/helpers/
create_doc.rs

1use std::sync::Arc;
2
3use mf_state::StateConfig;
4
5use crate::{error::ForgeError, types::Content, ForgeResult};
6
7/// 创建文档
8pub async fn create_doc(
9    content: &Content,
10    config: &mut StateConfig,
11) -> ForgeResult<()> {
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(ForgeError::Validation {
27                message: err.to_string(),
28                field: None,
29            });
30        }
31    }
32    Ok(())
33}