-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_store.rs
43 lines (38 loc) · 1.29 KB
/
data_store.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
//! This example demonstrates how the *LibraryLink* "DataStore" type can be used to pass
//! expression-like data structures to and from Rust.
//!
//! The "DataStore" can be manipulated natively from Rust code, and is serialized to
//! an expression structure that looks like:
//!
//! ```wolfram
//! Developer`DataStore[1, "Hello, World!", "key" -> -12.5]
//! ```
//!
//! A "DataStore" contains a sequence of values, which may differ in type, and each
//! value may optionally be named.
use wolfram_library_link::{self as wll, DataStore, DataStoreNodeValue};
//--------------
// string_join()
//--------------
/// Join the strings in a `DataStore`.
///
/// This function may be called by evaluating:
///
/// ```wolfram
/// stringJoin = LibraryFunctionLoad["libdata_store", "string_join", {"DataStore"}, "String"];
///
/// (* Evaluates to: "hello world" *)
/// stringJoin[Developer`DataStore["hello", " ", "world"]]
/// ```
#[wll::export]
fn string_join(store: DataStore) -> String {
let mut buffer = String::new();
for node in store.nodes() {
// If `node.value()` is a string, append it to our string.
// If `node.value()` is NOT a string, silently skip it.
if let DataStoreNodeValue::Str(string) = node.value() {
buffer.push_str(string);
}
}
buffer
}