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
define_api_id!(0x1bce_a30c_8000_c979, "behavior-controller-v1");

pub type ListModulesHandle = u32;
pub type LoadModuleHandle = u32;

use super::behavior_controller_v0::BehaviorModuleId;

/// Metadata for a single behavior in a behavior module
#[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct BehaviorMeta {
    /// The name of the behavior
    pub name: String,
}

/// Metadata of a behavior module
#[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct BehaviorModuleMeta {
    /// The name of the behavior module
    pub name: String,
    /// The `ModuleDescCid` of the module that implements the behavior(s)
    pub id: BehaviorModuleId,
    /// A list of behavior metadata for each behavior in the behavior module
    pub behavior_metas: Vec<BehaviorMeta>,
}

#[ark_api_macros::ark_bindgen(imports = "ark-behavior-controller-v1")]
mod behavior_controller {
    use super::*;
    use crate::FFIResult;

    extern "C" {
        /// Get a list of available behavior modules from the connected module store service.
        pub fn list_behavior_modules() -> ListModulesHandle;

        /// Retrieve the JSON serialized list of available behavior modules from the module store
        /// service.
        pub fn retrieve_module_list(handle: ListModulesHandle) -> FFIResult<Vec<u8>>;

        /// Starts an asynchronous retrieval of a behavior module.
        ///
        /// Will try to use the module store service, but may use the CAS store if modules aren't
        /// found in the module store service.
        pub fn load_module(module_cid: &str) -> FFIResult<LoadModuleHandle>;

        /// Retrieve the JSON serialized registration info for the behavior module
        ///
        /// # Errors
        ///
        /// The following errors should be handled gracefully by users of the FFI
        ///
        /// - Returns [`crate::ErrorCode::Unavailable`] when the loading process is unfinished
        /// - Returns [`crate::ErrorCode::NotFound`] when the requested module cannot be found
        pub fn retrieve_module(handle: LoadModuleHandle) -> FFIResult<Vec<u8>>;
    }
}

pub use behavior_controller::*;