Skip to content

Commit

Permalink
chore: clippy + fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyg committed Jul 25, 2023
1 parent a600cb3 commit 7030515
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 98 deletions.
14 changes: 3 additions & 11 deletions dnas/demo/zomes/coordinator/demo/src/demo_prefix_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ pub fn remove_hashtag_from_index_a(hashtag: String) -> ExternResult<()> {
}

fn make_hashtag_index_text(text: String) -> String {
text
.split('#')
.nth(1)
.unwrap_or(&text)
.to_string()
text.split('#').nth(1).unwrap_or(&text).to_string()
}

#[hdk_extern]
Expand Down Expand Up @@ -180,9 +176,5 @@ pub fn remove_cashtag_from_index_a(cashtag: String) -> ExternResult<()> {
}

fn make_cashtag_index_text(text: String) -> String {
text
.split('$')
.nth(1)
.unwrap_or(&text)
.to_string()
}
text.split('$').nth(1).unwrap_or(&text).to_string()
}
2 changes: 1 addition & 1 deletion dnas/demo/zomes/coordinator/demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ pub fn post_commit(committed_actions: Vec<SignedActionHashed>) {
}

/// Don't modify this function if you want the scaffolding tool to generate appropriate signals for your entries and links
fn signal_action(action: SignedActionHashed) -> ExternResult<()> {
fn signal_action(_action: SignedActionHashed) -> ExternResult<()> {
Ok(())
}
70 changes: 42 additions & 28 deletions dnas/demo/zomes/integrity/demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,47 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {

match op.flattened::<(), LinkTypes>()? {
FlatOp::StoreEntry(store_entry) => match store_entry {
OpEntry::CreateEntry { app_entry, action } => Ok(ValidateCallbackResult::Invalid(
OpEntry::CreateEntry {
app_entry: _,
action: _,
} => Ok(ValidateCallbackResult::Invalid(
"There are no entry types in this integrity zome".to_string(),
)),
OpEntry::UpdateEntry {
app_entry, action, ..
app_entry: _,
action: _,
..
} => Ok(ValidateCallbackResult::Invalid(
"There are no entry types in this integrity zome".to_string(),
)),
_ => Ok(ValidateCallbackResult::Valid),
},
FlatOp::RegisterUpdate(update_entry) => match update_entry {
OpUpdate::Entry {
original_action,
original_app_entry,
app_entry,
action,
original_action: _,
original_app_entry: _,
app_entry: _,
action: _,
} => Ok(ValidateCallbackResult::Invalid(
"There are no entry types in this integrity zome".to_string(),
)),
_ => Ok(ValidateCallbackResult::Valid),
},
FlatOp::RegisterDelete(delete_entry) => match delete_entry {
OpDelete::Entry {
original_action,
original_app_entry,
action,
original_action: _,
original_app_entry: _,
action: _,
} => Ok(ValidateCallbackResult::Invalid(
"There are no entry types in this integrity zome".to_string(),
)),
_ => Ok(ValidateCallbackResult::Valid),
},
FlatOp::RegisterCreateLink {
link_type,
base_address,
target_address,
tag,
base_address: _,
target_address: _,
tag: _,
action,
} => match link_type {
LinkTypes::PrefixIndexA => prefix_index_a.validate_create_link(action),
Expand All @@ -126,9 +131,9 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
},
FlatOp::RegisterDeleteLink {
link_type,
base_address,
target_address,
tag,
base_address: _,
target_address: _,
tag: _,
original_action,
action,
} => match link_type {
Expand All @@ -140,16 +145,19 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
// Complementary validation to the `StoreEntry` Op, in which the record itself is validated
// If you want to optimize performance, you can remove the validation for an entry type here and keep it in `StoreEntry`
// Notice that doing so will cause `must_get_valid_record` for this record to return a valid record even if the `StoreEntry` validation failed
OpRecord::CreateEntry { app_entry, action } => Ok(ValidateCallbackResult::Invalid(
OpRecord::CreateEntry {
app_entry: _,
action: _,
} => Ok(ValidateCallbackResult::Invalid(
"There are no entry types in this integrity zome".to_string(),
)),
// Complementary validation to the `RegisterUpdate` Op, in which the record itself is validated
// If you want to optimize performance, you can remove the validation for an entry type here and keep it in `StoreEntry` and in `RegisterUpdate`
// Notice that doing so will cause `must_get_valid_record` for this record to return a valid record even if the other validations failed
OpRecord::UpdateEntry {
original_action_hash,
app_entry,
action,
original_action_hash: _,
app_entry: _,
action: _,
..
} => Ok(ValidateCallbackResult::Invalid(
"There are no entry types in this integrity zome".to_string(),
Expand All @@ -158,8 +166,8 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
// If you want to optimize performance, you can remove the validation for an entry type here and keep it in `RegisterDelete`
// Notice that doing so will cause `must_get_valid_record` for this record to return a valid record even if the `RegisterDelete` validation failed
OpRecord::DeleteEntry {
original_action_hash,
action,
original_action_hash: _,
action: _,
..
} => Ok(ValidateCallbackResult::Invalid(
"There are no entry types in this integrity zome".to_string(),
Expand All @@ -168,9 +176,9 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
// If you want to optimize performance, you can remove the validation for an entry type here and keep it in `RegisterCreateLink`
// Notice that doing so will cause `must_get_valid_record` for this record to return a valid record even if the `RegisterCreateLink` validation failed
OpRecord::CreateLink {
base_address,
target_address,
tag,
base_address: _,
target_address: _,
tag: _,
link_type,
action,
} => match link_type {
Expand All @@ -183,7 +191,7 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
// Notice that doing so will cause `must_get_valid_record` for this record to return a valid record even if the `RegisterDeleteLink` validation failed
OpRecord::DeleteLink {
original_action_hash,
base_address,
base_address: _,
action,
} => {
let record = must_get_valid_record(original_action_hash)?;
Expand All @@ -204,9 +212,15 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
};

match link_type {
LinkTypes::PrefixIndexA => prefix_index_a.validate_delete_link(action, create_link.clone()),
LinkTypes::PrefixIndexB => prefix_index_b.validate_delete_link(action, create_link.clone()),
LinkTypes::PrefixIndexC => prefix_index_c.validate_delete_link(action, create_link.clone()),
LinkTypes::PrefixIndexA => {
prefix_index_a.validate_delete_link(action, create_link)
}
LinkTypes::PrefixIndexB => {
prefix_index_b.validate_delete_link(action, create_link)
}
LinkTypes::PrefixIndexC => {
prefix_index_c.validate_delete_link(action, create_link)
}
}
}
OpRecord::CreatePrivateEntry { .. } => Ok(ValidateCallbackResult::Valid),
Expand Down
2 changes: 1 addition & 1 deletion lib/prefix_index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
//! Useful for type-ahead "search" or autocomplete features.
pub mod prefix_index;
pub use prefix_index::*;
mod utils;
mod validate;
mod utils;
19 changes: 8 additions & 11 deletions lib/prefix_index/src/prefix_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl PrefixIndex {

fn inner_remove_result(&self, text: String, full_text: Option<String>) -> ExternResult<()> {
let path = self
.make_result_path(text.clone(), full_text)?
.make_result_path(text, full_text)?
.typed(self.link_type)?;

self.inner_remove_result_from_path(path)?;
Expand Down Expand Up @@ -110,14 +110,14 @@ impl PrefixIndex {

// Get other children of parent of path
let mut other_children = vec![];
for i in children.clone().into_iter() {
for i in children.into_iter() {
if !result_children.contains(&i) {
other_children.push(i);
}
}

// If there are no other children of parent of path, delete parent of path
if other_children.len() == 0 && !parent.is_root() {
if other_children.is_empty() && !parent.is_root() {
self.inner_remove_result_from_path(parent)?;
}
}
Expand Down Expand Up @@ -202,7 +202,7 @@ impl PrefixIndex {
original_action: CreateLink,
) -> ExternResult<ValidateCallbackResult> {
validate_delete_link_prefix_index(
action.clone(),
action,
original_action.clone(),
original_action.base_address,
original_action.target_address,
Expand All @@ -228,20 +228,17 @@ impl PrefixIndex {
) -> ExternResult<Vec<String>> {
let results = self.get_results_from_path(path, limit, shuffle)?;

let leafs: Vec<Component> = results
let strings: Vec<String> = results
.into_iter()
.filter(|r| r.leaf().is_some())
.map(|p| p.leaf().unwrap().clone())
.collect();

let strings = leafs
.into_iter()
.filter_map(|c| String::try_from(&c).ok())
.collect();

Ok(strings)
}

#[allow(clippy::only_used_in_recursion)]
fn inner_get_results_from_path(
&self,
path: TypedPath,
Expand All @@ -253,7 +250,7 @@ impl PrefixIndex {
visited.push(path.clone());

let mut children = get_children_paths(path.clone())?;
match children.len() == 0 {
match children.is_empty() {
true => {
if path.exists()? && !results.contains(&path) && results.len() < limit {
results.push(path.clone());
Expand Down Expand Up @@ -287,7 +284,7 @@ impl PrefixIndex {
visited.clone(),
results.clone(),
)
.unwrap_or(vec![]);
.unwrap_or_default();

for grandchild in grandchildren.into_iter() {
if grandchild.exists()?
Expand Down
84 changes: 42 additions & 42 deletions lib/prefix_index/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
use hdk::{prelude::*, hash_path::path::Component};
use hdk::{hash_path::path::Component, prelude::*};

/// Duplicates of get_children from holochain TypedPath
/// but without calling ensure() on those children
pub fn get_children(path: TypedPath) -> ExternResult<Vec<Link>> {
let mut unwrapped = get_links(
path.path_entry_hash()?,
LinkTypeFilter::single_type(path.link_type.zome_index, path.link_type.zome_type),
None,
)?;
// Only need one of each hash to build the tree.
unwrapped.sort_unstable_by(|a, b| a.tag.cmp(&b.tag));
unwrapped.dedup_by(|a, b| a.tag.eq(&b.tag));
Ok(unwrapped)
let mut unwrapped = get_links(
path.path_entry_hash()?,
LinkTypeFilter::single_type(path.link_type.zome_index, path.link_type.zome_type),
None,
)?;
// Only need one of each hash to build the tree.
unwrapped.sort_unstable_by(|a, b| a.tag.cmp(&b.tag));
unwrapped.dedup_by(|a, b| a.tag.eq(&b.tag));
Ok(unwrapped)
}

/// Duplicates of get_children_paths from holochain TypedPath
/// but without calling ensure() on those children
pub fn get_children_paths(path: TypedPath) -> ExternResult<Vec<TypedPath>> {
let children = get_children(path.clone())?;
let components: ExternResult<Vec<Option<Component>>> = children
.into_iter()
.map(|link| {
let component_bytes = &link.tag.0[..];
if component_bytes.is_empty() {
Ok(None)
} else {
Ok(Some(
SerializedBytes::from(UnsafeBytes::from(component_bytes.to_vec()))
.try_into()
.map_err(|e: SerializedBytesError| wasm_error!(e))?,
))
}
})
.collect();
Ok(components?
.into_iter()
.map(|maybe_component| {
let mut new_path = path.path.clone();
if let Some(component) = maybe_component {
new_path.append_component(component);
}
new_path.into_typed(path.link_type)
})
.collect())
let children = get_children(path.clone())?;
let components: ExternResult<Vec<Option<Component>>> = children
.into_iter()
.map(|link| {
let component_bytes = &link.tag.0[..];
if component_bytes.is_empty() {
Ok(None)
} else {
Ok(Some(
SerializedBytes::from(UnsafeBytes::from(component_bytes.to_vec()))
.try_into()
.map_err(|e: SerializedBytesError| wasm_error!(e))?,
))
}
})
.collect();
Ok(components?
.into_iter()
.map(|maybe_component| {
let mut new_path = path.path.clone();
if let Some(component) = maybe_component {
new_path.append_component(component);
}
new_path.into_typed(path.link_type)
})
.collect())
}

pub fn path_to_string(path: TypedPath) -> String {
let component_strings: Vec<String> = path
.as_ref()
.into_iter()
.filter_map(|c| String::try_from(c).ok())
.collect();
let component_strings: Vec<String> = path
.as_ref()
.iter()
.filter_map(|c| String::try_from(c).ok())
.collect();

component_strings.join(".")
component_strings.join(".")
}
8 changes: 4 additions & 4 deletions lib/prefix_index/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ pub fn validate_create_link_prefix_index(
else if EntryHash::try_from(base_address).ok().map_or_else(
|| false,
|base_address_entry_hash| base_address_entry_hash == prefix_index_entry_hash,
) {
if tag_string.chars().count() != prefix_index.width {
return Ok(ValidateCallbackResult::Invalid("PrefixIndex second component: tag string must have same number of chars as prefix index width".into()));
}
) && tag_string.chars().count() != prefix_index.width
{
return Ok(ValidateCallbackResult::Invalid("PrefixIndex second component: tag string must have same number of chars as prefix index width".into()));
}

// third or later component
// unable to validate since we don't have any way of getting the previous links in the path
// (we can't assume this link author is also the previous link author, so we can't use must_get_agent_activity)
Expand Down

0 comments on commit 7030515

Please sign in to comment.