[][src]Macro hdk::define_zome

macro_rules! define_zome {
    (
        entries : [
            $( $entry_expr:expr ),*
        ]

        genesis : || {
            $genesis_expr:expr
        }

        functions : {
            $(
                $cap:ident ( $vis:ident ) {
                    $(
                        $zome_function_name:ident : {
                            inputs: | $( $input_param_name:ident : $input_param_type:ty ),* |,
                            outputs: | $( $output_param_name:ident : $output_param_type:ty ),* |,
                            handler: $handler_path:path
                        }
                    )+
                }
            )*
        }

    ) => { ... };
}

Every Zome must utilize the define_zome macro in the main library file in their Zome. The define_zome macro has 3 component parts:

  1. entries: an array of ValidatingEntryType as returned by using the entry macro
  2. genesis: genesis is a callback called by Holochain to every Zome implemented within a DNA. It gets called when a new agent is initializing an instance of the DNA for the first time, and should return Ok or an Err, depending on whether the agent can join the network or not.
  3. functions: functions is divided up into capabilities, which specify who can access those functions. functions must be a tree structure where the first children are capabilities and the children of those capabilities are actual function definitions.

Examples

use holochain_core_types::cas::content::Address;



#[derive(Serialize, Deserialize, Debug, DefaultJson)]
pub struct Post {
    content: String,
    date_created: String,
}

fn handle_post_address(content: String) -> ZomeApiResult<Address> {
    let post_entry = Entry::App(AppEntryType::from("post"), Post {
        content,
        date_created: "now".into(),
    }.into());

    hdk::entry_address(&post_entry)
}

define_zome! {
    entries: [
        entry!(
            name: "post",
            description: "",
            sharing: Sharing::Public,
            native_type: Post,

            validation_package: || {
                hdk::ValidationPackageDefinition::ChainFull
            },

            validation: |post: Post, _ctx: hdk::ValidationData| {
                (post.content.len() < 280)
                    .ok_or_else(|| String::from("Content too long"))
            }
        )
    ]

    genesis: || {
        Ok(())
    }

    functions: {
        // "main" is the name of the capability
        // "Public" is the access setting of the capability
        main (Public) {
            // the name of this function, "post_address" is the
            // one to give while performing a `call` method to this function.
            // the name of the handler function must be different than the
            // name of the Zome function.
            post_address: {
                inputs: |content: String|,
                outputs: |post: ZomeApiResult<Address>|,
                handler: handle_post_address
            }
        }
    }
}