tags/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use hdk::{hdi::hash_path::path::root_hash, prelude::*};

pub fn create_link_relaxed<T, E>(
    base_address: impl Into<AnyLinkableHash>,
    target_address: impl Into<AnyLinkableHash>,
    link_type: T,
    tag: impl Into<LinkTag>,
) -> ExternResult<()>
where
    ScopedLinkType: TryFrom<T, Error = E>,
    WasmError: From<E>,
{
    let ScopedLinkType {
        zome_index,
        zome_type: link_type,
    } = link_type.try_into()?;
    HDK.with(|h| {
        h.borrow().create_link(CreateLinkInput::new(
            base_address.into(),
            target_address.into(),
            zome_index,
            link_type,
            tag.into(),
            ChainTopOrdering::Relaxed,
        ))
    })?;

    Ok(())
}

pub fn delete_link_relaxed(address: ActionHash) -> ExternResult<()> {
    HDK.with(|h| {
        h.borrow()
            .delete_link(DeleteLinkInput::new(address, ChainTopOrdering::Relaxed))
    })?;

    Ok(())
}

pub fn ensure_relaxed(path: &TypedPath) -> ExternResult<()> {
    if !path.exists()? {
        if path.is_root() {
            create_link_relaxed(
                root_hash()?,
                path.path_entry_hash()?,
                path.link_type,
                path.make_tag()?,
            )?;
        } else if let Some(parent) = path.parent() {
            ensure_relaxed(&parent)?;
            create_link_relaxed(
                parent.path_entry_hash()?,
                path.path_entry_hash()?,
                path.link_type,
                path.make_tag()?,
            )?;
        }
    }
    Ok(())
}