Skip to content

Commit

Permalink
chore: cargo fmt + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyg committed Aug 1, 2023
1 parent d7b7c11 commit 01967d6
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 528 deletions.
9 changes: 3 additions & 6 deletions crates/hc_zome_yjs_coordinator/src/all_documents.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use hdk::prelude::*;
use hc_zome_yjs_integrity::*;
use hdk::prelude::*;
#[hdk_extern]
pub fn get_all_documents(_: ()) -> ExternResult<Vec<Record>> {
let path = Path::from("all_documents");
let links = get_links(path.path_entry_hash()?, LinkTypes::AllDocuments, None)?;
let get_input: Vec<GetInput> = links
.into_iter()
.filter_map(|link| AnyDhtHash::try_from(link.target).ok())
.map(|hash| GetInput::new(
hash,
GetOptions::default(),
))
.map(|hash| GetInput::new(hash, GetOptions::default()))
.collect();
let records = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let records: Vec<Record> = records.into_iter().filter_map(|r| r).collect();
let records: Vec<Record> = records.into_iter().flatten().collect();
Ok(records)
}
30 changes: 12 additions & 18 deletions crates/hc_zome_yjs_coordinator/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use hdk::prelude::*;
use hc_zome_yjs_integrity::*;
use hdk::prelude::*;
#[hdk_extern]
pub fn create_document(document: Document) -> ExternResult<Record> {
let document_hash = create_entry(&EntryTypes::Document(document.clone()))?;
let record = get(document_hash.clone(), GetOptions::default())?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Could not find the newly created Document"))
),
)?;
let document_hash = create_entry(&EntryTypes::Document(document))?;
let record = get(document_hash.clone(), GetOptions::default())?.ok_or(wasm_error!(
WasmErrorInner::Guest(String::from("Could not find the newly created Document"))
))?;
let path = Path::from("all_documents");
create_link(
path.path_entry_hash()?,
document_hash.clone(),
document_hash,
LinkTypes::AllDocuments,
(),
)?;
Expand All @@ -29,8 +26,8 @@ pub fn get_document(original_document_hash: ActionHash) -> ExternResult<Option<R
.into_iter()
.max_by(|link_a, link_b| link_a.timestamp.cmp(&link_b.timestamp));
let latest_document_hash = match latest_link {
Some(link) => ActionHash::try_from(link.target.clone()).map_err(|e| wasm_error!(e))?,
None => original_document_hash.clone(),
Some(link) => ActionHash::try_from(link.target).map_err(|e| wasm_error!(e))?,
None => original_document_hash,
};
get(latest_document_hash, GetOptions::default())
}
Expand All @@ -47,17 +44,14 @@ pub fn update_document(input: UpdateDocumentInput) -> ExternResult<Record> {
&input.updated_document,
)?;
create_link(
input.original_document_hash.clone(),
input.original_document_hash,
updated_document_hash.clone(),
LinkTypes::DocumentUpdates,
(),
)?;
let record = get(updated_document_hash.clone(), GetOptions::default())?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Could not find the newly updated Document"))
),
)?;
let record = get(updated_document_hash, GetOptions::default())?.ok_or(wasm_error!(
WasmErrorInner::Guest(String::from("Could not find the newly updated Document"))
))?;
Ok(record)
}
#[hdk_extern]
Expand Down
4 changes: 2 additions & 2 deletions crates/hc_zome_yjs_coordinator/src/document_to_agents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub struct AddAgentForDocumentInput {
#[hdk_extern]
pub fn add_agent_for_document(input: AddAgentForDocumentInput) -> ExternResult<()> {
create_link(
input.base_document_hash.clone(),
input.target_agent.clone(),
input.base_document_hash,
input.target_agent,
LinkTypes::DocumentToAgents,
(),
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct AddStatevectorForDocumentInput {
#[hdk_extern]
pub fn add_statevector_for_document(input: AddStatevectorForDocumentInput) -> ExternResult<()> {
create_link(
input.base_document_hash.clone(),
input.target_statevector_hash.clone(),
input.base_document_hash,
input.target_statevector_hash,
LinkTypes::DocumentToStatevectors,
(),
)?;
Expand All @@ -34,7 +34,7 @@ pub fn get_statevectors_for_document(document_hash: ActionHash) -> ExternResult<
let records: Vec<Record> = HDK
.with(|hdk| hdk.borrow().get(get_input))?
.into_iter()
.filter_map(|r| r)
.flatten()
.collect();

Ok(records)
Expand All @@ -54,7 +54,7 @@ pub fn get_statevectors_for_document_delta(
all_statevectors
.iter()
.filter_map(|r| r.entry().to_app_option::<Statevector>().ok())
.filter_map(|s| s),
.flatten(),
);

let seen_statevectors_btreeset = BTreeSet::from_iter(input.statevectors.iter().cloned());
Expand Down
13 changes: 5 additions & 8 deletions crates/hc_zome_yjs_coordinator/src/statevector.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use hdk::prelude::*;
use hc_zome_yjs_integrity::*;
use hdk::prelude::*;

#[hdk_extern]
pub fn create_statevector(statevector: Statevector) -> ExternResult<Record> {
let statevector_hash = create_entry(EntryTypes::Statevector(statevector.clone()))?;
let record = get(statevector_hash.clone(), GetOptions::default())?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Could not find the newly created Statevector"))
),
)?;
let statevector_hash = create_entry(EntryTypes::Statevector(statevector))?;
let record = get(statevector_hash, GetOptions::default())?.ok_or(wasm_error!(
WasmErrorInner::Guest(String::from("Could not find the newly created Statevector"))
))?;
Ok(record)
}
#[hdk_extern]
Expand Down
49 changes: 18 additions & 31 deletions crates/hc_zome_yjs_integrity/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,24 @@ pub fn validate_create_link_document_updates(
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let action_hash = ActionHash::try_from(base_address)
.map_err(|e| wasm_error!(e))?;
let action_hash = ActionHash::try_from(base_address).map_err(|e| wasm_error!(e))?;
let record = must_get_valid_record(action_hash)?;
let _document: crate::Document = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Linked action must reference an entry"))
),
)?;
let action_hash = ActionHash::try_from(target_address)
.map_err(|e| wasm_error!(e))?;
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
let action_hash = ActionHash::try_from(target_address).map_err(|e| wasm_error!(e))?;
let record = must_get_valid_record(action_hash)?;
let _document: crate::Document = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Linked action must reference an entry"))
),
)?;
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_document_updates(
Expand All @@ -64,30 +58,25 @@ pub fn validate_delete_link_document_updates(
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Ok(
ValidateCallbackResult::Invalid(
String::from("DocumentUpdates links cannot be deleted"),
),
)
Ok(ValidateCallbackResult::Invalid(String::from(
"DocumentUpdates links cannot be deleted",
)))
}
pub fn validate_create_link_all_documents(
_action: CreateLink,
_base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let action_hash = ActionHash::try_from(target_address)
.map_err(|e| wasm_error!(e))?;
let action_hash = ActionHash::try_from(target_address).map_err(|e| wasm_error!(e))?;
let record = must_get_valid_record(action_hash)?;
let _document: crate::Document = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Linked action must reference an entry"))
),
)?;
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_all_documents(
Expand All @@ -97,9 +86,7 @@ pub fn validate_delete_link_all_documents(
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Ok(
ValidateCallbackResult::Invalid(
String::from("AllDocuments links cannot be deleted"),
),
)
Ok(ValidateCallbackResult::Invalid(String::from(
"AllDocuments links cannot be deleted",
)))
}
21 changes: 11 additions & 10 deletions crates/hc_zome_yjs_integrity/src/document_to_agents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ pub fn validate_create_link_document_to_agents(
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
// Check the entry type for the given action hash
let action_hash = ActionHash::try_from(base_address)
.map_err(|e| wasm_error!(e))?;
let action_hash = ActionHash::try_from(base_address).map_err(|e| wasm_error!(e))?;
let record = must_get_valid_record(action_hash)?;
let _document: crate::Document = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Linked action must reference an entry"))
),
)?;
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
// Check the target hash is an agentpubkey
let _ = AgentPubKey::try_from(target_address).map_err(|e| wasm_error!(e))?;

