Skip to content

Commit

Permalink
TrustAtom link tag has optional extra entry hash
Browse files Browse the repository at this point in the history
  • Loading branch information
dauphin3 authored and harlantwood committed Feb 28, 2022
1 parent 4d6a0ed commit 296bb45
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 220 deletions.
8 changes: 8 additions & 0 deletions bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ test_metal () {
shell "time cargo test -- --nocapture"
}

build_watch () {
shell "cargo watch -- bin/run build"
}

build_watch_clear () {
shell "cargo watch --clear -- bin/run build"
}

build () {
run build_happ
}
Expand Down
42 changes: 31 additions & 11 deletions zomes/hc_zome_trust_atom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,61 @@
// #![warn(clippy::cargo)]

use hdk::prelude::*;
use std::collections::BTreeMap;

// public for sweettest; TODO can we fix this:
pub mod trust_atom;
pub use crate::trust_atom::*;

pub mod test_helpers;
// pub use crate::test_helpers;

entry_defs![StringTarget::entry_def()];
entry_defs![test_helpers::StringTarget::entry_def(), Extra::entry_def()];

// INPUT TYPES

#[derive(Serialize, Deserialize, SerializedBytes, Debug, Clone)]
pub struct TrustAtomInput {
pub target: EntryHash, // TODO maybe target_entry_hash?
pub content: Option<String>,
pub value: Option<String>,
pub extra: Option<BTreeMap<String, String>>,
}

#[derive(Serialize, Deserialize, SerializedBytes, Debug, Clone)]
pub struct QueryInput {
pub source: Option<EntryHash>,
pub target: Option<EntryHash>,
pub content_full: Option<String>,
pub content_starts_with: Option<String>,
pub min_rating: Option<String>,
pub value_starts_with: Option<String>,
}

#[derive(Serialize, Deserialize, SerializedBytes, Debug, Clone)]
pub struct QueryMineInput {
pub target: Option<EntryHash>,
pub content_full: Option<String>,
pub content_starts_with: Option<String>,
pub min_rating: Option<String>,
pub value_starts_with: Option<String>,
}

// ZOME API FUNCTIONS

#[hdk_extern]
pub fn create_trust_atom(input: TrustAtomInput) -> ExternResult<()> {
trust_atom::create(input.target, &input.content, &input.value, input.attributes)
pub fn create_trust_atom(input: TrustAtomInput) -> ExternResult<TrustAtom> {
let trust_atom = trust_atom::create(input.target, input.content, input.value, input.extra)?;
Ok(trust_atom)
}

#[hdk_extern]
#[allow(clippy::needless_pass_by_value)]
pub fn get_extra(entry_hash: EntryHash) -> ExternResult<Extra> {
let extra = trust_atom::get_extra(&entry_hash)?;
Ok(extra)
}

#[hdk_extern]
pub fn calc_extra_hash(input: Extra) -> ExternResult<EntryHash> {
let hash = trust_atom::calc_extra_hash(input)?;
Ok(hash)
}

#[hdk_extern]
Expand All @@ -54,7 +75,7 @@ pub fn query(input: QueryInput) -> ExternResult<Vec<TrustAtom>> {
input.target,
input.content_full,
input.content_starts_with,
input.min_rating,
input.value_starts_with,
)
}

Expand All @@ -64,15 +85,14 @@ pub fn query_mine(input: QueryMineInput) -> ExternResult<Vec<TrustAtom>> {
input.target,
input.content_full,
input.content_starts_with,
input.min_rating,
input.value_starts_with,
)
}

// TEST HELPERS

#[hdk_extern]
pub fn create_string_target(input: String) -> ExternResult<EntryHash> {
crate::trust_atom::create_string_target(input)
crate::test_helpers::create_string_target(input)
}

#[hdk_extern]
Expand Down
17 changes: 15 additions & 2 deletions zomes/hc_zome_trust_atom/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

use hdk::prelude::*;

#[derive(Serialize, Deserialize, Debug, SerializedBytes)]
struct StringLinkTag(String);

#[hdk_entry(id = "restaurant", visibility = "public")]
#[derive(Clone)]
pub struct StringTarget(String);

pub fn list_links_for_base(base: EntryHash) -> ExternResult<Vec<Link>> {
let links = hdk::link::get_links(base, None)?;

Expand All @@ -19,8 +26,14 @@ pub fn list_links(base: EntryHash, link_tag_text: Option<String>) -> ExternResul
Ok(links)
}

#[derive(Serialize, Deserialize, Debug, SerializedBytes)]
struct StringLinkTag(String);
pub fn create_string_target(input: String) -> ExternResult<EntryHash> {
let string_target = StringTarget(input);

create_entry(string_target.clone())?;

let target_entry_hash = hash_entry(string_target)?;
Ok(target_entry_hash)
}

fn link_tag(tag: String) -> ExternResult<LinkTag> {
let serialized_bytes: SerializedBytes = StringLinkTag(tag).try_into()?;
Expand Down
Loading

0 comments on commit 296bb45

Please sign in to comment.