data_store/data_store.rs
1//! This example demonstrates how the *LibraryLink* "DataStore" type can be used to pass
2//! expression-like data structures to and from Rust.
3//!
4//! The "DataStore" can be manipulated natively from Rust code, and is serialized to
5//! an expression structure that looks like:
6//!
7//! ```wolfram
8//! Developer`DataStore[1, "Hello, World!", "key" -> -12.5]
9//! ```
10//!
11//! A "DataStore" contains a sequence of values, which may differ in type, and each
12//! value may optionally be named.
13
14use wolfram_library_link::{self as wll, DataStore, DataStoreNodeValue};
15
16//--------------
17// string_join()
18//--------------
19
20/// Join the strings in a `DataStore`.
21///
22/// This function may be called by evaluating:
23///
24/// ```wolfram
25/// stringJoin = LibraryFunctionLoad["libdata_store", "string_join", {"DataStore"}, "String"];
26///
27/// (* Evaluates to: "hello world" *)
28/// stringJoin[Developer`DataStore["hello", " ", "world"]]
29/// ```
30#[wll::export]
31fn string_join(store: DataStore) -> String {
32 let mut buffer = String::new();
33
34 for node in store.nodes() {
35 // If `node.value()` is a string, append it to our string.
36 // If `node.value()` is NOT a string, silently skip it.
37 if let DataStoreNodeValue::Str(string) = node.value() {
38 buffer.push_str(string);
39 }
40 }
41
42 buffer
43}