Skip to main content

create_workspace

Function create_workspace 

Source
pub async fn create_workspace<Func: CreateWorkspaceIoFunction + Send + Sync + 'static>(
    config: Arc<Config>,
    path_fields: &HashMap<FieldKey, PathValue>,
    template_fields: Arc<HashMap<FieldKey, TemplateValue>>,
    io_function: Func,
) -> Result<(), Error>
Expand description

Build a workspace by creating the files and folders for the given fields.

The create workspace function will use the path_fields to decide if a path should be built or not. In other words, this will create paths that can be resolved with the path fields, but other paths will not be created.

ยงExample

struct Func;

#[async_trait::async_trait]
impl CreateWorkspaceIoFunction for Func {
    async fn call(
        &self,
        _config: std::sync::Arc<Config>,
        _template_fields: std::sync::Arc<std::collections::HashMap<FieldKey, TemplateValue>>,
        _path_item: ResolvedPathItem,
    ) -> Result<(), Error> {
        Ok(())
    }
}

let config = ConfigBuilder::new()
    .add_path_item(PathItemArgs {
        key: "key1".try_into().unwrap(),
        path: "/path/to/{thing}".into(),
        parent: None,
        permission: Permission::default(),
        owner: Owner::default(),
        path_type: PathType::default(),
        deferred: false,
        metadata: std::collections::HashMap::new(),
    })
    .unwrap()
    .add_path_item(PathItemArgs {
        key: "key2".try_into().unwrap(),
        path: "/path/to/a/{thing}".into(),
        parent: None,
        permission: Permission::default(),
        owner: Owner::default(),
        path_type: PathType::default(),
        deferred: false,
        metadata: std::collections::HashMap::new(),
    })
    .unwrap()
    .add_path_item(PathItemArgs {
        key: "key3".try_into().unwrap(),
        path: "/path/to/b/{thing}".into(),
        parent: None,
        permission: Permission::default(),
        owner: Owner::default(),
        path_type: PathType::default(),
        deferred: false,
        metadata: std::collections::HashMap::new(),
    })
    .unwrap()
    .build()
    .unwrap();

let path_fields = {
    let mut fields = std::collections::HashMap::new();
    fields.insert("thing".try_into().unwrap(), "value".into());

    fields
};
let template_fields = {
    let mut fields = std::collections::HashMap::new();
    fields.insert("thing".try_into().unwrap(), "value".into());

    fields
};

create_workspace(
    std::sync::Arc::new(config),
    &path_fields,
    std::sync::Arc::new(template_fields),
    Func,
)
.await
.unwrap();