Skip to content

Instantly share code, notes, and snippets.

@freesig
Created December 5, 2019 06:13
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 freesig/1a2604d4c4ee7cdb2962ae582d580a43 to your computer and use it in GitHub Desktop.
Save freesig/1a2604d4c4ee7cdb2962ae582d580a43 to your computer and use it in GitHub Desktop.
#![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(())
}
)
}
}
@simwilso
Copy link

simwilso commented Dec 9, 2019

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:

[nix-shell:~/Code/emit_signal_zome/signal_zome]$ hc package
> cargo build --release --target=wasm32-unknown-unknown
   Compiling signal_node v0.1.0 (/home/simwilso/Code/emit_signal_zome/signal_zome/zomes/signal_node/code)
error[E0599]: no method named `to_string` found for type `std::result::Result<(), hdk::error::ZomeApiError>` in the current scope
  --> src/lib.rs:59:22
   |
59 |                     .to_string();
   |                      ^^^^^^^^^
   |
   = note: the method `to_string` exists but the following trait bounds were not satisfied:
           `std::result::Result<(), hdk::error::ZomeApiError> : std::string::ToString`

error[E0308]: mismatched types
  --> src/lib.rs:55:24
   |
55 |                   Ok(message) =>  {
   |  _________________________________^
56 | |                     let _ = hdk::emit_signal(message.signal.as_str(), JsonString::from_json(&format!(
57 | |                         "{{message: {:#?}}}", message
58 | |                     )))
59 | |                     .to_string();
60 | |                 }
   | |_________________^ expected struct `std::string::String`, found ()
   |
   = note: expected type `std::string::String`
              found type `()`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: Could not compile `signal_node`.

To learn more, run the command again with --verbose.
Error: Couldn't traverse DNA in directory "/home/simwilso/Code/emit_signal_zome/signal_zome": command cargo build --release --target=wasm32-unknown-unknown was not successful

@freesig
Copy link
Author

freesig commented Dec 9, 2019

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