Ok(ValidateCallbackResult::Valid)
}

Expand All @@ -34,12 +31,16 @@ pub fn validate_delete_link_document_to_agents(
) -> ExternResult<ValidateCallbackResult> {
// Check the delete action author matchs the create action author
if action.author != original_action.author {
return Ok(ValidateCallbackResult::Invalid("Delete action author must match create action author".into()));
return Ok(ValidateCallbackResult::Invalid(
"Delete action author must match create action author".into(),
));
}

// Check the delete action author is the target address
if action.author != AgentPubKey::try_from(target_address).map_err(|e| wasm_error!(e))? {
return Ok(ValidateCallbackResult::Invalid("Delete action author match link target".into()));
return Ok(ValidateCallbackResult::Invalid(
"Delete action author match link target".into(),
));
}

Ok(ValidateCallbackResult::Valid)
Expand Down
16 changes: 6 additions & 10 deletions crates/hc_zome_yjs_integrity/src/document_to_statevectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ pub fn validate_create_link_document_to_statevectors(
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Linked action must reference an entry"))
),
)?;
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
// Check the entry type for the given entry hash
let entry_hash = EntryHash::try_from(target_address).map_err(|e| wasm_error!(e))?;
let entry = must_get_entry(entry_hash)?.content;
Expand All @@ -31,9 +29,7 @@ pub fn validate_delete_link_document_to_statevectors(
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Ok(
ValidateCallbackResult::Invalid(
String::from("DocumentToStatevectors links cannot be deleted"),
),
)
Ok(ValidateCallbackResult::Invalid(String::from(
"DocumentToStatevectors links cannot be deleted",
)))
}
Loading

0 comments on commit 01967d6

Please sign in to comment.