rave_engine 0.8.0

A secure and efficient JSON Schema validation and Rhai script execution engine
Documentation
use hdk::prelude::*;
mod get_latest_records;
pub use get_latest_records::*;
mod get_links_and_load_types;
pub use get_links_and_load_types::*;
mod commit_idempotent;
pub use commit_idempotent::*;
mod create_idempotent_link;
pub use create_idempotent_link::*;
mod get_from;
pub use get_from::*;
mod local_source_chain;
pub use local_source_chain::*;
mod get_details;
// pub use get_details::*;
mod remove_link;
pub use remove_link::*;
mod get_entry_all_versions;
pub use get_entry_all_versions::*;
mod get_links_and_load_type_all_versions;
pub use get_links_and_load_type_all_versions::*;

/// optimized get details by links
/// TODO: multiple duplicates with slight variations in the links
pub fn hc_utils_get_details(
    links: Vec<Link>,
    option: GetOptions,
) -> ExternResult<Vec<(Details, Vec<Link>, ActionHash)>> {
    let links: Vec<(AnyDhtHash, Link)> = links
        .iter()
        .filter_map(|l| match l.clone().target.into_entry_hash() {
            Some(e) => Some((e.into(), l.clone())),
            None => l
                .clone()
                .target
                .into_action_hash()
                .map(|a| (a.into(), l.clone())),
        })
        .collect();
    let msg_results_input: Vec<GetInput> = links
        .clone()
        .into_iter()
        .map(|link| GetInput::new(link.0, option.clone()))
        .collect();
    let list = HDK.with(|hdk| hdk.borrow().get_details(msg_results_input))?;

    Ok(list
        .iter()
        .filter_map(|d| match d {
            Some(details) => {
                if let Details::Record(r) = details {
                    let action_hash = r.record.action_address().clone();
                    let any_hash: AnyDhtHash = action_hash.clone().into();
                    // let found = links
                    //     .iter()
                    //     .find_map(|(h, l)| if h == &any_hash { Some(l) } else { None });
                    // find all links with the target hash
                    let found_links = links
                        .iter()
                        .filter(|(h, _)| h == &any_hash)
                        .map(|(_, l)| l.clone())
                        .collect::<Vec<_>>();
                    Some((details.clone(), found_links, action_hash))
                } else {
                    //  Details::Entry(e): this is not needed for our app, but thats because we will alway link to ActionHash
                    None
                }
            }
            None => None,
        })
        .collect())
}

// todo: update the utils
/// TODO: multiple duplicates with slight variations in the links
pub fn hc_utils_get_links_and_load_type<R: TryFrom<Entry>>(
    query: LinkQuery,
    strategy: GetStrategy,
) -> ExternResult<Vec<get_links_and_load_type_all_versions::CustomLoadedLinks<R>>> {
    let link_info = get_links(query, strategy)?;
    let entries: Vec<LatestRecord> = get_latest_records(link_info, GetOptions::default())?;
    Ok(entries
        .iter()
        .flat_map(|latest_record| {
            let LatestRecord {
                original_record_hash,
                record,
                link,
            } = latest_record;
            if let RecordEntry::Present(entry) = &record.entry {
                match R::try_from(entry.clone()) {
                    Ok(e) => Ok(get_links_and_load_type_all_versions::CustomLoadedLinks {
                        original_record_hash: original_record_hash.clone(),
                        entry: e,
                        record: record.clone(),
                        found_links: vec![link.clone()],
                    }),
                    Err(_) => Err(wasm_error!(
                        "Could not convert get_links result to requested type"
                    )),
                }
            } else {
                Err(wasm_error!("get_links did not return an app entry"))
            }
        })
        .collect())
}