[−][src]Macro hdk::define_zome 
Every Zome must utilize the define_zome
macro in the main library file in their Zome.
The define_zome macro has 3 component parts:
- entries: an array of ValidatingEntryType as returned by using the entry macro
- genesis: genesisis 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 returnOkor anErr, depending on whether the agent can join the network or not.
- functions: functionsis divided up intocapabilities, which specify who can access those functions.functionsmust be a tree structure where the first children arecapabilitiesand the children of thosecapabilitiesare actual function definitions.
Examples
#[macro_use] extern crate hdk; extern crate serde; #[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_json; extern crate boolinator; #[derive(Serialize, Deserialize)] pub struct Post { content: String, date_created: String, } fn handle_hash_post(content: String) -> serde_json::Value { let maybe_address = hdk::hash_entry("post", json!({ "content": content, "date_created": "now" })); match maybe_address { Ok(address) => { json!({"address": address}) } Err(hdk_error) => hdk_error.to_json(), } } 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, "hash_post" 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. hash_post: { inputs: |content: String|, outputs: |post: serde_json::Value|, handler: handle_hash_post } } } }