Advertisement
jcray

Untitled

Jun 23rd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.08 KB | None | 0 0
  1. #![feature(try_from)]
  2. #[macro_use]
  3. extern crate hdk;
  4. extern crate serde;
  5. #[macro_use]
  6. extern crate serde_derive;
  7. extern crate serde_json;
  8. #[macro_use]
  9. extern crate holochain_core_types_derive;
  10.  
  11. use hdk::{
  12.     entry_definition::ValidatingEntryType,
  13.     error::ZomeApiResult,
  14. };
  15. use hdk::holochain_core_types::{
  16.     cas::content::Address,
  17.     entry::Entry,
  18.     dna::entry_types::Sharing,
  19.     error::HolochainError,
  20.     json::JsonString,
  21.     validation::EntryValidationData
  22. };
  23.  
  24. // see https://developer.holochain.org/api/0.0.20-alpha3/hdk/ for info on using the hdk library
  25.  
  26. // This is a sample zome that defines an entry type "MyEntry" that can be committed to the
  27. // agent's chain via the exposed function create_my_entry
  28.  
  29. #[derive(Serialize, Deserialize, Debug, DefaultJson,Clone)]
  30. pub struct MyEntry {
  31.     content: String,
  32. }
  33.  
  34. pub fn handle_create_my_entry(entry: MyEntry) -> ZomeApiResult<Address> {
  35.     let entry = Entry::App("my_entry".into(), entry.into());
  36.     let address = hdk::commit_entry(&entry)?;
  37.     Ok(address)
  38. }
  39.  
  40. pub fn handle_get_my_entry(address: Address) -> ZomeApiResult<Option<Entry>> {
  41.     hdk::get_entry(&address)
  42. }
  43.  
  44. fn definition() -> ValidatingEntryType {
  45.     entry!(
  46.         name: "my_entry",
  47.         description: "this is a same entry defintion",
  48.         sharing: Sharing::Public,
  49.         validation_package: || {
  50.             hdk::ValidationPackageDefinition::Entry
  51.         },
  52.  
  53.         validation: | _validation_data: hdk::EntryValidationData<MyEntry>| {
  54.             Ok(())
  55.         }
  56.     )
  57. }
  58.  
  59. define_zome! {
  60.     entries: [
  61.        definition()
  62.     ]
  63.  
  64.     genesis: || { Ok(()) }
  65.  
  66.     functions: [
  67.         create_my_entry: {
  68.             inputs: |entry: MyEntry|,
  69.             outputs: |result: ZomeApiResult<Address>|,
  70.             handler: handle_create_my_entry
  71.         }
  72.         get_my_entry: {
  73.             inputs: |address: Address|,
  74.             outputs: |result: ZomeApiResult<Option<Entry>>|,
  75.             handler: handle_get_my_entry
  76.         }
  77.     ]
  78.  
  79.     traits: {
  80.         hc_public [create_my_entry,get_my_entry]
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement