Skip to content

Instantly share code, notes, and snippets.

@simwilso
Last active December 9, 2019 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simwilso/23b0e8ba048167c7c715b5d25b1e8f42 to your computer and use it in GitHub Desktop.
Save simwilso/23b0e8ba048167c7c715b5d25b1e8f42 to your computer and use it in GitHub Desktop.
Basic Emit / Recieve Zome
#![feature(proc_macro_hygiene)]
#[macro_use]
extern crate hdk;
extern crate hdk_proc_macros;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
#[macro_use]
extern crate holochain_json_derive;
use std::convert::TryInto; //i added this line to your gist.
use hdk::{
entry_definition::ValidatingEntryType,
};
use hdk::holochain_core_types::{
dna::entry_types::Sharing,
};
use hdk::holochain_json_api::{
json::JsonString,
error::JsonError,
};
use hdk_proc_macros::zome;
// this is a struct for the signal String.
#[derive(Serialize, Deserialize, Debug, DefaultJson,Clone)]
pub struct Signal {
signal: String
}
#[zome]
mod signal_node {
#[init]
fn init() {
Ok(())
}
#[validate_agent]
pub fn validate_agent(validation_data: EntryValidationData<AgentId>) {
Ok(())
}
#[receive]
pub fn receive(_address: Address, _message: JsonString) -> String {
hdk::debug(format!("New message from: {:?}", _address)).ok();
let success: Result<Signal, _> = JsonString::from_json(&_message).try_into();
match success {
Err(err) => format!("error: {}", err),
Ok(message) => {
let _ = hdk::emit_signal(message.signal.as_str(), JsonString::from_json(&format!(
"{{message: {:#?}}}", message
)))
.to_string();
}
}
}
#[entry_def]
fn signal_entry_def() -> ValidatingEntryType {
entry!(
name: "signal",
description: "this is the current price enum as a string",
sharing: Sharing::Public,
validation_package: || {
hdk::ValidationPackageDefinition::Entry
},
validation: | _validation_data: hdk::EntryValidationData<Signal>| {
Ok(())
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment