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
61
62
63
//! Subgraph methods for retrieving information on the data source associated
//! with the current mapping execution.

use crate::{
    address::Address,
    entity::{Entity, EntityExt as _},
    ffi::{str::AscString, sys, value::AscArray},
};

/// Data source context.
pub type Context = Entity;

/// Returns the address of the current data source.
pub fn address() -> Address {
    let bytes = unsafe { &*sys::data_source__address() };
    Address::from_raw(bytes)
}

/// Returns the context of the current data source.
pub fn context() -> Context {
    let raw = unsafe { &*sys::data_source__context() };
    Entity::from_raw(raw)
}

/// Returns the network name of the current data source.
pub fn network() -> String {
    let str = unsafe { &*sys::data_source__network() };
    str.to_string_lossy()
}

/// Creates a new data source from a named template with parameters.
pub fn create(name: impl AsRef<str>, params: impl IntoIterator<Item = impl AsRef<str>>) {
    let name = AscString::new(name.as_ref());
    let params = AscArray::new(
        params
            .into_iter()
            .map(|param| AscString::new(param.as_ref()))
            .collect(),
    );

    unsafe { sys::data_source__create(name.as_ptr(), params.as_ptr()) };
}

/// Creates a new data source from a named template with parameters with
/// additional context.
pub fn create_with_context(
    name: impl AsRef<str>,
    params: impl IntoIterator<Item = impl AsRef<str>>,
    context: &Context,
) {
    let name = AscString::new(name.as_ref());
    let params = AscArray::new(
        params
            .into_iter()
            .map(|param| AscString::new(param.as_ref()))
            .collect(),
    );
    let context = context.to_raw();

    unsafe {
        sys::data_source__create_with_context(name.as_ptr(), params.as_ptr(), context.as_ptr())
    };
}