rave_engine 0.3.4

A secure and efficient JSON Schema validation and Rhai script execution engine
Documentation
// Core Types
pub mod common;
pub use common::*;
pub mod units;
pub use units::*;
pub mod ledger;
pub use ledger::*;

// Entry Types
pub mod entries;
pub use entries::*;

// Transaction & Return Types
pub mod ext_types;
pub use ext_types::*;
// Utility Types
pub mod tags;
pub use tags::*;
pub mod permission_space;
pub use permission_space::PermissionSpace;

// Re-export hdi/hdk preludes for convenience
// Note: We don't re-export hdi::prelude::* to avoid ambiguous glob re-exports
// Users should import hdi::prelude::* directly if needed
use hdk::prelude::*;
use serde::Deserializer;

#[derive(Serialize, Deserialize)]
#[hdk_link_types]
pub enum LinkTypes {
    InitAgent,

    // Links for Negotiation Phase
    ProposalNotification,   // Notifies counterparty of proposals
    CommitmentNotification, // Notifies counterparty of commitments
    AcceptNotification,     // Notifies counterparty of accepts
    RejectNotification,     // Notifies counterparty of rejects
    // ReclaimNotification,    // Notifies counterparty of reclaims
    // ReceiptNotification,    // Notifies counterparty of receipts

    // Links for RAVE
    CodeTemplateToSmartAgreements,
    SmartAgreementToRAVEs,
    AllCodeTemplates,
    AllMyCodeTemplates,
    AllMySmartAgreements,
    // SmartAgreementUpdates,

    // Parked Links
    ParkedData,
    ParkedSpendCredit,
    ParkedSpendBalance,

    // Agent Links
    NameableUnits,
    RatedUnitsLinks,

    // NotificationLinks
    SendExecutorParkedLinkNotification,
    SendSpenderParkedInvoiceNotification,
    SendReceiverDepositNotification,
    SystemAnchor,
    LaneDefinition,
    // todo: remove the need for this when we have progenitor setup for UI's
    GlobalUnits,

    // Tags
    PrefixPath,
    PathToEA,
    PathToCT,
    PathToDataBlob,
    PathToDocDef,

    // DocDef Links
    DocDefToDataBlob,
}

#[derive(Serialize, Debug, Clone)]
pub struct ZomeFnInput<T: Default> {
    #[serde(default)]
    pub input: T,
    #[serde(default, skip_serializing_if = "is_false")]
    pub local: bool,
}

// Custom Deserialize implementation that handles null
impl<'de, T: Default + Deserialize<'de>> Deserialize<'de> for ZomeFnInput<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
    where
        D: Deserializer<'de>,
    {
        // to avoid infinite recursion, we use a helper struct
        #[derive(Deserialize)]
        struct Helper<T> {
            #[serde(default)]
            input: T,
            #[serde(default)]
            local: bool,
        }

        let helper = Option::<Helper<T>>::deserialize(deserializer)?;
        match helper {
            Some(h) => Ok(ZomeFnInput {
                input: h.input,
                local: h.local,
            }),
            None => Ok(ZomeFnInput {
                input: T::default(),
                local: false,
            }),
        }
    }
}

fn is_false(b: &bool) -> bool {
    !*b
}

impl<T: Default> ZomeFnInput<T> {
    pub fn new(input: T, local: Option<bool>) -> Self {
        Self {
            input,
            local: local.unwrap_or(false),
        }
    }

    pub fn with_local(input: T, local: bool) -> Self {
        Self { input, local }
    }

    pub fn get_strategy(&self) -> GetStrategy {
        if self.local {
            GetStrategy::Local
        } else {
            GetStrategy::Network
        }
    }
}

impl<T: Default> From<ZomeFnInput<T>> for GetStrategy {
    fn from(val: ZomeFnInput<T>) -> Self {
        if val.local {
            GetStrategy::Local
        } else {
            GetStrategy::Network
        }
    }
}

impl<T: Default> From<ZomeFnInput<T>> for GetOptions {
    fn from(val: ZomeFnInput<T>) -> Self {
        if val.local {
            GetOptions::local()
        } else {
            GetOptions::network()
        }
    }
}

/// We use Tags as Folders and there are the 3 main folders:
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes, Default)]
pub enum FolderType {
    #[default]
    Public,
    System,
    Lane,
}