Created
December 5, 2019 06:13
-
-
Save freesig/1a2604d4c4ee7cdb2962ae582d580a43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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 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_zome { | |
#[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(()) | |
} | |
) | |
} | |
} |
Whoops I uploaded the wrong one.
#![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 hdk::entry_definition::ValidatingEntryType;
use hdk::holochain_core_types::dna::entry_types::Sharing;
use hdk::holochain_json_api::{error::JsonError, json::JsonString};
use hdk_proc_macros::zome;
use std::convert::TryInto;
use serde_json::json;
// this is a struct for the signal String.
#[derive(Serialize, Deserialize, Debug, DefaultJson, Clone)]
pub struct Signal {
signal: String,
}
#[zome]
mod signal_zome {
#[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 r = hdk::emit_signal(
message.signal.as_str(),
JsonString::from_json(&format!("{{message: {:?}}}", message)),
);
json!(r).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
Hi Tom.
Sorry as I'm sure I'm stuffing something really simple up here but I couldn't get this to compile still.
I get a return type error. I managed to address a couple of errors; had to add a 'use' statement for tryinto(), and a close delimiter on the receive function.
Here's my update of the GIST with those two changes - https://gist.github.com/simwilso/23b0e8ba048167c7c715b5d25b1e8f42
The version above removed those errors but still having this error below: